首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android警报对话框用另一种颜色替换默认的蓝色

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

Stack Overflow用户
提问于 2014-04-23 21:42:50
回答 1查看 7K关注 0票数 4

我使用一个单一的选择警报对话框,在这个对话框中,我希望将默认的蓝色(标题行和单选按钮)替换为我在标题栏中使用的橙色。我能够用setCustomTitle()换到标题栏,但是我无法摆脱这个该死的蓝色。

标题栏

代码语言:javascript
运行
复制
<?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

代码语言:javascript
运行
复制
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();

,这就是它看起来像

我要摆脱这蓝色!帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-23 22:51:50

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

下面是一个例子:

单选择项布局:我使用Android Holo颜色。生成了your_radio_button

代码语言:javascript
运行
复制
<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" />

Implementation

代码语言:javascript
运行
复制
    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));

结果

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23255776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档