Dưới đây là một giải pháp hoàn chỉnh với một ArrayAdapter mở rộng mà cho phép các biểu tượng.
Xem ghi chú thiết kế cho các hộp thoại tại http://developer.android.com/design/building-blocks/dialogs.html Iconogaphy tại http://developer.android.com/design/style/iconography.html và IconPacks tại http://developer.android.com/design/downloads/index.html
Note rằng kích thước cho các ngoại hình khá tốt tại 48 x 48 dp, mà không phải là một kích thước đóng gói, vì vậy bạn' sẽ phải mở rộng biểu tượng của riêng bạn từ các bản tải xuống.
SỬ DỤNG:
@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;
}
}
The Icon không thay đổi kích thước dựa trên kích thước màn hình ... đúng không? – Si8
@ SiKni8, bạn phải cung cấp các bản vẽ tài nguyên khác nhau nếu bạn muốn chúng ở các kích thước khác nhau cho các kích thước màn hình khác. – aaronvargas
@ SiKni8: vì bạn đang tham chiếu đến các tài nguyên có thể vẽ được, bạn phải chọn tùy chọn kích thước màn hình, nếu bạn đặt nó vào đúng thư mục (res/drawable-? Dpi /) – chteuchteu