2013-12-11 102 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); 
相关问题