更改当前选定项的颜色在Android警报的对话框 [英] Changing the color of a currently selected item in an Android alert-dialog

查看:248
本文介绍了更改当前选定项的颜色在Android警报的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建Android中一个对话框,自定义样式:

I'm creating a dialog in Android with a custom style:

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>

这工作正常(如果我取消注释字体 TEXTSIZE ,我看到的变化)。不过,我也想改变当前所选项目的浅蓝色成别的东西:

This works fine (if I uncomment the typeface and textSize, I see the changes). However, I would also like to change the light-blue color of the currently selected item into something else:

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

Is this possible through my CustomAlertDialog? If so, how?

推荐答案

好吧,这里是一个快速的回答,我可能会提高它以后就API级别8 +

Alright here is a quick answer, I might improve it later regarding API Level 8+

有关API级别11 +

For API Level 11+

这是他们增加了 alertDialogTheme textColorAlertDialogListItem 和公正的工作更轻松:

It is easy they added alertDialogTheme and textColorAlertDialogListItem and just work:

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 +

For API Level 8+

我认为最安全的方式去如前面提到的正在实施自己的项目布局,你将拥有完全控制您的物品的外观,这主要是因为在旧版本的平台,他们有硬codeD颜色和款式上的布局。

I think the "safest" way to go as mentioned before is implementing your own item layout as you will have total control over your items appearance this is mainly because in old versions of the platform they had "hard coded" colors and styles on those layouts.

您也可以发挥无生成器和使用 AlertDialog /对话框(上下文的背景下,诠释主题)直接,但我我一时间有点短,在这一点上,我不想投入太多的时间在这个现在。

You could also play without Builder and use AlertDialog/Dialog(Context context, int theme) directly but I am a bit short of time at this point and I didn't want to invest much time on this right now.

这篇关于更改当前选定项的颜色在Android警报的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆