簡體   English   中英

如何更改微調框的文字和背景顏色?

[英]How to change spinner text and background color?

我將微調器放在AlertDialog ,由於某種原因,其顏色顯示與正常活動中的顯示有所不同。 這給我帶來了這個問題:

Android微調器

當我在正常活動中使用該微調器時,文本顏色為黑色,下拉列表的背景顏色為灰色。 相反,下拉列表的背景顏色為黑色,文本顏色為白色。 那也可以,但是問題是,正如您在該圖像上看到的那樣,白色文本在該灰色背景上幾乎不可見。

我試圖定義新的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);

如果我在活動中使用的布局中放置了一個微調框,那么我想要的就是一樣的外觀。

因為這是因為您為應用程序設置了主題。 您需要為此實現Custom適配器類並實現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

將這些線條放在您的風格中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM