gpt4 book ai didi

java - 更改 Android 警报对话框中当前选定项目的颜色

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:21 26 4
gpt4 key购买 nike

我正在使用自定义样式在 Android 中创建一个对话框:

new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.CustomAlertDialog))
.setSingleChoiceItems(keys, selectedIndex,
new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int index) {
// ...
dialog.dismiss();
}
})
.create()
.show();

CustomAlertDialog 看起来像这样:

<style name="CustomAlertDialog" parent="@android:style/Theme.Dialog">

<!--
<item name="android:typeface">monospace</item>
<item name="android:textSize">30sp</item>
-->
</style>

这工作正常(如果我取消注释 typefacetextSize,我会看到更改)。但是,我还想将当前选中的项目的浅蓝色更改为其他颜色:

enter image description here

这可以通过我的 CustomAlertDialog 实现吗?如果是这样,如何?

最佳答案

好的,这是一个快速的答案,我可能会在以后针对 API 级别 8+ 进行改进

http://i.imgur.com/ZDVV1WY.png

对于 API 级别 11+

他们很容易添加 alertDialogThemetextColorAlertDialogListItem 并正常工作:

new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.CustomAlertDialog))
.setSingleChoiceItems(new String[] {"One", "Two", "Three", "Four"}, 2,
new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int index) {
// ...
dialog.dismiss();
}
})
.create()
.show();

样式.xml

<style name="CustomAlertDialog">
<item name="android:alertDialogTheme">@style/CustomAlertDialogTheme</item>
</style>

<style name="CustomAlertDialogTheme">
<item name="android:textColorAlertDialogListItem">@color/some_colors</item>
</style>

some_colors.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/my_dialog_checked"/>
<item android:color="@color/my_dialog_default"/> <!-- default -->
</selector>

对于 API 级别 8+

我认为如前所述,“最安全”的方式是实现您自己的项目布局,因为您将完全控制项目外观,这主要是因为在旧版本的平台中,它们具有“硬编码”的颜色和样式在这些布局上。

你也可以不用 Builder 直接使用 AlertDialog/Dialog(Context context, int theme) 但我现在时间有点短,我没有现在不想在这上面投入很多时间。

关于java - 更改 Android 警报对话框中当前选定项目的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17548499/

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