2013-12-11 156 views
3
<Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/editText1F" 
    android:layout_alignTop="@+id/txtLabel1F" 
    android:entries="@array/cat_array" 
    android:prompt="@string/cat_promt" 
    android:textColor="#ffffff"        
    /> 

我的背景是黑色的,顯示的項目是灰色的bg和黑色的字體顏色。但是當它被選中時,它將字體顯示爲黑色,因此不會被看到。我如何改變它的顏色?如何在微調器中設置所選的項目顏色?

回答

4

您需要創建一個自定義的微調器佈局來實現你想要的。

檢查這些問題,他們的答案,你想:

How to customize a Spinner in Android

Android: Custom Spinner Layout

的想法是你的行創建佈局,並創建一個帶有微調時,設置其代碼中的適配器。

+0

我用htt得到它p://adanware.blogspot.in/2012/03/android-custom-spinner-with-custom.html你給我的鏈接謝謝= D –

0

如果您正在使用MaterialBetterSpinner和綁定你的佈局,
試試這個,希望它可以幫助你:

public class MyAdapter extends ArrayAdapter<String> {  

    public MyAdapter(Context context, int textViewResourceId, List<String> objects) { 
     super(context, textViewResourceId, objects);   

    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     return getCustomView(position, convertView, parent); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     return getCustomView(position, convertView, parent); 
    } 

    public View getCustomView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final YourXMLBinding rowBinding = DataBindingUtil.inflate(inflater, R.layout.yourXML, parent,false); 
     rowBinding.tv1.setText(mMy.getValues().get(position)); 
     if(position == mMy.getCurrentIndex()) { 
      rowBinding.tv1.setTypeface(Typer.set(getContext()).getFont(Font.ROBOTO_BOLD));//change font 
      rowBinding.tv1.setTextColor(ContextCompat.getColor(getContext(), R.color.yourColor));//change color 
     } 
     return rowBinding.getRoot(); 
    } 
} 

你的XML應該是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<layout> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@color/colorBackgroundStart"> 
     <TextView 
      android:id="@+id/tv1" 
      android:layout_width="0dp" 
      android:layout_weight="0.7" 
      android:layout_height="30dp" 
      android:textColor="#fff" 
      android:textSize="16dp" 
      android:layout_marginTop="8dp" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="8dp"/> 
    </LinearLayout> 
</layout> 

創建一個微調這個適配器和你的XML:

final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues()); 

final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext()); 
spinner.setAdapter(adapter); 
相關問題