Android警告对话框更改默认蓝色为其他颜色

4
我正在使用单选警报对话框,在其中我想将默认的蓝色(标题行和单选按钮)替换为我在标题栏中使用的橙色。我已经成功使用setCustomTitle()更改了标题栏,但是我无法摆脱这个可恶的蓝色。 对于标题栏:
<?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"
    android:background="@color/orange" >

    <TextView
        android:id="@+id/alertTitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="14dp"
        android:gravity="center"
        android:text="Alert Title"
        android:textColor="@color/white"
        android:textSize="18sp" />    

</LinearLayout>

构建AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View customTitle = View.inflate(MainActivity.this, R.layout.custom_alert_title, null);
builder.setCustomTitle(customTitle);
builder.setSingleChoiceItems(mAlertOptions, -1, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {                    
        // do stuff
        dialog.dismiss();
    }
}).create().show();

这是它的外观

在此输入图片描述

我需要去掉这个蓝色!帮帮我!

1个回答

11

更改标题分隔线颜色的唯一方法是使用Resources.getIdentifierWindow.findViewById相结合。可以通过调用AlertDialog.Builder.setSingleChoiceItems(ListAdapter, int, OnClickListener)轻松更改复选标记。

以下是一个示例:

单选项布局 :我使用Android Holo Colors生成了your_radio_button

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkMark="@drawable/your_radio_button"
    android:ellipsize="marquee"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingEnd="16dip"
    android:paddingStart="16dip"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="?android:attr/textColorAlertDialogListItem" />

实施

    final ListAdapter adapter = new ArrayAdapter<String>(this,
            R.layout.select_dialog_singlechoice, android.R.id.text1, new String[] {
                    "Option 1", "Option 2"
            });

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCustomTitle(getLayoutInflater().inflate(R.layout.custom_alert_title, null));
    builder.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Do something
        }
    });

    // Show the AlertDialog
    final AlertDialog dialog = builder.show();

    // Change the title divider
    final Resources res = getResources();
    final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
    final View titleDivider = dialog.findViewById(titleDividerId);
    titleDivider.setBackgroundColor(res.getColor(android.R.color.holo_orange_dark));

结果

示例


1
太好了!非常感谢!最后一个问题,你知道如何消除行被点击时闪烁的蓝色高亮吗?这似乎是最后一处蓝色。 - Kevin_TA
@Kelvin_TA 将此属性添加到 checkedTextView:android:background="?android:attr/selectableItemBackground"。但请记住,您必须将 clickable 和 focusable 属性设置为 true 才能使其正常工作,方法是添加 android:clickable="true" 和 android:focusable="true"。 - chisom emmanuel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接