2011-12-16 3 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

처럼에 AlertDialog 객체에이보기를 추가하는 확장 된 ArrayAdapter와 가진 완벽한 솔루션입니다 아이콘을 허용합니다.

참조 http://developer.android.com/design/downloads/index.html

에서 http://developer.android.com/design/style/iconography.html에서 http://developer.android.com/design/building-blocks/dialogs.html Iconogaphy에서 대화 상자와 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; 
} 

} 
+0

에 아이콘은 않습니다 ... 화면 크기에 따라 크기를 변경하지 않는 이유는 무엇입니까? – Si8

+1

@ SiKni8의 경우, 다른 크기의 다른 화면 크기를 원하면 다른 리소스 드로어 블을 제공해야합니다. – aaronvargas

+0

@ SiKni8 : 리소스 드로어 블을 참조하고 있기 때문에 올바른 디렉토리 (res/drawable-? dpi /)에 넣으면 화면 크기에 따라 올바른 이미지가 표시됩니다 – chteuchteu

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> 

지금이 방법

체크 다음은이 게시물에 http://mgmblog.com/2010/06/10/arrayadapter-and-alertdialog-for-single-choice-items/

+1

링크가 죽었다는 ... – kirtan403