2014-11-24 48 views
0

我正在使用演示應用程序,其中我使用的是Spinner並且具有黑色背景色,所以微調框選擇的項目顏色也默認爲黑色,因此它不可見。如何在Android中將項目文本顏色設置爲微調框的白色

我希望通過xml或更新樣式將此顏色更改爲白色,以便它可以通過黑色顯示。

<Spinner 
    android:id="@+id/countrySpinner" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="15dp" 
    android:hint="@string/country" /> 

enter image description here

沒有辦法做到這一點?提前致謝。

+0

與父母=定式「@安卓風格/ Widget.Holo.Dark.ProgressBar」 – CSmith 2014-11-24 20:04:30

+0

你是不是想使文本白色只對選定的項目? – NSouth 2014-11-24 20:06:41

+0

@NSouth是的...... – 2014-11-24 20:07:51

回答

3

嘗試在OnItemSelectedListener中處理此操作。我認爲以下應該工作。它採用所選項目的視圖,在其中查找TextView(Spinner視圖具有帶有text1標識的TextView子項),並設置其顏色。

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

     public void onItemSelected(AdapterView<?> arg0, View view, 
       int position, long id) { 

      TextView tmpView = (TextView) mySpinner.getSelectedView().findViewById(android.R.id.text1); 
      tmpView.setTextColor(Color.WHITE); 
     } 

     public void onNothingSelected(AdapterView<?> arg0) { 
      // do stuff 

     } 
    }); 
+0

它的工作謝謝你+1 :) – 2014-11-24 20:24:52

+0

高興地幫助! – NSouth 2014-11-24 20:26:55

0

設置的樣式在您的RES /價值/ styles.xml:

<style name="mySpinner" parent="@android:style/Widget.Holo.Dark.ProgressBar" /> 

和參考這種風格上的微調:

<Spinner style="@style/mySpinner" ... /> 

父樣式,Widget.Holo.Dark.ProgressBar應該給你一個適合黑暗背景的旋鈕。

有關Styles的信息,請參閱https://developer.android.com/guide/topics/resources/style-resource.html。您可以在SDK res /文件夾中嗅探以瞭解內置樣式。

+0

我正在使用支持庫appcompat。現在我越來越不能解決@android:style/Widget.Holo.Dark.ProgressBar – 2014-11-24 20:10:31

+0

請注意,我不想像我在問題中添加的那樣。我只想將選定的文字顏色變成白色。 – 2014-11-24 20:11:30

相關問題