Android: 如何在不使用自定义布局的情况下更改 AlertDialog 标题文本颜色和背景颜色?

Android: How can I change AlertDialog Title Text Color and Background Color without using custom layout?

我想更改 AlertDialog 标题颜色背景颜色 而不使用自定义布局。我的要求,

我尝试了下面的代码,但无法正常工作。

final CharSequence[] items = {" Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage(message)
            .setTitle(title).setCancelable(false);

    builder.setItems(items, (dialog, item) -> {
    });

    AlertDialog dialog = builder.create();
            dialog.show();
    int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
    TextView tv = dialog.findViewById(textViewId); // It always returns null
    if (tv != null) {
        tv.setTextColor(activity.getResources().getColor(R.color.white));
        tv.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));
}

我尝试使用以下行,但在 findViewById

中总是 returns null
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId);

我也试过使用 style 但它只会改变标题文本颜色,

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:background">#ffffff</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:headerBackground">@color/colorPrimary</item>
</style>

您可以在提醒对话框中使用自定义标题:

TextView textView = new TextView(context);
textView.setText("Select an option");
textView.setPadding(20, 30, 20, 30);
textView.setTextSize(20F);
textView.setBackgroundColor(Color.CYAN);
textView.setTextColor(Color.WHITE);

final CharSequence[] items = {"Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCustomTitle(textView);
builder.setItems(items, (dialog, item) -> {
    }).show();

您也可以通过代码进行更改。

AlertDialog dialog = builder.create();
dialog.show();
Button buttonPositive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
buttonPositive.setTextColor(ContextCompat.getColor(this, R.color.green));
Button buttonNegative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonNegative.setTextColor(ContextCompat.getColor(this, R.color.red));

希望对你有所帮助。

我知道你不想要这个,但使用视图是最好的方法。 使用自定义视图,您可以非常轻松地更改每种颜色。

  1. 只需创建一个布局并将您的项目放在那里(并更改颜色)

  2. 创建 LayoutInflater:LayoutInflater layoutinflater = getLayoutInflater;

  3. 像这样设置视图:View view1 = layoutinflater.inflate(R.layout.yourlayout, null);

  4. view1 设置为您的构建器:builder.setView(view1);

如果您想在 AlertDialog 中使用项目,请使用 view1 声明您的变量!

示例:Textview tx = (TextView)view1.findViewById(R.id.textview)

IMO 你在设置颜色之前调用 dialog.show(); 所以试试下面的代码

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage(message)
        .setTitle(title).setCancelable(false);
AlertDialog dialog = builder.create();

int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId); // It always returns null
if (tv != null) {
    tv.setTextColor(activity.getResources().getColor(R.color.white));
    tv.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));

    }
  dialog.show(); //change here 

更新

只需尝试将标题设置为在行下方提醒的位置

alert.setTitle( Html.fromHtml("<font color='#FF7F27'>Hello World</font>"));

您可以更改警报对话框中的所有内容:

// Title
TextView titleView = new TextView(context);
titleView.setText("Title");
titleView.setGravity(Gravity.CENTER);
titleView.setPadding(20, 20, 20, 20);
titleView.setTextSize(20F);
titleView.setTypeface(Typeface.DEFAULT_BOLD);
titleView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
titleView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));

AlertDialog ad = new AlertDialog.Builder(context).create();

ad.setCustomTitle(titleView);

ad.setCancelable(false);

ad.setMessage("Message");

ad.setButton(Dialog.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // your code
    }
});

ad.show();

// Message
TextView messageView = ad.findViewById(android.R.id.message);

if (messageView != null) {
    messageView.setGravity(Gravity.CENTER);
}

// Buttons
Button buttonOK = ad.getButton(DialogInterface.BUTTON_POSITIVE);
buttonOK.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));

您可以设置主题:

new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);

或者您可以添加 setOnShowListener(),如下所示:

final CharSequence[] items = {" Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage(message)
            .setTitle(title).setCancelable(false);

builder.setItems(items, (dialog, item) -> {
});

AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface arg0) {
                    int titleId = getResources().getIdentifier("alertTitle", "id", "android");
                    TextView dialogTitle = (TextView) dialog.findViewById(titleId);
                    dialogTitle.setTextColor(Color.WHITE);
                    dialogTitle.setBackgroundColor(Color.BLACK);
                }
 });
 dialog.show();

check sample image

您可以使用自定义主题更改警报对话框标题的颜色、背景和按钮颜色。

   <style name="CustomDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"> 
        <item name="android:windowBackground">@android:color/black</item>
        <item name="colorAccent">@android:color/white</item>
        <item name="android:textColorPrimary">@android:color/white</item>
    </style>

android: windowBackground 改变背景颜色

colorAccent 更改按钮颜色

android:textColorPrimary 更改对话框标题颜色

将此主题应用于对话框

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.CustomDialogTheme);

这就是对我有用的方法,非常简单: 由于某种原因,我的对话框显示的标题背景与对话框背景不同,所以添加这段代码后,问题就解决了。

对话框显示后,设置 setBackgroundDrawable 为您的颜色:

mDialog.show();

mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(put here your color));