2013-02-06 30 views
10

我使用的是微調,並希望添加微調 - 改變行爲取決於狀態(聚焦,按下)微調不適dropDownSelector屬性

示例項目在這裏https://github.com/vovs/spinner_issue

我的代碼:

activity_main.xml中

<Spinner 
     android:id="@+id/spinner" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="30dp" 
     android:spinnerMode="dropdown" 
     android:dropDownSelector="@drawable/spinner_state" /> 

蜘蛛r_state.xml

<?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_enabled="false" 
     android:drawable="@color/black" /> 
    <item 
     android:state_pressed="true" 
     android:state_enabled="true" 
     android:drawable="@color/red" /> 
    <item 
     android:state_focused="true" 
     android:state_enabled="true" 
     android:drawable="@color/red" /> 
    <item 
     android:state_enabled="true" 
     android:drawable="@color/gray" /> 
</selector> 

AndroidManifest:

<uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="16" /> 

所以,如果我在模擬器的Android 4.0.2 API 14上運行的應用程序,並嘗試選擇一些項目或滾動使用我的鼠標輪不任何影響,我在選擇設置(當按下或滾動 - 項目應該是紅色的,但它是藍色的 - 默認ICS顏色)。

對於Android 2.2的API 8壓或使用車輪滾動時(在這種情況下狀態是聚焦的)的顏色是黃色[橙](默認爲Android 2.2顏色)

如何啓用微調選擇?

enter image description here

回答

8

也是官方的bug ...... https://code.google.com/p/android/issues/detail?id=24922

什麼幫助:

<resources> 
    <style name="Theme.MyTheme" parent="@android:style/Theme.Holo.Light"> 
     <item name="android:dropDownListViewStyle">@style/Theme.MyListView</item> 
    </style> 

    <style name="Theme.MyListView" parent="@android:style/Widget.Holo.Light.ListView"> 
     <item name="android:listSelector">@drawable/orange_list</item> 
    </style> 
</resources> 

好運!

1

可能有其他的方法,但這裏是我使用的Generators的一個理解。

聲明自己的風格在你res/values/styles.xml指向微調到你的繪製。

<style name="myCustomSpinner" parent="android:Widget.Holo.Light.Spinner"> 
    <item name="android:background">@drawable/spinner_state</item> 
</style> 

創建res/values/themes.xml並聲明自己的主題繼承自當前主題。在這個主題,每個屬性正在修改,並從最後一步指向您的自定義樣式添加項目。我想,這可能會在風格去文件,如果你想要的,但由於發電機將它們分開我效仿。

<style name="myCustomTheme" parent="android:Theme.Light"> 
    <item name="android:dropDownSpinnerStyle">@style/myCustomSpinner</item> 
</style> 

在你AndroidManifest,添加android:theme="@style/myCustomTheme"開口application標籤。

您的parent值將取決於項目是如何建立,我想在您的項目紡織家這種風格的意願,而不僅僅是一個。試試看,讓我知道你得到了什麼。

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

<color name="lighter_gray_no_transparent">#FFDDDDDD</color> 

設置背景選擇器爲您的項目視圖,就這樣。爲我工作。顏色阿爾法值應該是FF,或者它會顯示橙色背景,左一個顏色值是#FFDDDDDD,右邊爲#55DDDDDD

enter image description here enter image description here