一些你在使用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();
}
}
yourXML是這樣的:
<?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"/>
</layout>
創建與此適配器和yourXML一個微調:
final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues());
final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext());
spinner.setAdapter(adapter);
提到這個http://stackoverflow.com/questions/7584158/setting-background-color-for-spinner-item-on-selection – rajeshwaran 2013-03-22 07:14:16