我使用AlertDialog提醒用户确认删除.我检查了我的设备(Android 5.1),它显示得很好
但在另一台设备上(也运行Android 5.1),对话框错过了正面和负面按钮.
我查了一下,发现设备发生这个问题有中等分辨率(960x540,854x480).
解决方案是否涉及此问题?如果没有,你能告诉我原因以及如何解决这个问题吗?
我的显示对话框代码:
public static final Dialog yesNoDialog(Context context,
String message,
DialogInterface.OnClickListener yesAction, DialogInterface.OnClickListener noAction) {
AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.todoDialogLight);
builder.setTitle(context.getString(R.string.app_name))
.setMessage(message)
.setCancelable(false)
.setPositiveButton("YES", yesAction)
.setNegativeButton("NO", noAction);
return builder.create();
}
Run Code Online (Sandbox Code Playgroud)
和styles.xml
<style name="todoDialogLight" parent="Theme.AppCompat.Light.Dialog">
<!-- Used for the buttons -->
<item name="colorAccent">@color/colorPrimaryDark</item>
<item name="android:textStyle">bold</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">@color/colorText</item>
<!-- Used for the background -->
<!-- <item name="android:background">#4CAF50</item>-->
<item name="android:fontFamily">sans-serif</item>
<item name="android:windowAnimationStyle">@style/RemindDialogAnimation</item>
<item name="android:layout_width">@dimen/width_remind_dialog</item>
<item name="android:layout_height">wrap_content</item>
</style>
Run Code Online (Sandbox Code Playgroud)
Ali*_*Ali 38
按钮就在我身边.不幸的是,他们是白色背景上的白色文本.它与分辨率无关,但更多与您选择的主题有关.要解决此问题,您需要在对话框主题中设置正确的文本颜色.
例如,在styles.xml中添加
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">@color/colorPrimaryDarkBlue</item>
</style>
Run Code Online (Sandbox Code Playgroud)
并在您的活动中添加
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MyActivity.this, R.style.MyDialogTheme);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
@Ali 答案是正确的,但不适用于新的设计库
要将其与设计库一起使用,请使用此样式。
<style name="AlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="colorPrimary">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后你像这样使用它。
AlertDialog.Builder(it, R.style.AlertDialogTheme)
.setMessage("The message")
.setPositiveButton("Yes") { _, _ -> /* Do something*/}
.setNegativeButton("No") { _, _ -> }
.show()
Run Code Online (Sandbox Code Playgroud)
这确实与分辨率有关,我不知道确切的原因,只是做一个 if else 条件来解决这个问题。
public static String getDensity(Context context) {
float density = context.getResources().getDisplayMetrics().density;
if (density >= 4.0) {
return "xxxhdpi";
}
if (density >= 3.0) {
return "xxhdpi";
}
if (density >= 2.0) {
return "xhdpi";
}
if (density >= 1.5) {
return "hdpi";
}
if (density >= 1.0) {
return "mdpi";
}
return "ldpi";
}
Run Code Online (Sandbox Code Playgroud)
警报对话框
public static Dialog yesNoDialog(final Context context,
final String message,
final DialogInterface.OnClickListener yesAction,
final DialogInterface.OnClickListener noAction) {
int theme = PreferenceUtil.getThemeSetting(context, PreferenceUtil.PREF_THEME);
AlertDialog.Builder builder = null;
String density = AppUtil.getDensity(context);
if (theme == ThemeUtil.THEME_LIGHT) {
if(density.equals("hdpi")){
builder = new AlertDialog.Builder(context);
}else{
builder = new AlertDialog.Builder(context, R.style.todoDialogLight);
}
} else {
if(density.equals("hdpi")){
builder = new AlertDialog.Builder(context);
}else{
builder = new AlertDialog.Builder(context, R.style.todoDialogDark);
}
}
builder.setTitle(context.getString(R.string.app_name))
.setMessage(message)
.setCancelable(false)
.setPositiveButton("YES", yesAction)
.setNegativeButton("NO", noAction);
return builder.create();
}
Run Code Online (Sandbox Code Playgroud)
希望它对遇到同样问题的其他开发人员有所帮助。
| 归档时间: |
|
| 查看次数: |
14980 次 |
| 最近记录: |