AlertDialog更改片段中的背景颜色

时间:2018-08-08 23:55:25

标签: java android fragment alertdialog

早上好,如何在AlertDialog中更改fragment的背景?

当我单击按钮(片段->按钮->警报对话框)时,将显示我的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));  

,但背景仍然相同。

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以尝试以下带有黄色的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>

您的输出将如下图所示:

enter image description here

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

答案 2 :(得分:0)

尝试一下:

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