我一直在搜索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)使其存在,但使用左侧带有图标的自定义视图,如下所示:
答案 0 :(得分:61)
这是一个包含扩展ArrayAdapter的完整解决方案,允许使用图标。
请参阅位于http://developer.android.com/design/building-blocks/dialogs.html的http://developer.android.com/design/style/iconography.html Iconogaphy和位于http://developer.android.com/design/downloads/index.html的IconPacks
上的对话框设计说明注意这些尺寸相当于48 x 48 dp,这不是捆绑尺寸,因此您必须从下载中缩放自己的图标。
USAGE :
@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;
}
}
答案 1 :(得分: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中。