【问题标题】:Android Alert Dialog with text and icons带有文本和图标的 Android 警报对话框
【发布时间】:2018-06-03 15:11:15
【问题描述】:

我想要一个带有文本和图标的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);

【问题讨论】:

  • 你为什么setIcon(TypeArray)?从developer.android.com/reference/android/app/…,我只看到int idDrawable在这里。
  • builder.setIcon 不是您正在寻找的方法。该方法是AlertDialog 的遗留方法,当时Android 曾经在对话框界面的标题部分有一个图标——他们不再这样做了。如果您想要带有图标的选项列表,则必须实现自定义对话框 UI。试试看这个:stackoverflow.com/a/15453996/394933.
  • @Brian,谢谢,我会尝试实施建议的解决方案。

标签: android


【解决方案1】:

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

1.添加alerdialog_layout作为视图。

2.在里面使用ListView

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

注意

如果要使用动态图标,可以使用BaseAdapter

alerdialog_layout

<?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>

item_dialog

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:padding="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
    android:id="@+id/iv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher_round"/>

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="right"
    android:text="text1"/>
</LinearLayout>

Java 代码

/**
 * 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();
}

输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多