5
我在我的代碼中使用了Spinner控件。我希望項目在選擇時突出顯示(即,該項目的背景顏色已更改)。這怎麼能實現?爲選定的旋轉器項目設置背景顏色
我在我的代碼中使用了Spinner控件。我希望項目在選擇時突出顯示(即,該項目的背景顏色已更改)。這怎麼能實現?爲選定的旋轉器項目設置背景顏色
創建一個XML:爲前:mybg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@color/anyColor" />
<item android:drawable="@android:color/transparent" />
</selector>
,並在活動XML
做
<Spinner...............
android:drawSelectorOnTop="true"
android:background="@drawable/mybg"/>
選擇:custom_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@color/light_grey" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@color/light_grey" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@color/light_grey" />
<item android:state_selected="true" android:drawable="@color/light_grey"/>
<item android:drawable="@color/white" />
</selector>
自定義視圖佈局:my_simple_item
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:padding="5dip"
android:background="@drawable/custom_selector"/>
初始化微調:
String[] items = new String[] {"One", "Two", "Three"};
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_simple_item, items);
希望這幫助
使用鼠標滾輪滾動時沒有任何效果。項目應該是light_grey,但相反,他們是橙色(Android 2.2的默認顏色) 看到我的問題http://stackoverflow.com/questions/14737811/spinner-does-not-apply-dropdownselector-attribute – vsvydenko