无法更改对话框背景?

时间:2016-12-10 06:13:33

标签: android themes android-alertdialog android-theme

我知道这很糟糕。从几个小时开始,我正在尝试更改theme AlertDialog,但不会更改<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> <!-- Customize your theme here. --> <item name="android:background">@color/primary_background</item> <item name="android:colorPrimaryDark">@color/primary_background</item> <item name="android:navigationBarColor">@color/primary_background</item> <item name="android:popupBackground">@color/primary_background</item> <item name="android:alertDialogTheme">@style/myDialog</item> </style>

父主题:

<style name="myDialog" parent="Theme.AppCompat.Dialog">
    <item name="dialogPreferredPadding">@dimen/dialog_padding</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
</style>

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.myDialog));

对话主题:

/usr/local/lib/python2.7

我无法弄清楚出了什么问题。我觉得这很容易。

3 个答案:

答案 0 :(得分:1)

予。为对话框背景声明自定义drawable background_dialog.xml。

<?xml version="1.0" encoding="utf-8"?>  
<!-- From: support/v7/appcompat/res/drawable/abc_dialog_material_background_light.xml -->  
<inset xmlns:android="http://schemas.android.com/apk/res/android"  
    android:insetLeft="16dp"
    android:insetTop="16dp"
    android:insetRight="16dp"
    android:insetBottom="16dp">

    <shape android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="@color/indigo" />
    </shape>

</inset>  

II。在styles.xml文件中声明自定义样式。

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">  
    <!--buttons color-->
    <item name="colorAccent">@color/pink</item>
    <!--title and message color-->
    <item name="android:textColorPrimary">@android:color/white</item>
    <!--dialog background-->
    <item name="android:windowBackground">@drawable/background_dialog</item>
</style>  

III。创建对话框并使用样式作为AlertDialog.Builder中的参数。

AlertDialog.Builder builder =  
        new AlertDialog.Builder(this, R.style.MyDialogTheme);
...
AlertDialog dialog = builder.create();  
// display dialog
dialog.show();  

查看此document了解详情。

您可以通过以下方式以编程方式设置对话框的自定义视图。

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();

然后,您可以获得组件的参考,如下所示

 Button btn = (Button) dialoglayout.findViewById(R.id.button_id);

答案 1 :(得分:0)

而不是xml样式,你可以简单地以编程方式实现它。

创建您必须在AlertDialog

中显示的xml文件

示例:名称为abc_dialog.xml

现在这样做

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.abc_dialog, (ViewGroup) getCurrentFocus());

并将该视图设置为AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();

答案 2 :(得分:0)

您可以在DialogFragment类中尝试以下操作。

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    return dialog;
}

@Override
public void onStart() {
    super.onStart();

    final Dialog dialog = getDialog();
    if(dialog!=null)
    {
        dialog.setContentView(R.layout.dialog_contract);
        dialog.getWindow().setLayout(1000, 1500);
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.background_my_background);
    }
}