2013-03-05 62 views
3

我想在點擊Spinner時移除藍色(我正在測試使用Holo的應用程序)。刪除Spinner上的按下顏色

我的代碼:

ArrayAdapter<String> array_adapter = new ArrayAdapter<String> (getActivity(), 
      R.layout.spinner_item, string_array); 
    array_adapter.setDropDownViewResource(R.layout.spinner_item); 

    Spinner spinner = (Spinner) getView().findViewById(R.id.spinner); 
    spinner.setAdapter(array_adapter); 

spinner_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/spinner_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAllCaps="true" 
    android:background="@drawable/item" 
    style="@style/EquidiaTheme.MySpinner" /> 

和item.xml

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

這是行不通的。任何想法?

+0

我不知道。我希望項目在選中/按下時不顯示任何顏色。 – thomaus 2013-03-05 16:56:16

回答

0

我能夠擺脫對當我選擇它的項目藍色矩形,當我做到了這一點: 首先我聲明,我想在values.xml使用顏色:

<resources> 
    <drawable name="red_color">#ff0000</drawable> 
    <drawable name="blue_color">#0000ff</drawable> 
    <drawable name="green_color">#00ff00</drawable> 
    <drawable name="transparent_color">#00000000</drawable> 
</resources> 

比我Styles.xml

<resources> 
    <style name="Theme.Spinner" parent="android:Theme.Holo"> 
     <item name="android:attr/listChoiceBackgroundIndicator">@drawable/transparent_color</item> 
    </style> 
</resources> 
定義自定義樣式

在我的風格是隻能使用在資源定義的顏色(設置顏色,如:@android:顏色/ XXX或#XXX直接在風格上沒有工作)

畢竟我aplly了他們的活動。我使用Xamarin所以5月的代碼是:

[Activity(Label = "TestLayouts", MainLauncher = true, Icon = "@drawable/icon", Theme="@style/Theme.Spinner")] 

但是對於Android的它應該是: <activity android:theme="@style/Theme.Spinner">

我用這個答案以供參考: Default selector background in Clickable Views

也擺脫對微調藍色矩形本身使用:

<Spinner 
    android:background="@null" 

您需要使用兩者來徹底刪除藍色矩形能夠。 也可能這種風格的屬性可以幫助你:

<item name="android:attr/colorPressedHighlight">#FF0000</item> 
    <item name="android:attr/colorLongPressedHighlight">#FF0000</item> 
    <item name="android:attr/listChoiceIndicatorSingle">@drawable/red_color</item> 
0

一個沒有主題的解決方案。如果你只有幾個微調,那就很理想。

創建一個可繪製(XML)與國家(https://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

使用相同的影像按下狀況及正常

然後用它作爲背景:

mySpinner.setBackgroundResource(R.drawable.my_spinner_state_drawable) 

額外提示:

  • 您可以使用系統資源中的系統可繪製(圖像):如「@an droid:drawable/btn_dropdown_normal「。它更容易維護並提供更原生的外觀和感覺。
  • ref http://androiddrawables.com/Buttons.html