2014-07-10 116 views
0

由於某種原因,我的微調框中所選項目的文本顏色爲白色。下面是該活動的代碼:更改微調框中所選項目的顏色

spinner = (Spinner) findViewById(R.id.spinner_categories); 
arr_list_categories = manager.get_all_categories(); 
arr_list_categories_names = new ArrayList<String>(); 
// Loop through the Categories array 
for (int i = 0; i < arr_list_categories.size(); i++) 
{ 
    Category curr_category = arr_list_categories.get(i); // Get the Category object at current position 
    arr_list_categories_names.add(curr_category.getCategory_name() 
      .toString()); // Add the name of the Category to the Names Array   
} 
// Setting the Adapter, to display retrieved data in the Spinner 
adapter=new ArrayAdapter<String> (this, R.layout.simple_spinner_item, arr_list_categories_names); 
// Setting the display source for the Spinner View 
adapter.setDropDownViewResource(R.layout.simple_spinner_item); 
// Populating the Spinner 
spinner.setAdapter(adapter); 
// Registering for On Item Selected listener 
spinner.setOnItemSelectedListener(spinnerListener); 

而這裏的simple_spinner_item的XML:

<?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:gravity="left" 
    android:padding="5dip" 
    android:textColor="#FF0000" 
    android:textSize="20sp" /> 

下拉項顯示爲紅色,而不是選定的項目。我試着在XML來改變文字顏色來選擇:android:textColor="@drawable/spinner_text_color_selector"並創造了這個選擇:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:color="#FF0000"/><!-- pressed --> 
    <item android:state_focused="true" android:color="#FF0000"/><!-- focused --> 
    <item android:state_selected="true" android:color="#FF0000"/> 
    <item android:state_activated="true" android:color="#FF0000"/> 
    <item android:color="#FF0000"/><!-- default --> 
</selector> 

但所選項目的文本顏色仍然是白色的。於是,我就這樣做在OnItemSelectedListener:

TextView selected_tv = (TextView) parent.getChildAt(0); 
selected_tv.setTextColor(Color.BLUE); 

尚白...

爲什麼會發生,如何解決呢?謝謝!

+0

我有同樣的疑難問題只是改變ArrayAdapter (this)來ArrayAdapter (classname.this) .dont知道你的工作是否 – Meghna

+0

我試過你的代碼..它的工作正常..顯示所選文本的紅色。 –

+0

@Sania試了一下,但它仍然顯示我的文字白色... – Igal

回答

0
TextView selected_tv = (TextView) parent.getChildAt(0); 
selected_tv.setTextColor(Color.BLUE); 

在這段代碼中,像這樣的(使用的TextView的id)第一線的變化:

TextView selected_tv = (TextView) parent.findViewById(R.id.selected_tv); 
selected_tv.setTextColor(Color.BLUE); 
相關問題