带有文本和图标的Android警报对话框

带有文本和图标的Android警报对话框,android,Android,我想要一个带有文本和图标的AlertDialog。我已经创建了两个数组,一个用于对话框项,一个用于对话框项图标,但它不起作用。以下是我目前的代码: CharSequence options[] = new CharSequence[] {"RETEN", "ACCIDENTE VIAL", "INUNDACION", "ABUSO", "ASALTO / VIOLENCIA"}; builder = new AlertDialog.Builder(getActivity(), android.R

我想要一个带有文本和图标的
AlertDialog
。我已经创建了两个数组,一个用于对话框项,一个用于对话框项图标,但它不起作用。以下是我目前的代码:

CharSequence options[] = new CharSequence[] {"RETEN", "ACCIDENTE VIAL", "INUNDACION", "ABUSO", "ASALTO / VIOLENCIA"};
builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_DeviceDefault_Settings);
builder.setCancelable(false);
Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.alertas);
builder.setIcon(icons);

builder.setTitle(getContext().getString(R.string.select_alert_type));
builder.setItems(options, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // the user clicked on options[which]

        //1.RECUPERAR OPCION SELECCIONADA

        if (which == 0){ ... }

        ...

    });
我在电话线上收到一条警告:

builder.setIcon(icons);

您可以尝试在
AlertDialog.Builder
中使用
setView
方法

1.添加一个
alerdialog\u布局作为视图。

2.在其中使用
ListView

3.使用
setView
向其中添加数据。

注意

/**
 * add dialog
 */
protected void addDailog() {
    CharSequence options[] = new CharSequence[]{"RETEN", "ACCIDENTE VIAL", "INUNDACION", "ABUSO", "ASALTO / VIOLENCIA"};
    View view = LayoutInflater.from(this).inflate(R.layout.alerdialog_layout, null);
    ListView listView = view.findViewById(R.id.list_view);
    ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.item_dialog, R.id.tv1, options){};
    listView.setAdapter(arrayAdapter);
    AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("AlertDialog")
            .setMessage("AlertDialog Message")
            .setView(view)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            })
            .setNegativeButton("No", null)
            .create();
    dialog.show();
}
如果要使用动态图标,可以使用
BaseAdapter

alerdialog\u布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>
</LinearLayout>
输出

您可以尝试使用
AlertDialog.Builder
中的
setView
方法

1.添加一个
alerdialog\u布局作为视图。

2.在其中使用
ListView

3.使用
setView
向其中添加数据。

注意

/**
 * add dialog
 */
protected void addDailog() {
    CharSequence options[] = new CharSequence[]{"RETEN", "ACCIDENTE VIAL", "INUNDACION", "ABUSO", "ASALTO / VIOLENCIA"};
    View view = LayoutInflater.from(this).inflate(R.layout.alerdialog_layout, null);
    ListView listView = view.findViewById(R.id.list_view);
    ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.item_dialog, R.id.tv1, options){};
    listView.setAdapter(arrayAdapter);
    AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("AlertDialog")
            .setMessage("AlertDialog Message")
            .setView(view)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            })
            .setNegativeButton("No", null)
            .create();
    dialog.show();
}
如果要使用动态图标,可以使用
BaseAdapter

alerdialog\u布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>
</LinearLayout>
输出

为什么
设置图标(TypeArray)
?从中,我只看到
int-id
Drawable
在这里。
builder.setIcon
不是您要寻找的方法。当Android以前在对话框界面的标题部分有一个图标时,这种方法是
AlertDialog
的传统方法——他们不再这样做了。如果你想要一个带有图标的选项列表,你必须实现一个自定义的对话框用户界面。试试看:。@Brian,谢谢,我将尝试实施建议的解决方案。你为什么
setIcon(TypeArray)
?从中,我只看到
int-id
Drawable
在这里。
builder.setIcon
不是您要寻找的方法。当Android以前在对话框界面的标题部分有一个图标时,这种方法是
AlertDialog
的传统方法——他们不再这样做了。如果你想要一个带有图标的选项列表,你必须实现一个自定义的对话框用户界面。试着看一下:。@Brian,谢谢,我将尝试实施建议的解决方案。