I updated my phone to Android 6.0 and I have these 2 problems with dialogs:
我将手机更新到Android 6.0,我有两个对话框问题:
1)The title is shown but the messages isn't for alert dialog(SOLVED):
1)显示标题,但消息不用于警告对话框(已解决):
new AlertDialog.Builder(context).setTitle("Title").setMessage("Message");
2)Also custom dialog fragment's title is not shown(NOT SOLVED):
2)还没有显示自定义对话框片段的标题(未解决):
getDialog().setTitle("Title");
There was not such a problem in lollipop or in older versions, the problem appeared only after updating my phone to marshmallow.
在棒棒糖或旧版本中没有这样的问题,只有在将手机更新为棉花糖后才出现问题。
How to solve the problem?
如何解决问题?
11 个解决方案
#1
34
Use constructor with theme for Lollipop and newer android versions:
使用带有主题的构造函数用于Lollipop和更新的Android版本:
Dark theme
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
And for Light theme
并为Light主题
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
#2
26
I've come across a few answers that say that you should use the following in your app theme:
我遇到了一些答案,说你应该在你的应用主题中使用以下内容:
<item name="android:alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
While that is correct, it wasn't working in all places for me. Eventually, I realized that in some places I was using the regular AlertDialog builder and in other places I was using the support builder. If you are using the support AlertDialog, make sure to also include the following in your theme:
虽然这是正确的,但它并不适用于所有地方。最后,我意识到在某些地方我使用常规的AlertDialog构建器,而在其他地方我使用的是支持构建器。如果您使用支持AlertDialog,请确保在主题中包含以下内容:
<item name="alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
#3
18
Just put this in your styles file inside your Base Theme style tag and you are good to go
只需将它放在您的基本主题样式标记内的样式文件中,就可以了
<item name="android:alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
#4
8
Answer for 2)
, we need to call setStyle()
before onCreateDialog()
because theme
is used in onCreateDialog()
回答2),我们需要在onCreateDialog()之前调用setStyle(),因为在onCreateDialog()中使用了主题
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setStyle(STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog_Alert);
}
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle(R.string.dialog_title);
return dialog;
}
#5
6
i tried to fix this problem by making a style of my own.
我尝试通过制作自己的风格来解决这个问题。
your dialog object should be like this and your style pointed to this Dialog will be written below
你的对话框对象应该是这样的,你的样式指向这个对话框将写在下面
new AlertDialog.Builder(new ContextThemeWrapper(Context, R.style.Dialog))
.setTitle("Title")
.setMessage("Sample Message ...").show();
R.style.Dialog ::
<style name="Dialog">
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:textColor">@color/primary_color</item>
</style>
Colors ::
<color name="primary_text">#212121</color>
<color name="primary_color">#2196f3</color>
finally the output should be something like this
最后输出应该是这样的
Note : "android:textColorPrimary" goes for dialog setMessage and "android:textColor" goes for dialog setTitle ; i do not use setPositive and setNegative button and listener in my dialog object but you can see them in the picture if you want you can make your own dialog object.
注意:“android:textColorPrimary”用于对话框setMessage,“android:textColor”用于对话框setTitle;我不在对话框对象中使用setPositive和setNegative按钮和监听器,但如果您希望自己可以创建自己的对话框对象,则可以在图片中看到它们。
#6
1
new AlertDialog.Builder(context)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
Hope this will help you.............
希望对你有帮助.............
#7
1
I suspect that you are ending up showing white text on a white background! (Looking at the screenshot, the (i) icon is not showing up well either, suggesting that it was designed to be shown on a background other than white.
我怀疑你最终在白色背景上显示白色文字! (看截图,(i)图标也没有很好地显示,表明它被设计为显示在除白色之外的背景上。
You can use the constructor public AlertDialog.Builder (Context context, int themeResId)
to ensure you are using a specific theme to style your dialog, where the Theme_Material_Dialog
is probably what you want.
您可以使用构造函数public AlertDialog.Builder(Context context,int themeResId)来确保使用特定主题来设置对话框的样式,其中Theme_Material_Dialog可能就是您想要的。
#8
1
Maybe your main text color is the same as dialog background color, in that case in your styles.xml use (just an example):
也许你的主文本颜色与对话框背景颜色相同,在那种情况下你在styles.xml中使用(只是一个例子):
<style name="AlertDialog" parent="@android:style/Theme.Material.Light.Dialog.Alert">
<item name="android:textColor">#000000</item>
</style>
and in dialog creation:
并在对话框创建中:
new AlertDialog.Builder(
new ContextThemeWrapper(getContext(), R.style.AlertDialog))
).setMessage("test");
#9
0
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
Once check this hope this will work.......
一旦检查这个希望这将工作.......
#10
0
In my case all dialogs had the same color as fonts (title & message). What I have done to fix this state was the change of the color attribute in my theme. I copied the xml theme file into the 'res/values-v22' and set different color for marshmallow.
在我的例子中,所有对话框都与字体(标题和消息)具有相同的颜色。我修复此状态所做的是更改主题中的颜色属性。我将xml主题文件复制到'res / values-v22'中,并为marshmallow设置不同的颜色。
<resources>
<style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:textColorPrimary">@android:color/tertiary_text_dark</item>
</style>
</resources>
or only for dialogs
或仅用于对话
<resources>
<style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:alertDialogTheme">@style/LywAlertDialogStyle</item>
</style>
<style name="LywAlertDialogStyle" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">@color/lyw_secondary_text_color</item>
</style>
</resources>
#11
0
When using DialogFragment
you need to
使用DialogFragment时,您需要
-
Add style
<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog"> <item name="android:windowNoTitle">false</item> </style>
-
In your
DialogFragment
addgetTheme
在DialogFragment中添加getTheme
public class CustomDialogFragment extends DialogFragment{ public int getTheme() { return R.style.CustomDialogFragment; } . . .
添加样式
And that's it!
就是这样!
#1
34
Use constructor with theme for Lollipop and newer android versions:
使用带有主题的构造函数用于Lollipop和更新的Android版本:
Dark theme
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
And for Light theme
并为Light主题
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
#2
26
I've come across a few answers that say that you should use the following in your app theme:
我遇到了一些答案,说你应该在你的应用主题中使用以下内容:
<item name="android:alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
While that is correct, it wasn't working in all places for me. Eventually, I realized that in some places I was using the regular AlertDialog builder and in other places I was using the support builder. If you are using the support AlertDialog, make sure to also include the following in your theme:
虽然这是正确的,但它并不适用于所有地方。最后,我意识到在某些地方我使用常规的AlertDialog构建器,而在其他地方我使用的是支持构建器。如果您使用支持AlertDialog,请确保在主题中包含以下内容:
<item name="alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
#3
18
Just put this in your styles file inside your Base Theme style tag and you are good to go
只需将它放在您的基本主题样式标记内的样式文件中,就可以了
<item name="android:alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
#4
8
Answer for 2)
, we need to call setStyle()
before onCreateDialog()
because theme
is used in onCreateDialog()
回答2),我们需要在onCreateDialog()之前调用setStyle(),因为在onCreateDialog()中使用了主题
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setStyle(STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog_Alert);
}
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle(R.string.dialog_title);
return dialog;
}
#5
6
i tried to fix this problem by making a style of my own.
我尝试通过制作自己的风格来解决这个问题。
your dialog object should be like this and your style pointed to this Dialog will be written below
你的对话框对象应该是这样的,你的样式指向这个对话框将写在下面
new AlertDialog.Builder(new ContextThemeWrapper(Context, R.style.Dialog))
.setTitle("Title")
.setMessage("Sample Message ...").show();
R.style.Dialog ::
<style name="Dialog">
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:textColor">@color/primary_color</item>
</style>
Colors ::
<color name="primary_text">#212121</color>
<color name="primary_color">#2196f3</color>
finally the output should be something like this
最后输出应该是这样的
Note : "android:textColorPrimary" goes for dialog setMessage and "android:textColor" goes for dialog setTitle ; i do not use setPositive and setNegative button and listener in my dialog object but you can see them in the picture if you want you can make your own dialog object.
注意:“android:textColorPrimary”用于对话框setMessage,“android:textColor”用于对话框setTitle;我不在对话框对象中使用setPositive和setNegative按钮和监听器,但如果您希望自己可以创建自己的对话框对象,则可以在图片中看到它们。
#6
1
new AlertDialog.Builder(context)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
Hope this will help you.............
希望对你有帮助.............
#7
1
I suspect that you are ending up showing white text on a white background! (Looking at the screenshot, the (i) icon is not showing up well either, suggesting that it was designed to be shown on a background other than white.
我怀疑你最终在白色背景上显示白色文字! (看截图,(i)图标也没有很好地显示,表明它被设计为显示在除白色之外的背景上。
You can use the constructor public AlertDialog.Builder (Context context, int themeResId)
to ensure you are using a specific theme to style your dialog, where the Theme_Material_Dialog
is probably what you want.
您可以使用构造函数public AlertDialog.Builder(Context context,int themeResId)来确保使用特定主题来设置对话框的样式,其中Theme_Material_Dialog可能就是您想要的。
#8
1
Maybe your main text color is the same as dialog background color, in that case in your styles.xml use (just an example):
也许你的主文本颜色与对话框背景颜色相同,在那种情况下你在styles.xml中使用(只是一个例子):
<style name="AlertDialog" parent="@android:style/Theme.Material.Light.Dialog.Alert">
<item name="android:textColor">#000000</item>
</style>
and in dialog creation:
并在对话框创建中:
new AlertDialog.Builder(
new ContextThemeWrapper(getContext(), R.style.AlertDialog))
).setMessage("test");
#9
0
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
Once check this hope this will work.......
一旦检查这个希望这将工作.......
#10
0
In my case all dialogs had the same color as fonts (title & message). What I have done to fix this state was the change of the color attribute in my theme. I copied the xml theme file into the 'res/values-v22' and set different color for marshmallow.
在我的例子中,所有对话框都与字体(标题和消息)具有相同的颜色。我修复此状态所做的是更改主题中的颜色属性。我将xml主题文件复制到'res / values-v22'中,并为marshmallow设置不同的颜色。
<resources>
<style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:textColorPrimary">@android:color/tertiary_text_dark</item>
</style>
</resources>
or only for dialogs
或仅用于对话
<resources>
<style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:alertDialogTheme">@style/LywAlertDialogStyle</item>
</style>
<style name="LywAlertDialogStyle" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">@color/lyw_secondary_text_color</item>
</style>
</resources>
#11
0
When using DialogFragment
you need to
使用DialogFragment时,您需要
-
Add style
<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog"> <item name="android:windowNoTitle">false</item> </style>
-
In your
DialogFragment
addgetTheme
在DialogFragment中添加getTheme
public class CustomDialogFragment extends DialogFragment{ public int getTheme() { return R.style.CustomDialogFragment; } . . .
添加样式
And that's it!
就是这样!