2010-10-13 15 views
31

Używam AlertDialog (patrz poniższy kod) i chciałbym umieścić obraz przed każdym tekstem.Jak dodać ikonę przed każdym elementem w oknie dialogowym alertu?

Na przykład, ikona email potem tekst „Wyślij”, Facebook ikona wtedy tekst „Facebook”, itp

Korzystanie poniższy kod, jak dodać ikonę przed każdym wartości tekstowej?

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

Odpowiedz

2

zrobić coś takiego:

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 

Razem pomysłem jest stworzenie Układ/viewgroup i dodać ikonę + tekst + co chcesz w viewgroup

+0

jak skojarzyć go z alertem? – UMAR

+0

AlertDialog zwykle zawiera ListView, który wyświetla wiadomości. Więc postaraj się dostać jak AlertDialog.getListView(); A następnie dodaj swój układ zawierający całe rzeczy za pomocą ikony/tekstu dodaj do listy widoku. jak: AlertDialog.getListview(). add (layout) – barmaley

77

Musisz użyć niestandardowego ListAdapter dodać Twój obraz. Na drodze jest podklasa ArrayAdapter (używana domyślnie przez AlertDialog). Oto przykład:

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(); 

Oto klasa Element

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

Czy istnieje sposób na ustawienie rozmiaru do rysowania? – Muzikant

+1

Nieważne, po prostu dowiedz się, jak to zrobić ... wywołaj setBounds z wymaganym rozmiarem, a następnie wywołaj setCompoundDrawables zamiast setCompoundDrawablesWithIntrinsicBounds – Muzikant

+0

Cześć, to działa, ale rozmiar tekstu elementu nie jest domyślny - jest za duży . Czy są jakieś przemyślenia na temat tego, jak ustawić domyślny rozmiar tekstu? – Nedko

1

skalować obrazy:

//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

lubię Tom Esterez jest rozwiązanie, ale zalecamy korzystanie z funkcji względnego zamiast RTL wsparcie.

Więc użyj tego:

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

zamiast tego:

tv.setCompoundDrawablesWithIntrinsicBounds(items[position].iconID, 0, 0, 0); 
Powiązane problemy