2016-07-29 542 views
3

我正在開發一個android應用程序,並在我的應用程序的許多地方使用Spinner。 我想要的是更改所選項目的背景顏色,以便人們可以輕鬆識別當前選定的項目。更改Android Spinner中選定項目的背景顏色

我已經檢查了這個鏈接Setting background color for Spinner Item on selection,但是這樣做會改變選定的textview背景顏色,但是不會在下拉列表中改變它的顏色,並且當我將看到下拉列表時,我想更改所選textview的背景顏色。

我想更改列表中選定項目的顏色而不是微調,請參閱下圖。

enter image description here 我該怎麼做?請,有人可以幫我嗎?

非常感謝先進的。

+0

你需要實現它的getDropDownView()方法,做到這一點,如果不能夠再在這裏發表您的代碼 – Vickyexpert

+0

在旋轉的適配器的onclick應用,併爲特定項目的新的背景顏色。 –

+0

@Vickyexpert:你能給我一個想法,我怎樣才能使用getDropDownView()方法來實現這一點。這將是一個很大的幫助。先進的感謝很多。 –

回答

10

您需要在適配器類中實現以下方法:

它會幫助你:

int selectedItem = -1; 

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list) { 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) 
    { 
     View v = null; 
     v = super.getDropDownView(position, null, parent); 
     // If this is the selected item position 
     if (position == selectedItem) { 
      v.setBackgroundColor(Color.BLUE); 
     } 
     else { 
      // for other views 
      v.setBackgroundColor(Color.WHITE); 

     } 
     return v; 
    } 
}; 

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
mySpinner.setAdapter(dataAdapter); 

現在在微調選擇的項目放

selectedItem = position; 
+1

感謝這段代碼:)但是這是改變了第0個位置項目的背景顏色,而不是這個項目的背景顏色選擇在微調。 我該怎麼做,並且在覆蓋getDropDownView方法後,下拉圖標也不可見。 –

+0

你需要通過有selectedItemPosition而不是0, – Vickyexpert

+0

它現在工作正常,由於一噸.. :) :) –

0
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="colorControlNormal">@color/spinner_background</item> 

</style> 

定義顏色的文件夾Spinner_background顏色..

+0

對'colorControlNormal'無效。 – Davidea

0

嘗試建立在繪製一個選擇,像,

<?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/colorPrimary" /> 
<item android:drawable="@android:color/transparent" /> 
</selector> 

,並設置微調背景

android:background="@drawable/spinner_selector" 
+0

這個變化對背景沒有任何影響,它使右側的迷你三角形消失... – Davidea

0

我在網上搜索,爲妥善解決要做到這一點下面沒有硬編碼的背景在java代碼中的行爲。 您可以使用drawable來實現這一點(設置選定的項目背景顏色)。

您需要做什麼將dropdownViewResource設置爲自定義佈局。這佈局應該是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/spinner_item_background" 
    android:gravity="left" 
    android:padding="8dp" /> 

spinner_item_background.xml,您可以定義爲每個項目狀態的背景。例如,如果你想選擇的時候對新聞界產生連鎖反應,但堅實的效果,你可以試試這個:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Activated state is the selected item --> 
    <item android:state_activated="true" android:drawable="#00ff00"/> 
    <!-- Pressed state is the one the user is interacting with --> 
    <item android:state_pressed="true" android:drawable="#00ff00"/> 
    <!-- The rest of the items --> 
    <item android:drawable="#ffffff"/> 
</selector> 
+0

我試過這個,但各州不能正常工作。我使用:// ArrayAdapter <的CharSequence>適配器= ArrayAdapter.createFromResource(this.getContext(), R.array.task_status,R.layout.spinner_item_when_closed); // adapter.setDropDownViewResource(R.layout.spinner_item_when_open); //使用像「spinner_item_background.xml」這樣的代碼在文件「res \ layout \ spinner_item_when_open.xml」中設置背景 – educoutinho

+0

我不知道這是否能解決問題,但background-xml需要在drawable '文件夾。 –

1

這裏是通過XML解決方案:

微調的樣子:

<Spinner 
     android:id="@+id/settingsSleepingTimePicker" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/spinner_main_button" 
     android:popupBackground="@color/colorPrimary" 
     android:textColor="@android:color/white" 
     android:textSize="20sp"/> 

在創建微調器設置setDropDownViewResource自定義佈局:

adapter.setDropDownViewResource(R.layout.spinner_item); 

和SPINN er_item。XML是什麼樣子:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/spinner" 
    android:textColor="#ffffff" 
    android:textSize="20sp" /> 

最後,我們設置@繪製/微調這樣的:

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

希望我的回答將是有益的!

相關問題