【问题标题】:AlertDialog change background color in a fragment [duplicate]AlertDialog 更改片段中的背景颜色 [重复]
【发布时间】:2019-01-16 08:03:16
【问题描述】:

您好,如何更改fragmentAlertDialog 的背景?

我的AlertDialog 将在我单击按钮时显示(片段->按钮->警报对话框)。

我在实现我的AlertDialog 并更改其背景颜色时尝试了以下代码:

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(R.string.scaleTitle);
builder.setView(R.layout.scale_layout);
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        dialogInterface.dismiss();
    }
});
AlertDialog alertDialog = builder.create();
builder.show();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));  

但背景还是一样的。

请问,我怎么可能改变它?谢谢。

【问题讨论】:

  • 尝试使用 setBackgroundDrawableResource 代替 setBackgroundDrawable
  • @ShivamKumar 我也尝试了 setBackgroundDrawableResource 但它没有效果。谢谢你的建议。
  • @KoralReef 你能分享你想要的和你得到的截图吗

标签: java android fragment android-alertdialog


【解决方案1】:

在你的 res-->values-->styles.xml 中定义这样的样式

<style name="CustomAlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:colorBackground">@color/dialogColor</item>
    <item name="android:windowBackground">@color/dialogWindowColor</item>
</style>

并使用上述主题资源创建构建器。

AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style. 
CustomAlertDialogTheme);

https://developer.android.com/reference/android/app/AlertDialog.Builder.html#AlertDialog.Builder(android.content.Context,%20int)

【讨论】:

    【解决方案2】:

    您可以尝试下面的黄色 AlertDialog 代码。

    在 Java 中:

    View view = LayoutInflater.from(this).inflate(R.layout.dialog, null);
            View customTitleView = LayoutInflater.from(this).inflate(R.layout.title, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogCustom);
            builder.setView(view);
            builder.setCustomTitle(customTitleView);
            builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
    

    在 style.xml 中:

     <style name="AlertDialogCustom" parent="Theme.AppCompat.Dialog.Alert">
            <item name="android:colorBackground">@color/yellow</item>
            <item name="android:windowBackground">@color/yellow</item>
        </style>
    

    在 colors.xml 中:

    <color name="yellow">#FFFF00</color>
    

    创建以下布局 XML:

    title.xml

    <?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">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Dialog"
            android:textColor="@android:color/black"
            android:textSize="20sp" />
    </LinearLayout>
    

    dialog.xml

    <?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">
    
        Create a custom view with textview, edittext or whatever you want.
    
    </LinearLayout>
    

    您的输出将如下图所示:

    我希望这是您想要实现的,如果您有任何疑问,请告诉我。

    【讨论】:

      【解决方案3】:

      试试这个:

      AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
      builder.setTitle(R.string.scaleTitle);
      builder.setView(R.layout.scale_layout);
      builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
              dialogInterface.dismiss();
          }
      });
      final AlertDialog alertDialog = builder.create();
      alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                      @Override
                      public void onShow(DialogInterface arg0) {
                      alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
                      }
      });
      alertDialog.show();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-26
        • 1970-01-01
        • 2020-01-01
        • 1970-01-01
        • 2021-12-28
        • 2021-06-27
        • 2021-08-31
        • 2013-04-27
        相关资源
        最近更新 更多