Android的警告对话框替代默认的蓝色与其他颜色 [英] Android Alert Dialog replace default blue with another color

查看:736
本文介绍了Android的警告对话框替代默认的蓝色与其他颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是单一的选择中,我想,以取代默认的蓝色(标题行和单选按钮),我使用的标题栏中的橙色警报对话框。我可以使用 setCustomTitle(),但我失去了试图摆脱这个该死的蓝更改为标题栏。

I'm using a single choice alert dialog in which I'd like to replace the default blue (the title line and the radio buttons) to the orange that I am using in the title bar. I was able to change to the title bar using setCustomTitle(), but I am lost trying to get rid of this damn blue.

作为标题栏

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

这是什么样子

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

I need to get rid of this blue! Help!

推荐答案

要更改标题分颜色的唯一方法就是使用 Resources.getIdentifier 联合 Window.findViewById 。该对号可以通过调用轻易改变 AlertDialog.Builder.setSingleChoiceItems(ListAdapter,INT,OnClickListener)

The only way to change the title divider color is to use Resources.getIdentifier in combination with Window.findViewById. The checkmark can be easily changed by calling AlertDialog.Builder.setSingleChoiceItems(ListAdapter, int, OnClickListener).

下面是一个例子:

单选项目布局 :我生成 your_radio_button 使用 Android的全息颜色

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

结果

这篇关于Android的警告对话框替代默认的蓝色与其他颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆