在Android下拉菜单上设置所选项目的背景颜色

问题描述:

我认为这是一个简单的问题,但是我无法找到针对我特定问题的答案.

I think this is an easy question, but I haven't been able to find an answer to my specific problem.

我已阅读这篇很棒的文章并希望在普通ActionBar上的Android Dropdown列表中设置所选项目的背景颜色(我没有使用Sherlock,而是以ICS +为目标),如下面的图片链接中所述:

I've read this great article and would like to set the background color of the selected item on the Android Dropdown list on the stock ActionBar (I'm not using Sherlock and I'm targetting ICS+), as explained in the image link below:

到目前为止,我已经以这种方式设置了主题:

So far I've set my theme in this way:

<resources>
  <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
    <item name="android:dropDownListViewStyle">@style/MyDropDownListView</item>        
  </style>
  <style name="MyDropDownListView" parent="android:style/Widget.Holo.ListView.DropDown">
    <item name="android:listSelector">@drawable/ad_selectable_background</item>
  </style>
</resources>

这是我的 ad_selectable_background.xml :

<selector xmlns:android="http://schemas.android.com/apk/res/android"
              android:exitFadeDuration="@android:integer/config_mediumAnimTime" >
    <item android:state_pressed="true" android:drawable="@android:color/holo_blue_light" />
    <item android:drawable="@android:color/transparent" />
</selector>

我的下拉视图资源是:

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

已按下状态有效,但是当您打开菜单时,不会显示任何选择. 我想使用选择器将其设置为 holo_blue_light ,但是我无法找到正确的语法.

The pressed state works, but when you open the menu no selection is displayed. I would like to set it to holo_blue_light using the selector, but I'm not able to find the correct syntax using XML.

我正在寻找仅在可能的情况下(即不使用任何代码)的XML答案.谢谢!

I'm looking for an XML only answer if it's possible (i.e. not using any code). Thanks!

显然,这很简单!

我必须编辑 ad_selectable_background.xml 才能使用已选中状态(就像我之前所做的那样,但是我缺少下面的主题):

I had to edit ad_selectable_background.xml to use the checked state (as I did before, but I was missing the theme below):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
              android:exitFadeDuration="@android:integer/config_mediumAnimTime" >
    <item android:state_pressed="false" android:state_checked="true" android:drawable="@android:color/holo_blue_light" />
    <item android:state_pressed="true" android:drawable="@android:color/holo_blue_light" />
    <item android:drawable="@android:color/transparent" />
</selector>

然后通过添加 android:spinnerDropDownItemStyle 项并禁用 checkMark 来编辑主题(因为我不想使用单选按钮),以这种方式:

And then edit the theme by adding the android:spinnerDropDownItemStyle item and disabiling the checkMark (as I didn't want the radio buttons), in this way:

<resources>
  <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
    <item name="android:dropDownListViewStyle">@style/MyDropDownListView</item>
    <item name="android:spinnerDropDownItemStyle">@style/MySpinnerDropDownItem</item>
  </style>
  <style name="MyDropDownListView" parent="android:style/Widget.Holo.ListView.DropDown">
    <item name="android:listSelector">@drawable/ad_selectable_background</item>
  </style>
  <style name="MySpinnerDropDownItem" parent="android:style/Widget.DropDownItem.Spinner">
    <item name="android:background">@drawable/ad_selectable_background</item>
    <item name="android:checkMark">@null</item>
  </style>
</resources>