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

时间:2013-07-09 12:45:13

标签: java android user-interface 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();

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吗?如果是这样,怎么样?

3 个答案:

答案 0 :(得分:5)

好的,这是一个快速的答案,我稍后可能会对API Level 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();

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 +

我认为&#34;最安全&#34;如前所述的方法是实现你自己的项目布局,因为你可以完全控制你的项目外观,这主要是因为在平台的旧版本中他们有&#34;硬编码&#34;这些布局上的颜色和样式。

您也可以在没有Builder的情况下玩游戏并直接使用AlertDialog/Dialog(Context context, int theme),但此时我的时间有点短暂而且我现在不想花很多时间在这上面。< / p>

答案 1 :(得分:0)

 <item android:state_checked="true" android:drawable="@color/checkbox_active" />

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

答案 2 :(得分:0)

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

<item name="colorPressedHighlight">@color/yourcolour</item>
<item name="colorLongPressedHighlight">@color/yourcolour2</item>