列表对话框中的图标

A.Q*_*oga 22 android dialog list

我一直在搜索ListDialogs.每当你可以把你想要的项目放在:

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

   }
});
Run Code Online (Sandbox Code Playgroud)

并且考虑了items对象,这是一个CharSequence:

CharSequence[] items = getResources().getStringArray(R.array.share_dialog_list);
Run Code Online (Sandbox Code Playgroud)

我想知道一种方式(其他一些必须使它:D)使它存在,但使用左侧带有图标的自定义视图,如下所示:

在此输入图像描述

aar*_*gas 61

这是一个包含扩展ArrayAdapter的完整解决方案,允许使用图标.

有关对话框的设计说明,请访问http://developer.android.com/design/building-blocks/dialogs.html Iconogaphy,网址http://developer.android.com/design/style/iconography.html,IconPacks位于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();
        }
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

  • @gbotha,看起来像您需要更改textView.setCompoundDrawablesWithIntrinsicBounds()来textView.setCompoundDrawablesRelativeWithIntrinsicBounds()和离开的参数相同.这是在API 17中添加的,请参阅此问题以获取有关以前API级别的更多帮助,http://stackoverflow.com/questions/18996183/identifyng-rtl-language-in-android (2认同)