【问题标题】:Changing the color of a currently selected item in an Android alert-dialog更改 Android 警报对话框中当前选定项目的颜色
【发布时间】:2013-07-07 01:49:13
【问题描述】:

我正在使用自定义样式在 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,我会看到更改)。不过,我还想把当前选中项的淡蓝色改成别的颜色:

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

【问题讨论】:

  • 也许你可以在这里找到解决方案Customize dialog which has single-choice list items
  • 您是否需要通过 STYLE 修改视图,而不是通过列表适配器为列表中的每个列表项添加自定义布局?
  • @anudroid,好吧,要求是:尽可能少地编写代码。当然我可以创建自己的布局,但我更愿意使用 Android 的内置对话框(并设置它们的样式)。鉴于缺乏对这个问题的答案,我可能会求助于自己。
  • @BartKiers 您的目标 API 级别是什么?
  • @EvelioTarazona,Android 版本 2.2+。

标签: java android user-interface dialog


【解决方案1】:

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

对于 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+

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

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

【讨论】:

  • 太糟糕了,它不适用于 API 8,但仍然感谢您的回答!
【解决方案2】:
 <item android:state_checked="true" android:drawable="@color/checkbox_active" />

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

【讨论】:

  • 感谢您的回答,但将其放在我的CustomAlertDialog 样式的&lt;style&gt; ... &lt;/style&gt; 中不起作用。你以为我会把这个放在哪里?请注意,我不是直接使用单选按钮,而是使用AlertDialog
  • 嗯。你是对的,在我自己寻找它时,它似乎没有任何在线记录。您是否尝试过使用 CTRL-SPACE 自动完成您认为可能的内容?
  • 是的,在我的CustomAlertDialog 样式中尝试了各种&lt;item name="android:...Color..."&gt; 变体,不幸的是,这些都不起作用。
【解决方案3】:

这个我没试过,好像在 Theme.Holo 文档里有,可能在 Theme.Dialog 里,不过你可以试试添加这个

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

【讨论】:

    猜你喜欢
    • 2020-09-24
    • 2020-06-21
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    相关资源
    最近更新 更多