列表对话框中的图标 [英] Icons in a List dialog

查看:101
本文介绍了列表对话框中的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索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 at 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

注意的大小e看起来相当不错,48 x 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);
}

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

}

这篇关于列表对话框中的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆