2011-12-16 16 views
21

ListDialogs hakkında araştırma yapıyorum. öğeyi koyabilirsiniz zaman sizinle istiyorum:Liste iletişim kutusundaki simgeler

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

    } 
}); 

Ve öğeleri nesne düşünmeye, hangi bir böyle CharSequence: Ben bilmek istiyorum

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

eğer bir yol (diğer bazı zorunluluk D) Bu mevcut yapmak ama böyle sola simgelerle özel bir görünüm kullanarak: o yaptık biz yaratmak gibi

enter image description here

cevap

56

gibi AlertDialog nesnesine bu görünüm eklemek uzatılmış ArrayAdapter ile tam bir çözüm olduğunu simgelere izin verir.

bakın http://developer.android.com/design/downloads/index.html

Not de http://developer.android.com/design/style/iconography.html de http://developer.android.com/design/building-blocks/dialogs.html Iconogaphy de diyaloglar ve IconPacks için tasarım notları olduğu bir paket boyutu değil 48 x 48 dp oldukça iyi bu görünüşleri için boyut, sana böylece' indirmelerinizden kendi simgenizi ölçeklendirmelisiniz.

KULLANIMI:

  @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

Simge öyle ... ekran boyutuna göre boyutu değişmez? – Si8

+1

@ SiKni8, farklı ekran boyutlarında farklı boyutlarda olmasını isterseniz farklı kaynak çekilişleri sağlamanız gerekir. – aaronvargas

+0

@ SiKni8: kaynak çizgilerine atıfta bulunduğunuz için, doğru boyuta yerleştirildiyse, ekran boyutuna bağlı olarak doğru olanı (res/drawable-? Dpi /) – chteuchteu

2

özel görünümü yapmak liste görünümü

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> 

anda bu şekilde

onay İşte bu yayını http://mgmblog.com/2010/06/10/arrayadapter-and-alertdialog-for-single-choice-items/

+1

Bağlantı öldü ... – kirtan403

İlgili konular