2011-12-16 14 views
21

मैं ListDialogs के बारे में खोज कर रहा हूं। जब भी आप आइटम रख सकते हैं आप के साथ हैं:एक सूची संवाद में आइकन

builder.setItems(items, new DialogInterface.OnClickListener() 
{ 
    public void onClick(DialogInterface dialog, int item) 
    { 

    } 
}); 

और आइटम आपत्ति के बारे में सोच है, जो इस तरह की एक CharSequence है:

CharSequence[] items = getResources().getStringArray(R.array.share_dialog_list); 

मैं जानना चाहता है, तो एक तरह से (कुछ अन्य जरूरी डी) इस अस्तित्व है, लेकिन बाईं ओर माउस के साथ एक कस्टम दृश्य का उपयोग कर, इस तरह के: यह बना दिया है जैसे हम के लिए बनाने के

enter image description here

उत्तर

56

यहाँ की तरह AlertDialog वस्तु में इस दृश्य को जोड़ने के लिए एक विस्तृत ArrayAdapter साथ एक पूर्ण समाधान है कि प्रतीक की अनुमति देता है।

देखें, तो आप ऐसा इन दिखता 48 x 48 डी पी में बहुत अच्छे के लिए आकार, जो एक बंडल आकार नहीं है कि कम से http://developer.android.com/design/downloads/index.html

नोटhttp://developer.android.com/design/style/iconography.html पर http://developer.android.com/design/building-blocks/dialogs.html Iconogaphy पर संवादों और IconPacks के लिए डिजाइन नोट ' डाउनलोड से अपना खुद का आइकन स्केल करना होगा।

उपयोग:

  @Override 
     public void onClick(View v) { 
      final String [] items = new String[] {"From Gallery", "From Camera"}; 
      final Integer[] icons = new Integer[] {R.drawable.dialog_gallery_icon, R.drawable.dialog_camera_icon}; 
      ListAdapter adapter = new ArrayAdapterWithIcon(getActivity(), items, icons); 

      new AlertDialog.Builder(getActivity()).setTitle("Select Image") 
       .setAdapter(adapter, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int item) { 
         Toast.makeText(getActivity(), "Item Selected: " + item, Toast.LENGTH_SHORT).show(); 
        } 
      }).show(); 
     } 

ArrayAdapterWithIcon.java

public class ArrayAdapterWithIcon extends ArrayAdapter<String> { 

private List<Integer> images; 

public ArrayAdapterWithIcon(Context context, List<String> items, List<Integer> images) { 
    super(context, android.R.layout.select_dialog_item, items); 
    this.images = images; 
} 

public ArrayAdapterWithIcon(Context context, String[] items, Integer[] images) { 
    super(context, android.R.layout.select_dialog_item, items); 
    this.images = Arrays.asList(images); 
} 

public ArrayAdapterWithIcon(Context context, int items, int images) { 
    super(context, android.R.layout.select_dialog_item, context.getResources().getTextArray(items)); 

    final TypedArray imgs = context.getResources().obtainTypedArray(images); 
    this.images = new ArrayList<Integer>() {{ for (int i = 0; i < imgs.length(); i++) {add(imgs.getResourceId(i, -1));} }}; 

    // recycle the array 
    imgs.recycle(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view = super.getView(position, convertView, parent); 
    TextView textView = (TextView) view.findViewById(android.R.id.text1); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
     textView.setCompoundDrawablesRelativeWithIntrinsicBounds(images.get(position), 0, 0, 0); 
    } else { 
     textView.setCompoundDrawablesWithIntrinsicBounds(images.get(position), 0, 0, 0); 
    } 
    textView.setCompoundDrawablePadding(
      (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getContext().getResources().getDisplayMetrics())); 
    return view; 
} 

} 
+0

चिह्न स्क्रीन आकार के आधार आकार परिवर्तन नहीं करता है ... यह करता है? – Si8

+1

@ SiKni8, यदि आप उन्हें अन्य स्क्रीन आकारों के लिए अलग-अलग आकारों में चाहते थे तो आपको विभिन्न संसाधन ड्रॉबल्स प्रदान करना होगा। – aaronvargas

+0

@ SiKni8: चूंकि आप संसाधन ड्रॉबल्स का संदर्भ दे रहे हैं, सही स्क्रीन स्क्रीन के आधार पर लिया जाता है, यदि आप इसे सही निर्देशिका में डालते हैं (res/drawable-? Dpi /) – chteuchteu

2

कस्टम दृश्य बनाने के सूची दृश्य

alert_customlist.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp" android:background="#ffffffff"> 
    <ImageView android:layout_width="50dp" android:layout_height="50dp" 
     android:textColor="#ffff0000" android:textSize="20dp" android:id="@+id/text1"/> 
    <TextView android:text="text view two" android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:textColor="#ffff0000" android:textSize="20dp" android:id="@+id/text2"/> 
</LinearLayout> 

अब इस तरह से

जांच इस पोस्ट http://mgmblog.com/2010/06/10/arrayadapter-and-alertdialog-for-single-choice-items/

+1

लिंक मर चुका है ... – kirtan403

संबंधित मुद्दे