Set selected item background color on Android drop

2019-03-31 20:42发布

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

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:

Dropdown list screenshot

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>

And this is my 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>

My dropdown view resource is:

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

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.

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

1条回答
【Aperson】
2楼-- · 2019-03-31 20:52

Apparently, it was quite simple!

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>

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>
查看更多
登录 后发表回答