列表对话框中的图标

25

我一直在搜索ListDialogs。每当您可以使用“:”将所需项目放置其中时:

builder.setItems(items, new DialogInterface.OnClickListener() 
{
   public void onClick(DialogInterface dialog, int item) 
   {
                        
   }
});

考虑一下items对象,它是一个字符序列,就像这样:

CharSequence[] items = getResources().getStringArray(R.array.share_dialog_list);

我想知道是否有一种方法(其他人必须制作)可以使用带有图标的自定义视图来创建这个,就像这样:

进入图像描述

2个回答

64

这里有一个扩展了ArrayAdapter的完整解决方案,可以允许图标。

查看设计笔记以获取对话框的详情,请前往http://developer.android.com/design/building-blocks/dialogs.html;图标详见http://developer.android.com/design/style/iconography.html;图标包下载请访问http://developer.android.com/design/downloads/index.html

注意:这些图标的大小在48 x 48 dp下看起来非常好,这不是一个包含的大小,因此您需要从下载的文件中自行缩放您的图标。

用法:

            @Override
        public void onClick(View v) {
            final String [] items = new String[] {"From Gallery", "From Camera"};
            final Integer[] icons = new Integer[] {R.drawable.dialog_gallery_icon, R.drawable.dialog_camera_icon};
            ListAdapter adapter = new ArrayAdapterWithIcon(getActivity(), items, icons);

            new AlertDialog.Builder(getActivity()).setTitle("Select Image")
                .setAdapter(adapter, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item ) {
                        Toast.makeText(getActivity(), "Item Selected: " + item, Toast.LENGTH_SHORT).show();
                    }
            }).show();
        }

ArrayAdapterWithIcon.java

public class ArrayAdapterWithIcon extends ArrayAdapter<String> {

private List<Integer> images;

public ArrayAdapterWithIcon(Context context, List<String> items, List<Integer> images) {
    super(context, android.R.layout.select_dialog_item, items);
    this.images = images;
}

public ArrayAdapterWithIcon(Context context, String[] items, Integer[] images) {
    super(context, android.R.layout.select_dialog_item, items);
    this.images = Arrays.asList(images);
}

public ArrayAdapterWithIcon(Context context, int items, int images) {
    super(context, android.R.layout.select_dialog_item, context.getResources().getTextArray(items));

    final TypedArray imgs = context.getResources().obtainTypedArray(images);
    this.images = new ArrayList<Integer>() {{ for (int i = 0; i < imgs.length(); i++) {add(imgs.getResourceId(i, -1));} }};

    // recycle the array
    imgs.recycle();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    TextView textView = (TextView) view.findViewById(android.R.id.text1);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        textView.setCompoundDrawablesRelativeWithIntrinsicBounds(images.get(position), 0, 0, 0);
    } else {
        textView.setCompoundDrawablesWithIntrinsicBounds(images.get(position), 0, 0, 0);
    }
    textView.setCompoundDrawablePadding(
            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getContext().getResources().getDisplayMetrics()));
    return view;
}

}

1
@SiKni8,如果您希望在其他屏幕尺寸上以不同的大小显示它们,则需要提供不同的资源可绘制项。 - aaronvargas
@SiKni8:由于您正在引用资源可绘制对象,因此根据屏幕大小选择正确的对象,如果将其放在正确的目录中(res/drawable-?dpi/)。 - Quentin S.
@aaronvargas ... 我使用了你的alertDialog代码片段,但是getActivity()没有被识别... 当我将代码放在MainActivity.class中时。 - anand
getActivity() 用于获取上下文。如果您在Activity中使用它,请改用'this',因为Activity本身就是一个上下文。 - aaronvargas
2
@gbotha,看起来你需要将textView.setCompoundDrawablesWithIntrinsicBounds()更改为textView.setCompoundDrawablesRelativeWithIntrinsicBounds(),并保持参数不变。这是在API 17中添加的,请参阅此问题以获取有关先前API级别的更多帮助,https://dev59.com/jmMk5IYBdhLWcg3wxAhM - aaronvargas
显示剩余4条评论

3

将自定义视图制作得像我们为列表视图创建的那样

alert_customlist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp" android:background="#ffffffff">
    <ImageView android:layout_width="50dp" android:layout_height="50dp"
        android:textColor="#ffff0000" android:textSize="20dp" android:id="@+id/text1"/>
    <TextView android:text="text view two" android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:textColor="#ffff0000" android:textSize="20dp" android:id="@+id/text2"/>
</LinearLayout>

现在将此视图添加到AlertDialog中。

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