4

我正在使用自定义样式在 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>

这很好用(如果我取消注释typefaceand textSize,我会看到更改)。但是,我还想将当前所选项目的浅蓝色更改为其他颜色:

在此处输入图像描述

这可以通过我的CustomAlertDialog吗?如果是这样,怎么做?

4

3 回答 3

5

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

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

对于 API 级别 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();

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>

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)直接使用,但此时我的时间有点短,我现在不想在这方面投入太多时间。

于 2013-07-23T20:47:06.957 回答
0
 <item android:state_checked="true" android:drawable="@color/checkbox_active" />

应该处理任何按下的单选按钮。

于 2013-07-09T14:50:10.957 回答
0

我没有尝试过,这似乎存在于 Theme.Holo 文档中,可能在 Theme.Dialog 中,但你可以尝试添加这个

<item name="colorPressedHighlight">@color/yourcolour</item>
<item name="colorLongPressedHighlight">@color/yourcolour2</item>
于 2013-07-20T18:30:35.143 回答