2011-12-16 38 views
21

我一直在搜索ListDialogs。每當你可以把項目要與:列表對話框中的圖標

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

    } 
}); 

和思維有關的項目對象,至極是這樣一個的CharSequence:

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

我想知道,如果一個方法(其他一些必須的使人們:d)使這個存在,但使用與圖標的自定義視圖的左邊,這樣的:

enter image description here

回答

56

這裏是一個擴展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×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; 
} 

} 
+0

圖標沒有根據屏幕大小改變大小...不是嗎? – Si8 2013-09-06 20:23:51

2

使自定義視圖就像我們創造列表視圖

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對象像這樣

檢查這個帖子http://mgmblog.com/2010/06/10/arrayadapter-and-alertdialog-for-single-choice-items/

+1

Link是死了...... – kirtan403 2016-09-17 12:59:34