在api 21下应用主题到对话框时的奇怪行为 [英] weird behavior when applying theme to dialog under api 21
问题描述
我在我的应用程序中使用主题Theme.AppCompat.Light.NoActionBar
我想让我的一些对话框应用黑暗的AppCompat主题。
I am using the theme "Theme.AppCompat.Light.NoActionBar"
in my app .
I want to make some of my dialogs applying the dark AppCompat theme.
所以,我创建了对话框的样式$ /
So,i created style for the dialog
<style name="MyDialogStyle" parent="Theme.AppCompat.Dialog">
</style>
(当父级为Theme.AppCompat.Dialog.Alert时,同样的问题)
一个在没有版本的xml文件中限制
和与版本api 21约束的xml文件中相同的样式。
调用对话框我使用这个功能:
(same issue when the parent is "Theme.AppCompat.Dialog.Alert") one in xml file without version constrain and the same style in xml file with version api 21 constrain. to invoke the dialog i used this function :
public void showSimplestDialog(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyDialogStyle);
AlertDialog alertDialog = builder.setTitle("title")
.setMessage("message ")
.create();
alertDialog.show();
}
结果api 21+看起来很好
the result in api 21+ look fine
但是在api 17中我有一些重复的背景,我无法摆脱即使我尝试使用builder.setView(MyView)将自定义视图应用于对话框
but in api 17 i got some duplicate background that i cant get rid off (even when i try to apply custom view to the dialog with builder.setView(MyView)
推荐答案
确保你必须 import android.support.v7.app.AlertDialog
这个东西。
然后以这种方式创建
AlertDialog.Builder builder =
new AlertDialog.Builder(this, R.style.DialogStyle);
builder.setTitle("Title");
builder.setMessage("Abc ...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
并在 styles.xml中创建样式
/ p>
and Create style in styles.xml
<style name="DialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#FFCC00</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#5fa3d0</item>
</style>
这篇关于在api 21下应用主题到对话框时的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!