Android自定义IcsSpinner(来自ActionBarSherlock)

时间:2013-10-01 02:12:56

标签: android android-listview actionbarsherlock android-spinner android-theme

目前我正在使用IcsSpinner,因为我想在所有设备上使用下拉式微调器。 这就是我想要实现的目标(显示封闭的微调器并扩展):

enter image description here

然而,我无法这样做。我试过这段代码:

<com.actionbarsherlock.internal.widget.IcsSpinner
     android:id="@+id/myspinner"
     style="@style/SpinnerStyle"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:drawSelectorOnTop="true"
     android:minHeight="@dimen/spinner_min_height"
     android:prompt="@string/pick_one" />

风格:

<style name="SpinnerStyle" parent="Sherlock.__Widget.Holo.Spinner">
    <item name="android:popupBackground">@drawable/spinner_selector</item>
    <item name="android:dropDownSelector">@color/white_text</item>
</style>

和drawable spinner_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:state_pressed="true" android:drawable="@color/red" />
   <item android:state_focused="true" android:drawable="@color/red"/>
   <item android:state_selected="true" android:drawable="@color/red"/>
   <item android:drawable="@color/spinner_grey_bg"/>
</selector>

然而输出是这样的:

enter image description here

我尝试了另一种方法,通过这样做来自定义适配器中的视图:

ArrayAdapter<String> myAdapter= new ArrayAdapter<String>(mContext,
                        R.layout.spinner_entry_textview, adapterArray);
mySpinner.setAdapter(myAdapter);

使用 spinner_entry.textview.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_spinner_entry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/min_height_touch"
android:background="@drawable/spinner_selector"
android:textColor="@drawable/spinner_text_selector"
android:gravity="right|center_vertical"
/>

spinner_text_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="@color/white_text" />
<item android:state_focused="true" android:color="@color/white_text"/>
<item android:state_selected="true" android:color="@color/white_text"/>
<item android:color="@color/red_text"/>
</selector>

但我得到这个输出几乎是我想要的,但它也在关闭时绘制灰色和红色文本。 (箭头指向的第一个元素应为带有白色文本的深灰色背景)enter image description here

还尝试将样式应用于默认列表视图,因为微调器似乎有一个内部listView但没有做任何事情:

 <style name="AppTheme" parent="Theme.Sherlock.Light">
    <item name="android:listViewStyle">@style/MyListView</item>
    <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
    <item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItem</item>
    <item name="spinnerItemStyle">@style/SpinnerItem</item>
    <item name="spinnerDropDownItemStyle">@style/SpinnerDropDownItem</item>
</style>

<style name="MyListView" parent="@android:style/Widget.ListView">
    <item name="android:textColor">@drawable/spinner_text_selector</item>
    <item name="android:background">@drawable/spinner_selector</item>
</style>

<style name="SpinnerDropDownItem">
    <item name="android:textColor">@drawable/spinner_text_selector</item>
    <item name="android:background">@drawable/spinner_selector</item>
</style>

<style name="SpinnerStyle" parent="Sherlock.__Widget.Holo.Spinner">
    <item name="android:popupBackground">@drawable/spinner_selector</item>
    <item name="android:dropDownSelector">@color/white_text</item>
</style>

关于如何实现这一目标的任何想法?非常感谢。

0 个答案:

没有答案