在列表对话框图标对话框、图标、列表

2023-09-06 02:13:07 作者:城南花开

我一直在寻找有关ListDialogs。每当你可以把你想用的项目:

I have been searching about ListDialogs . Whenever you can put the item you want with the :

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

   }
});

和思考的项目对象,至极是这样的的CharSequence:

And thinking about the the items object , wich is a CharSequence like this :

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

我想知道,如果一个方法(其他一些必须做的:D),使这个存在,但使用自定义视图图标的左边,这样的:

I wanna know if a way (some other must have made it :D ) to make this exist but using a custom view with icons to the left , like this :

推荐答案

下面是一个扩展ArrayAdapter,使图标的完整解决方案。

Here is a complete solution with an extended ArrayAdapter that allows icons.

请参阅设计笔记对话框的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

See design notes for dialogs at http://developer.android.com/design/building-blocks/dialogs.html Iconogaphy at http://developer.android.com/design/style/iconography.html and IconPacks at http://developer.android.com/design/downloads/index.html

注意对于这些看起来pretty的好,48×48 DP,这不是捆绑的大小,所以你必须从下载扩展自己的图标的大小。

Note that the size for these looks pretty good at 48 x 48 dp, which isn't a bundled size, so you'll have to scale your own icon from the downloads.

用法

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

@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);
    textView.setCompoundDrawablesWithIntrinsicBounds(images.get(position), 0, 0, 0);
    textView.setCompoundDrawablePadding(
            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getContext().getResources().getDisplayMetrics()));
    return view;
}

}