【问题标题】:Icons in a List dialog列表对话框中的图标
【发布时间】:2012-01-21 22:25:17
【问题描述】:

我一直在搜索 ListDialogs 。只要你可以把你想要的物品放在:

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

想想 items 对象,它是一个像这样的 CharSequence:

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

我想知道是否有办法(一定是其他人做到了)使其存在,但使用左侧图标的自定义视图,如下所示:

【问题讨论】:

    标签: android list dialog


    【解决方案1】:

    像我们为列表视图创建的自定义视图

    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 中。

    【讨论】:

      【解决方案2】:

      这是一个完整的解决方案,带有一个允许图标的扩展 ArrayAdapter。

      请参阅http://developer.android.com/design/building-blocks/dialogs.htmlhttp://developer.android.com/design/style/iconography.html 上的 Iconogaphy 和http://developer.android.com/design/downloads/index.html 上的 IconPacks 的对话框设计说明

      请注意,这些尺寸在 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;
      }
      
      }
      

      【讨论】:

      • 图标不会根据屏幕大小改变大小...是吗?
      • @SiKni8,如果您希望其他屏幕尺寸具有不同尺寸,则必须提供不同的资源可绘制对象。
      • @SiKni8 : 因为你引用了资源drawables,所以如果你把它放在正确的目录(res/drawable-?dpi/)中,根据屏幕大小选择正确的一个
      • @aaronvargas ...我已将您的 sn-p 用于 alertDialog 但 getActivity() 它无法识别...当我将代码放入 MainActivity.class 时
      • @gbotha,看起来您需要将 textView.setCompoundDrawablesWithIntrinsicBounds() 更改为 textView.setCompoundDrawablesRelativeWithIntrinsicBounds() 并保持参数不变。这是在 API 17 中添加的,有关以前 API 级别的更多帮助,请参阅此问题,stackoverflow.com/questions/18996183/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 2011-05-05
      • 1970-01-01
      • 2022-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多