如何更改微调器文本和背景颜色?

问题描述:

我把一个微调器放在AlertDialog中,并且由于某些原因,颜色的显示方式与正常活动不同。这使我想到这个问题:如何更改微调器文本和背景颜色?

android spinner

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

我试着定义新的TextView并应用新的适配器,但只影响下拉列表的颜色。该项目被选中后,文本仍然是白色的。

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); 

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

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

这里是被选择的索引项中针对该

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的例子。你需要实现这一点,因为你确定选择哪个项目并将其设置为可绘制的项目。只需在所选的微调控件的项目上实现,并从中设置此适配器类的索引值。

这里是另一种方式也

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

+0

而已。谢谢。选定项目(selIndex)的颜色不同的部分不适用于我。它仅为第一个项目改变颜色。 – Cristiano 2013-02-16 13:18:34

把这些线在你的风格

<style name="mytheme" parent="@android:style/Theme.Dialog"> 
    <item name="android:textColor">#000000</item> 
</style> 

,然后在乌拉圭回合清单文件包括这种风格 像下面

<activity 
     android:name="com.agencymodel.views.TempOrderActivity"   
     android:theme="@style/mytheme" > 
    </activity>