将相似的外观样式应用于不同 Android 版本的 AlertDialog 框
Apply similar looking styles to AlertDialog box for different Android versions
我想让我的应用程序的 AlertDialog 框看起来像 Android > 6.0.0 上的那些(如以下屏幕截图所示)对于所有较低的 android 版本直到 4.4.4 .我为对话框应用了自定义样式。
AlertDialog 的 styles.xml 样式如下:
<style
name="AlertDialogCustom" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:textColor">@color/blue</item>
<item name="android:textColorSecondary">@color/white</item>
<item name="android:textColorPrimary">@color/white</item>
<item name="android:background">@color/dark_grey</item>
<item name="android:backgroundDimAmount">0.3</item>
</style>
以下是我在 java 中应用样式的方式:
new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom))
.setMessage("Why is DialogBox Style Inconsistent through different android versions?")
// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//Do Something
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//Do Something
}
})
.show();
作为解决方法,我至少设法将深色背景上的文本颜色更改为白色,方法是:
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1)
style = android.R.style.Theme_Material_Dialog;
else
style = R.style.AlertDialogCustom;
到目前为止,我已尝试 为较低的 Android 版本制作单独的样式,并通过检测 Android 版本通过 java 应用它们。但是,我无法在 Android 6+ 的较低 android 版本上复制 AlertDialog 框的样式,直到 4.4.4。而且我无法在互联网上找到解决方案。
虽然有一个异常;如果我在 kobakei 提供的 Rate this app 上应用我的自定义样式,生成的 RateThisApp 对话框在所有 android 版本上看起来都一样。所以我相信以特定方式设计 AlertDialog 应该可以解决这个问题。
这就是我想要的 - 使 Android 4.4.4 和 5.1.0 上的 AlertDialog 框看起来像 Android 6.0.0 及更高版本中的通过使用合适的样式。为什么 textColor 和 textColorPrimary 属性没有应用到较低的 android 版本?
如果我的问题中提供的资源不足,请在评论中告诉我,我会分享更多细节。
以下是 AlertDialog 框在不同 Android 版本上应用相同样式的屏幕截图。
- Android >= 6.0.0
- Android = 5.1.0
- Android 4.4.4
为了获得更大的灵活性,您可以使用自定义布局创建自定义对话框 class。
它将使您更好地控制布局的外观。
您可以在对话框中创建非常复杂的 UI 而无需编写大量代码
您可以像这样简单地调用对话框:
CustomDialog customDialog = new CustomDialog();
customDialog.show();
- 并且您可以在对话框中管理点击侦听器和更多事件 class,而不是在 activity 中添加那些内嵌的代码并使其变得非常大且难以理解。
举个例子:
public class CustomDialog extends Dialog {
private ImageView imageView;
private Context dialogContext;
public CustomDialog (@NonNull Context context) {
super(context);
setContentView(R.layout.full_size_image_dialog); .. //your layout
dialogContext = context;
imageView = findViewById(R.id.full_size_image); //controll your views (use any view, the image view is only an example)
}
public void someMethod() {
}
}
}
我想让我的应用程序的 AlertDialog 框看起来像 Android > 6.0.0 上的那些(如以下屏幕截图所示)对于所有较低的 android 版本直到 4.4.4 .我为对话框应用了自定义样式。
AlertDialog 的 styles.xml 样式如下:
<style
name="AlertDialogCustom" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:textColor">@color/blue</item>
<item name="android:textColorSecondary">@color/white</item>
<item name="android:textColorPrimary">@color/white</item>
<item name="android:background">@color/dark_grey</item>
<item name="android:backgroundDimAmount">0.3</item>
</style>
以下是我在 java 中应用样式的方式:
new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom))
.setMessage("Why is DialogBox Style Inconsistent through different android versions?")
// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//Do Something
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//Do Something
}
})
.show();
作为解决方法,我至少设法将深色背景上的文本颜色更改为白色,方法是:
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1)
style = android.R.style.Theme_Material_Dialog;
else
style = R.style.AlertDialogCustom;
到目前为止,我已尝试 为较低的 Android 版本制作单独的样式,并通过检测 Android 版本通过 java 应用它们。但是,我无法在 Android 6+ 的较低 android 版本上复制 AlertDialog 框的样式,直到 4.4.4。而且我无法在互联网上找到解决方案。
虽然有一个异常;如果我在 kobakei 提供的 Rate this app 上应用我的自定义样式,生成的 RateThisApp 对话框在所有 android 版本上看起来都一样。所以我相信以特定方式设计 AlertDialog 应该可以解决这个问题。
这就是我想要的 - 使 Android 4.4.4 和 5.1.0 上的 AlertDialog 框看起来像 Android 6.0.0 及更高版本中的通过使用合适的样式。为什么 textColor 和 textColorPrimary 属性没有应用到较低的 android 版本?
如果我的问题中提供的资源不足,请在评论中告诉我,我会分享更多细节。
以下是 AlertDialog 框在不同 Android 版本上应用相同样式的屏幕截图。
- Android >= 6.0.0
- Android = 5.1.0
- Android 4.4.4
为了获得更大的灵活性,您可以使用自定义布局创建自定义对话框 class。 它将使您更好地控制布局的外观。
您可以在对话框中创建非常复杂的 UI 而无需编写大量代码
您可以像这样简单地调用对话框:
CustomDialog customDialog = new CustomDialog();
customDialog.show();
- 并且您可以在对话框中管理点击侦听器和更多事件 class,而不是在 activity 中添加那些内嵌的代码并使其变得非常大且难以理解。
举个例子:
public class CustomDialog extends Dialog {
private ImageView imageView;
private Context dialogContext;
public CustomDialog (@NonNull Context context) {
super(context);
setContentView(R.layout.full_size_image_dialog); .. //your layout
dialogContext = context;
imageView = findViewById(R.id.full_size_image); //controll your views (use any view, the image view is only an example)
}
public void someMethod() {
}
}
}