【问题标题】:How to change the background color of a text in a spinner item?如何更改微调器项目中文本的背景颜色?
【发布时间】:2010-12-23 15:02:15
【问题描述】:

如何更改微调器选项项的背景颜色?我不想改变整个背景颜色,所以我不能在布局中定义它。

【问题讨论】:

    标签: java android android-layout android-widget


    【解决方案1】:
    String[] strings = {"Somelist", "SOmelist"};
    
    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
            spinner.setAdapter(new MyAdapter(this, R.layout.row, strings));
    
    ....    
    
    public class MyAdapter extends ArrayAdapter<String> {
    
                public MyAdapter(Context context, int textViewResourceId,
                        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 = getLayoutInflater();
    
                    View row = inflater.inflate(R.layout.row, parent, false);
    
                    TextView label = (TextView) row.findViewById(R.id.company);
    
                    label.setText(strings[position]);
    
                    TextView sub = (TextView) row.findViewById(R.id.sub);
    
                    sub.setText(subs[position]);
    
    ///You can have different combinations on which items in the list you want to change background, text or anything else
    
                    if (position % 2 == 0) {
                        label.setTextColor(Color.BLUE);
                        row.setBackgroundColor(Color.RED);
                    }
    
                    return row;
    
                }
    
            }
    

    布局:

    <TextView
        android:id="@+id/company"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="2dip"
        android:padding="3dip"
        android:text="CoderzHeaven"
        android:textStyle="bold" />
    
    <TextView
        android:id="@+id/sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/company"
        android:layout_marginLeft="5dip"
        android:padding="2dip"
        android:text="Heaven of all working codes" />
    

    【讨论】:

      猜你喜欢
      • 2018-04-19
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      相关资源
      最近更新 更多