2010-10-13 15 views
31

मैं एक अलर्टडिअलॉग का उपयोग कर रहा हूं (नीचे कोड देखें) और प्रत्येक पाठ से पहले एक छवि डालना चाहूंगा।चेतावनी संवाद में प्रत्येक आइटम से पहले आइकन कैसे जोड़ें?

उदाहरण के लिए, ईमेल आइकन फिर पाठ "ईमेल", फेसबुक आइकन फिर पाठ "फेसबुक", आदि

निम्नलिखित कोड का उपयोग करना, कैसे एक आइकन प्रत्येक पाठ मान से पहले जोड़ने के लिए?

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

उत्तर

2

कुछ इस तरह कार्य करें:

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 

कुल विचार लेआउट/viewgroup बना सकते हैं और आइकन + पाठ जोड़ने के लिए है + आप viewgroup

+0

कैसे इसे अलर्ट के साथ जोड़ने के लिए? – UMAR

+0

अलर्टडिअलॉग में आमतौर पर ListView शामिल होता है जो संदेश दिखाता है। तो AlertDialog.getListView() की तरह प्राप्त करने का प्रयास करें; और फिर सूची में आइकन/टेक्स्ट जोड़ें के साथ पूरी सामग्री युक्त अपना लेआउट जोड़ें। जैसे: AlertDialog.getListview()। जोड़ें (लेआउट) – barmaley

77

में चाहते हैं जो कुछ भी आप को जोड़ने के लिए एक कस्टम ListAdapter का उपयोग करना चाहिए आपका प्रतिबिम्ब। रास्ते पर ArrayAdapter (AlertDialog द्वारा डिफ़ॉल्ट रूप से उपयोग किया जाता है) को उप-वर्ग करना है।

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

यहाँ है मद वर्ग

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

क्या ड्रायबल आकार सेट करने का कोई तरीका है? – Muzikant

+1

कभी भी ध्यान न दें, बस यह पता लगाया जाए कि यह कैसे करें ... कॉल सेट आवश्यक आकार के साथ बाउंड्स और फिर सेट के लिए सेट कॉम्पैन्डड्राउबल्स को कॉल करेंडॉम्पाउंडड्राबल्स WithIntrinsicBounds – Muzikant

+0

हाय, यह काम करता है लेकिन किसी आइटम के टेक्स्ट का आकार डिफ़ॉल्ट नहीं है - यह बहुत बड़ा है । कोई विचार मैं इसे डिफ़ॉल्ट ग्रंथों के आकार में कैसे सेट कर सकता हूं? – Nedko

1

पैमाने पर करने के लिए छवियों:

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

मैं Tom Esterez के समाधान चाहते हैं, लेकिन RTL के लिए बजाय रिश्तेदार समारोह उपयोग करने की अनुशंसा यहाँ एक उदाहरण है समर्थन। इस के बजाय

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

:

तो इस का उपयोग

tv.setCompoundDrawablesWithIntrinsicBounds(items[position].iconID, 0, 0, 0); 
संबंधित मुद्दे