如何更改微调文字和背景颜色? [英] How to change spinner text and background color?

查看:234
本文介绍了如何更改微调文字和背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把 AlertDialog 旋转器和一些原因的颜色有不同的显示比正常活动。这使我想到这个问题:

I put a spinner in AlertDialog and from some reason colors there are displayed differently than in normal activity. That brings me to this problem:

当我有微调的正常活动,文本颜色为黑色和下拉列表的背景颜色为灰色。这里是相反的,的下拉列表的背景颜色是黑色和文本颜色为白色。这也将是确定的,但问题是,你可以在图片上看到的,说白了文字上灰色背景几乎看不见。

When I have that spinner in normal activity, text color is black and background color of dropdown list is gray. Here is the opposite, background color of dropdown list is black and text color is white. That would also be OK but the problem is, as you can see on that image, that white text is almost invisible on that gray background.

我试图定义新的TextView和应用新的适配器,但影响的下拉列表中唯一的颜色。该项目被选中后,文字仍然是白色的。

I tried to define new TextView and apply new adapter but that affects only color of the dropdown list. After the item is selected, text is still white.

spinner_text.xml

spinner_text.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView  
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"
  android:gravity="left"  
  android:textColor="@android:color/black"        
/>

适配器

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_text, values);
spinner.setAdapter(adapter);

我要的是同一个样子这将是,如果我已经把布局中的微调这是由活动。

All I want is the same look like it would be if I have put a spinner in layout which is used by activity.

推荐答案

由于这一点,因为你设置的主题为您的应用程序。您需要实现您的自定义适配器类,并实现SpinnerAdapter这一点。

As this because of you set the theme for your application. You need to implement your Custom adapter class and implement SpinnerAdapter for this.

下面是这个例子的这个

public class CusSpinnerAdapter extends ArrayAdapter<String> 
    implements SpinnerAdapter{
    private LayoutInflater inflate;
    private int resourceId;
    private String[] options;
    private int selIndex;
    private Context context;

    public CusSpinnerAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.inflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.resourceId = textViewResourceId;
        this.options = objects;
    }
    public void setSelectedIndex(int selIndex){
        this.selIndex = selIndex;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = inflate.inflate(resourceId, null);
            Holder holder = new Holder();
            holder.textView = (TextView)convertView.findViewById(R.id.spinner_item);
            convertView.setTag(holder);
        }
        Holder holder = (Holder)convertView.getTag();
        holder.textView.setText(options[position]);

        return convertView;
    }
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = inflate.inflate(resourceId, null);
            Holder holder = new Holder();
            holder.textView = (TextView)convertView.findViewById(R.id.spinner_item);
            convertView.setTag(holder);
        }
        Holder holder = (Holder)convertView.getTag();
        holder.textView.setText(options[position]);
        if(position==selIndex){
            holder.textView.setBackgroundColor(context.getResources().getColor(R.color.spinner_item_selected));
        }else
            holder.textView.setBackgroundColor(context.getResources().getColor(R.color.spinner_item_default));

        return convertView;
    }
    private class Holder{
        TextView textView;
    }
}

在此selIndex被选中的索引项。你需要实现这个,你确定哪些项目被选定和组选定的可绘制您的物品。只要落实微调控制选择的项目,并设置该指标值这个适配器类。

In this selIndex was selected index item. You need to implement this as you identify that which item was selected and set selected drawable for your item. Just implement on item selected of spinner control and from that set the index value for this adapter class.

下面是另一种方式也

http://stackoverflow.com/a/4662939/760489

这篇关于如何更改微调文字和背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆