Android警报对话框用另一种颜色替换默认蓝色

时间:2014-04-23 21:42:50

标签: android android-layout alertdialog android-alertdialog

我使用单选警告对话框,其中我想将默认蓝色(标题行和单选按钮)替换为我在标题栏中使用的橙色。我可以使用setCustomTitle()更改为标题栏,但是我想要摆脱这个该死的蓝色。{/ p>

标题栏

<?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();

这就是它的样子

enter image description here

我需要摆脱这个蓝色!救命啊!

1 个答案:

答案 0 :(得分: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));

<强>结果

Example