gpt4 book ai didi

android - 在 Android 下拉导航中设置所选项目的背景颜色

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:36 24 4
gpt4 key购买 nike

我认为这是一个简单的问题,但我无法找到我的具体问题的答案。

我读过 this great article并想在股票 ActionBar 的 Android 下拉列表中设置 selected item 的背景颜色(我没有使用 Sherlock,我的目标是 ICS+),如下面的图片链接所述:

Dropdown list screenshot

到目前为止,我是这样设置主题的:

<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>

我的下拉 View 资源是:

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

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

如果可能(即不使用任何代码),我正在寻找仅 XML 的答案。谢谢!

最佳答案

显然,这很简单!

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

<?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 来编辑主题(因为我不想要单选按钮), 这样:

<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>

关于android - 在 Android 下拉导航中设置所选项目的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20846687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com