在Android警报对话框中更改当前所选项目的颜色

Bar*_*ers 3 java user-interface android dialog

我正在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();
Run Code Online (Sandbox Code Playgroud)

这里CustomAlertDialog看起来是这样的:

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

  <!--
  <item name="android:typeface">monospace</item>
  <item name="android:textSize">30sp</item>
  -->
</style>
Run Code Online (Sandbox Code Playgroud)

这工作正常(如果我取消了typefacetextSize,我看到的变化).但是,我还想将当前所选项目的浅蓝色更改为其他内容:

在此输入图像描述

这可能通过我CustomAlertDialog吗?如果是这样,怎么样?

eve*_*otc 5

好的,这是一个快速的答案,我可能会在以后关于API Level 8+进行改进

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

适用于API Level 11+

他们很容易添加alertDialogTheme并且textColorAlertDialogListItem正常工作:

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();
Run Code Online (Sandbox Code Playgroud)

styles.xml

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

<style name="CustomAlertDialogTheme">
  <item name="android:textColorAlertDialogListItem">@color/some_colors</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

适用于API等级8+

我认为前面提到的"最安全"的方法是实现你自己的项目布局,因为你可以完全控制你的项目外观,这主要是因为在旧版本的平台上,他们在这些布局上有"硬编码"的颜色和样式.

你也可以在没有直接Builder使用的情况下AlertDialog/Dialog(Context context, int theme)直接使用,但我现在有点时间,而且我现在不想在这方面投入太多时间.