【问题标题】:Android: How can I change AlertDialog Title Text Color and Background Color without using custom layout?Android:如何在不使用自定义布局的情况下更改 AlertDialog 标题文本颜色和背景颜色?
【发布时间】:2018-12-25 01:57:41
【问题描述】:

我想在不使用自定义布局的情况下更改 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 中返回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>

【问题讨论】:

  • 您可以通过主题更改
  • 试试这个,但它使用自定义视图作为标题。但非常简单整洁的ans。 stackoverflow.com/a/12813088/6060743
  • @SaurabhBhandari,我的问题不同,我想更改标题文本背景而不是对话框背景。
  • @SagarZala 在主题中添加您的文本颜色属性并为背景添加背景属性

标签: android android-alertdialog background-color textcolor


【解决方案1】:

这对我有用,很简单: 我的对话框由于某种原因显示的标题背景与对话框背景不同,因此添加此代码后,问题就解决了。

对话框显示后,用你的颜色设置 setBackgroundDrawable:

mDialog.show();

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • AlertDialog 标题没有帮助。
    【解决方案3】:

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

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

    【讨论】:

    • 我只想更改标题文本颜色和背景颜色。
    • 它会改变你的标题文字颜色
    • 是的,但是标题背景颜色呢?
    • 现在这正是你想要的。
    • 我的问题是没有使用自定义布局,但它是一个很好的解决方案。现在我可以使用它。谢谢。
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      您可以设置主题:

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

      【讨论】:

      • getResources().getIdentifier("alertTitle", "id", "android") 不起作用。返回空对象。
      【解决方案6】:

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

      【讨论】:

      • 使用样式可以改变字体颜色,背景呢??
      • 为此尝试dialog.getWindow().setBackgroundDrawableResource(R.color.glassblack); ...
      • omik,这会将整个对话框背景更改为该颜色。不是标题。
      【解决方案7】:

      我知道您不希望这样做,但使用视图是执行此操作的最佳方式。 使用自定义视图,您可以非常轻松地更改每种颜色。

      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)

      【讨论】:

      • 不,我不想使用自定义布局。
      • @SagarZala 为什么?这非常简单,并且您有更多选项来自定义您的 AlertDialog
      【解决方案8】:

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

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

      希望对你有所帮助。

      【讨论】:

      • 我不想更改按钮颜色。我想更改标题文本和背景颜色。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多