如何在警告对话框中每个项目前添加图标?

41

我正在使用AlertDialog(参见下面的代码),想要在每个文本前面放置一张图片。

例如,邮件图标然后是"电子邮件"文本,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();

http://stackoverflow.com/questions/7792931/how-to-design-single-selection-dialog - UMAR-MOBITSOLUTIONS
4个回答

92

你需要自定义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;
    }
}

1
有没有一种方法可以设置可绘制对象的大小? - Muzikant
1
没关系,我已经知道怎么做了...使用所需大小调用setBounds,然后调用setCompoundDrawables而不是setCompoundDrawablesWithIntrinsicBounds。 - Muzikant
嗨,这个可以工作,但是一个项目的文本大小不是默认值 - 它太大了。有什么想法如何将其设置为默认文本大小吗? - Nedko
1
好的,我成功地得到了正确的大小。不要使用android.R.layout.select_dialog_item,而是使用typedArray.getResourceId(R.styleable.AlertDialog_listItemLayout, 0)。该类型数组定义如下:TypedArray typedArray = context.obtainStyledAttributes(null, R.styleable.AlertDialog, R.attr.alertDialogStyle, 0);。我直接从警告对话框列表视图的支持库代码中获取了这个。 - Nedko
即使像@Nedko上面展示的那样使用getResourceId,它仍然不能像使用setItems()时那样呈现项目。 项目左填充似乎更小,并使项目与标题不对齐。 有任何想法如何解决这个问题吗? - Don Box
嘿,你介意帮我一下吗,为什么它不接受点击? - Samrez Ikram

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

总的想法是创建布局/视图组并将图标+文本+您想要的任何内容添加到视图组中。


如何将它与警报关联起来? - UMAR-MOBITSOLUTIONS
AlertDialog通常包含显示消息的ListView。因此,请尝试获取类似AlertDialog.getListView()的内容。然后添加包含整个图标/文本的布局到listView中。例如:AlertDialog.getListview().add(layout)。 - Barmaley

1

我喜欢 Tom Esterez的解决方案,但建议使用相对函数来支持RTL。

因此,请使用以下内容:

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

而不是这个:

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接