2010-10-13 22 views
31

Bir AlertDialog kullanıyorum (aşağıdaki koda bakın) ve her metinden önce bir resim koymak istiyorum.Uyarı iletişim kutusundaki her öğeden önce bir simge nasıl eklenir?

Örneğin, e-posta simgesi sonra vb

metin "E-posta", Facebook simgesi sonra metin "Facebook", aşağıdaki kodu kullanarak, nasıl her metin değerinden önce bir simge eklenir?

final CharSequence[] items = { "Email", "Facebook", "Twitter", "LinkedIn" }; 
AlertDialog.Builder builder = new AlertDialog.Builder(More.this); 
builder.setTitle("Share Appliction"); 
builder.setItems(items, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int item) { 
     if (item == 0) { 

     } else if (item == 1) { 

     } else if (item == 2) { 

     } else if(item == 3) { 

     } 
    } 
}); 
AlertDialog alert = builder.create(); 
alert.show(); 
+0

http://stackoverflow.com/questions/7792931/how-to-design-single-selection-dialog – UMAR

cevap

2

böyle bir şey yapın:

ViewGroup layout=new LinearLayout(context); 
TextView tv=new TextView(context); //your text 
tv.setText("my text"); 
ImageView imageView=new ImageView(context); //your icon 
//filling image view with icon bitmap (in this case from resource) 
imageView.setImageBitmap(BitmapFactory.decodeStream(context.getResources().openRawResource(resourceId))); 
//ensuring that icon size will be more or less like text height 
imageView.setAdjustViewBounds(true); 
imageView.setMaxHeight((int)(tv.getLineHeight()*1.5)); 
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
layout.addView(imageView); //adding icon 
tv.setGravity(Gravity.BOTTOM|Gravity.LEFT); 
layout.addView(tv); //adding text 

Toplam fikri simgesi + metin düzen/viewgroup oluşturmak ve eklemektir + sen ekleyebilir için özel bir ListAdapter kullanmalıdır viewgroup

+0

nasıl onu uyarı ile ilişkilendirmek için? – UMAR

+0

AlertDialog genellikle iletileri gösteren ListView içerir. Yani AlertDialog.getListView() gibi olsun deneyin! Ardından, Simge/Metin ekle listesindeki tüm öğeleri içeren düzeninizi listView'e ekleyin. gibi: AlertDialog.getListview(). add (düzen) – barmaley

77

içine istersen senin resim. Yolda ArrayAdapter'u (varsayılan olarak AlertDialog tarafından kullanılır) alt sınıfa ayırmaktır. İşte

final Item[] items = { 
    new Item("Email", android.R.drawable.ic_menu_add), 
    new Item("Facebook", android.R.drawable.ic_menu_delete), 
    new Item("...", 0),//no icon for this one 
}; 

ListAdapter adapter = new ArrayAdapter<Item>(
    this, 
    android.R.layout.select_dialog_item, 
    android.R.id.text1, 
    items){ 
     public View getView(int position, View convertView, ViewGroup parent) { 
      //Use super class to create the View 
      View v = super.getView(position, convertView, parent); 
      TextView tv = (TextView)v.findViewById(android.R.id.text1); 

      //Put the image on the TextView 
      tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0); 

      //Add margin between image and text (support various screen densities) 
      int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); 
      tv.setCompoundDrawablePadding(dp5); 

      return v; 
     } 
    }; 


new AlertDialog.Builder(this) 
    .setTitle("Share Appliction") 
    .setAdapter(adapter, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
      //... 
     } 
    }).show(); 

Ürün sınıfı

public static class Item{ 
    public final String text; 
    public final int icon; 
    public Item(String text, Integer icon) { 
     this.text = text; 
     this.icon = icon; 
    } 
    @Override 
    public String toString() { 
     return text; 
    } 
} 
+1

Çizilebilir boyutu ayarlamak için bir yolu var mı? – Muzikant

+1

Boşver, sadece nasıl yapılacağını öğrendim ... gerekli büyüklükte setSatırlar ve sonra setCompoundDrawablesWithIntrinsicBounds – Muzikant

+0

yerine setCompoundDrawables çağrısı Merhaba, bu çalışır ancak bir öğenin metnin boyutu varsayılan değil - çok büyük . Varsayılan metinler boyutuna nasıl ayarlayabilirim? – Nedko

1

ölçeklemek için görüntüleri edilir:

//Put the image on the TextView 
int dp50 = (int) (50 * getResources().getDisplayMetrics().density + 0.5f); 
Drawable dr = getResources().getDrawable(R...YOUR_DRAWABLE); 
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap(); 
Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, dp50, dp50, true)); 
tv.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null); 

// Add margin between image and text (support various screen densities) 
int dp10 = (int) (10 * getResources().getDisplayMetrics().density + 0.5f); 
tv.setCompoundDrawablePadding(dp10); 
1

Ben Tom Esterez 'ın çözüm gibi ama RTL için yerine nispi işlevini kullanarak tavsiye İşte bir örnek destek.

Yani bu kullanın:

tv.setCompoundDrawablesRelativeWithIntrinsicBounds(items[position].iconID, 0, 0, 0); 

bunun yerine:

tv.setCompoundDrawablesWithIntrinsicBounds(items[position].iconID, 0, 0, 0); 
İlgili konular