使用自定义窗口背景保留AlertDialog默认布局参数 [英] Keep AlertDialog default layout params using custom window background
问题描述
我需要一个具有自定义背景色和圆角的对话框.如何使用AlertDialog的自定义窗口背景保留默认布局参数?
I need a dialog with custom background color and rounded corners. How can I keep default layout params using custom window background for AlertDialog?
当前,我的对话框的左右边距比常规对话框小.此外,诸如正键和中性键之类的对话框按钮从对话框边缘移出的距离也更大.
Currently, my dialog has smaller left-right margin than in regular dialog. Also, dialog buttons such as positive and neutral have more shift from the edge of dialog.
<style name="DialogStuffTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@drawable/bg_custom</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:gravity">center</item>
</style>
bg_custom.xml:
bg_custom.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="270"
android:startColor="@color/Cyan200"
android:endColor="@color/Green200"
android:type="linear" />
<corners
android:radius="@dimen/dialog_corner_radius" />
</shape>
</item>
</selector>
推荐答案
在创建AlertDialog时应使用它:new AlertDialog.Builder(context, R.style.MyDialog)
Should use this when creating AlertDialog: new AlertDialog.Builder(context, R.style.MyDialog)
<style name="DialogBarButton" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">@color/button_bar_enabled_selector</item>
</style>
<style name="DialogBase" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowMinWidthMajor">@dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">@dimen/dialog_min_width_minor</item>
<item name="android:windowAnimationStyle">@style/Animation.AppCompat.Dialog</item>
<item name="android:buttonBarButtonStyle">@style/DialogBarButton</item>
</style>
<style name="MyDialog" parent="DialogBase">
<item name="android:windowBackground">@drawable/bg_custom</item>
</style>
values/dimens.xml:
values/dimens.xml:
<dimen name="dialog_min_width_major">65%</dimen>
<dimen name="dialog_min_width_minor">85%</dimen>
color/button_bar_enabled_selector.xml:
color/button_bar_enabled_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorAccent" android:state_enabled="true" />
<item android:color="@color/TextDisabled"/>
</selector>
我不知道为什么在这种情况下对话框栏按钮的颜色已更改.要解决此问题,我必须对选择器使用启用状态和禁用状态的颜色.
I don't know why color of dialog bar buttons had changed in this case. To fix this issue I have to use selector with colors for enabled and disabled states.
这篇关于使用自定义窗口背景保留AlertDialog默认布局参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!