【问题标题】:How to add an icon before each item in alert dialog?如何在警报对话框中的每个项目之前添加一个图标?
【发布时间】:2011-04-24 15:41:12
【问题描述】:

我正在使用 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();

【问题讨论】:

标签: android android-alertdialog


【解决方案1】:

做这样的事情:

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

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

【讨论】:

  • 如何关联alert?
  • AlertDialog 通常包含显示消息的 ListView。所以试着变得像 AlertDialog.getListView();然后将包含所有内容的布局添加到列表视图中,并添加图标/文本。比如:AlertDialog.getListview().add(layout)
【解决方案2】:

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

【讨论】:

  • 有没有办法设置drawable的大小?
  • 没关系,刚刚知道怎么做...用所需的大小调用 setBounds,然后调用 setCompoundDrawables 而不是 setCompoundDrawablesWithIntrinsicBounds
  • 嗨,这可行,但项目文本的大小不是默认值 - 它太大了。有什么想法如何将其设置为默认文本大小?
  • 好的,我设法得到了合适的尺寸。而不是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);。我直接从警报对话框列表视图的支持库代码中获取了这个。
  • 即使使用getResourceId 就像上面显示的@Nedko,它仍然不会像使用setItems() 时那样呈现项目。项目左填充似乎更小,并使项目与标题不对齐。知道如何解决这个问题吗?
【解决方案3】:

缩放图像:

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

【讨论】:

    【解决方案4】:

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

    所以用这个:

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

    而不是这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 2012-11-20
      相关资源
      最近更新 更多