[英]How to add an icon before each item in alert dialog?
我正在使用AlertDialog(請參見下面的代碼),並希望在每個文本之前放置一個圖像。
例如,電子郵件圖標,然后輸入文本“ Email”,Facebook圖標,然后輸入文本“ Facebook”,等等。
使用以下代碼,如何在每個文本值之前添加圖標?
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();
您需要自定義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();
這是Item類
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;
}
}
做這樣的事情:
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
總的想法是創建布局/視圖組並將圖標+文本+所需的任何內容添加到視圖組
要縮放圖像:
//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);
我喜歡Tom Esterez的解決方案,但建議使用相對函數代替RTL支持。
所以使用這個:
tv.setCompoundDrawablesRelativeWithIntrinsicBounds(items[position].iconID, 0, 0, 0);
代替這個:
tv.setCompoundDrawablesWithIntrinsicBounds(items[position].iconID, 0, 0, 0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.