Android AlertDialog - 标题背景颜色

rad*_*dan 5 android android-alertdialog

我正在尝试更改AlertDialog的"标题"(顶部)的背景颜色.我设法改变标题的颜色,但我找不到你如何改变其容器的背景颜色.可能吗?有什么建议?

这就是我到目前为止所拥有的.

AndroidManifest.xml中

<application
    ...
    android:theme="@style/AppTheme">
Run Code Online (Sandbox Code Playgroud)

styles.xml

<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
</style>

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/ActionBarStyle</item>
    <item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
</style>
Run Code Online (Sandbox Code Playgroud)

another_file_with_styles.xml

<style name="AlertDialogTheme" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:textColor">@color/success_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)

类中的方法可以做到这一点

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(searchCriteria.getName());
builder.setItems(items, clickListener);

AlertDialog alert = builder.create();
alert.show();

// Eventually I'll do this to change the color of the divider
// int titleDividerId = context.getResources().getIdentifier("titleDivider", "id", "android");
// View titleDivider = alert.findViewById(titleDividerId);
//
// if (titleDivider != null) {
// titleDivider.setBackgroundColor(context.getResources().getColor(R.color.accent_color));
//}
Run Code Online (Sandbox Code Playgroud)

我试图按照本教程,但它没有解释如何更改窗口的背景颜色.

编辑:只是为了清楚,箭头指向灰色/白色背景颜色(不是标题[制造和模型])

在此输入图像描述

use*_*678 5

您可以使用自定义标题视图设置标题的背景颜色。

创建自定义标题视图并定义背景颜色:

res/布局/custom_title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroundColor"
    android:padding="16dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/textColor" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

设置自定义标题视图:

View customTitleView = getLayoutInflater().inflate(R.layout.custom_title, null);
TextView title = (TextView) customTitleView.findViewById(R.id.title);
title.setText("TITLE");

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setItems(items, clickListener);
builder.setCustomTitle(customTitleView);

AlertDialog alert = builder.create();
alert.show();
Run Code Online (Sandbox Code Playgroud)