gpt4 book ai didi

android - 为不同的 Android 版本将相似的外观样式应用于 AlertDialog 框

转载 作者:行者123 更新时间:2023-11-29 23:05:41 24 4
gpt4 key购买 nike

我想让我的应用程序的 AlertDialog 框看起来像 Android > 6.0.0(如以下屏幕截图所示),适用于 4.4.4 之前的所有较低 Android 版本。我为对话框应用了自定义样式。

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 应用它们。但是,我无法在 4.4.4 之前的较低版本的 Android 6+ 上复制 AlertDialog 框的样式。而且我无法在互联网上找到解决方案。

虽然有一个异常;如果我将我的自定义样式应用于 kobakei 提供的此应用评分生成的 RateThisApp 对话框在所有 Android 版本上看起来都一样。所以我相信以特定方式设计 AlertDialog 应该可以解决这个问题。

这就是我想要的 - 通过使用合适的样式,使 Android 4.4.4 和 5.1.0 上的 AlertDialog 框看起来像 Android 6.0.0 及更高版本中的一样。为什么 textColor 和 textColorPrimary 属性不能应用于较低的 android 版本?

如果我的问题中提供的资源不足,请在评论中告诉我,我会分享更多细节。

以下是相同样式的AlertDialog框在不同Android版本上的截图。

  • 安卓 >= 6.0.0

enter image description here

  • Android = 5.1.0

enter image description here

  • 安卓4.4.4

enter image description here

最佳答案

为了获得更大的灵 active ,您可以只创建具有自定义布局的自定义对话框类。它将使您更好地控制布局的外观。

  • 您无需编写大量代码即可在对话框中创建非常复杂的 UI

  • 您可以像这样简单地调用您的对话框:

CustomDialog customDialog = new CustomDialog();
customDialog.show();

  • 并且您可以在对话框类中管理点击监听器和更多事件,而不是添加那些内嵌在 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() {
}

}
}

关于android - 为不同的 Android 版本将相似的外观样式应用于 AlertDialog 框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56581167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com