如何更改Spinner字体颜色?

22

我遇到了Droid X手机的问题,用户反映在下拉列表中字体颜色变成了白色,除非用户选中其中一个选项否则看不到。其他手机貌似没有这个问题。我想尝试将字体强制设置为黑色来解决这个问题。该怎么做?

以下是我目前用于填充下拉列表的代码。在Droid X上似乎simple_spinner_item有问题。

String spin_arry[] = new String[str_vec.size()];
str_vec.copyInto(spin_arry);
ArrayAdapter adapter =
    new ArrayAdapter(this,android.R.layout.simple_spinner_item, spin_arry);
8个回答

46

我将使用Android SDK中的Spinner项目示例作为下一个代码示例。


代码:

首先,您需要创建自定义适配器来拦截下拉列表中视图的创建:

static class CustomArrayAdapter<T> extends ArrayAdapter<T>
{
    public CustomArrayAdapter(Context ctx, T [] objects)
    {
        super(ctx, android.R.layout.simple_spinner_item, objects);
    }

    //other constructors

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
        View view = super.getView(position, convertView, parent);

        //we know that simple_spinner_item has android.R.id.text1 TextView:         

        /* if(isDroidX) {*/
            TextView text = (TextView)view.findViewById(android.R.id.text1);
            text.setTextColor(Color.RED);//choose your color :)         
        /*}*/

        return view;

    }
}

然后您可以在代码中创建适配器,例如:
 String [] spin_arry = getResources().getStringArray(R.array.Planets);        
 this.mAdapter = new CustomArrayAdapter<CharSequence>(this, spin_arry);

解释:

由于 CustomArrayAdapter 知道我们使用的是 Android 内置的布局资源,它也知道文本将被放置在 ID 为 android.R.id.text1TextView 中。这就是为什么它可以拦截下拉列表中视图的创建并将文本颜色更改为所需的任何颜色。


屏幕截图:

enter image description here


更好的解决方案,修改此代码:https://dev59.com/M2kv5IYBdhLWcg3wtS8N - Gerry

7

Simple and Crisp ...

private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
  public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    ((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
    ((TextView) parent.getChildAt(0)).setTextSize(5);
  }

  public void onNothingSelected(AdapterView<?> parent) {
  }
};

这会导致在旋转设备时TextView为空。 - tolgap

7

请编写一个R.layout.simplespinneritem布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

ID是android:id="@android:id/text1",设置字体和背景颜色。
ArrayAdapter adapter =
  new ArrayAdapter(this,packagename.R.layout.simple_spinner_item, spin_arry);

1
如果Spinner的项目是使用“entries”属性从<string-array>加载的,那么您该如何使用它? - TheRealChx101

3
public class ee extends Activity{
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ww);
addListenerOnSpinnerItemSelection();

}
public void addListenerOnSpinnerItemSelection(){

    ArrayList<String> array = new ArrayList<String>();
    array.add("item0");
    Spinner spinner1;
    ArrayAdapter<String> mAdapter;
    spinner1= (Spinner) findViewById(R.id.spinner2);
    spinner1= new ArrayAdapter<String>(this, R.layout.spinner_item, array);
    spinner1.setAdapter(mAdapter);

}  
}

在xml的res/layout中添加一个新的xml文件:类型为布局,名称为spinner。(在spinner_item.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="fill_parent"
    android:gravity="top"
    android:singleLine="true"
    android:textColor="#00f0ff" />

2

在sasad的回复中,增加一条建议,即在Android文件夹中找到该文件的副本,在项目中更改该文件中TextView的文本颜色,并在初始化适配器时使用该布局,而不是使用Android默认的。


0
你也可以尝试这种方法,其中你需要添加两个新的布局资源文件:
  1. Custom_spinner_list_item
  2. Custom_spinner_dropdown_item
然后在代码中使用它们。
String spin_arry[] = new String[str_vec.size()];
str_vec.copyInto(spin_arry);
ArrayAdapter adapter =
    new ArrayAdapter(this,R.layout.custom_simple_spinner_item, spin_arry);
adapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);

custom_spinner_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    style="?attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:fontFamily="@font/roman"
    android:singleLine="true"
    android:textAlignment="inherit"
    android:textColor="@color/black"
    android:textSize="14sp">

</TextView>

custom_spinner_dropdown_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    style="?attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="?attr/dropdownListPreferredItemHeight"
    android:ellipsize="marquee"
    android:fontFamily="@font/roman"
    android:singleLine="true"
    android:textAlignment="textStart"
    android:textColor="@color/black"
    android:textSize="14sp">

</TextView>

编程愉快!!:)


-1
创建自己的布局XML文件,并为黑色文本添加android:textColor="#000"。

-1

这里有更合适的方法,伙计们:

首先,在您的系统中找到“simple_spinner_item.xml”文件, 按照以下路径操作: C:\Users[用户名]\AppData\Local\Android\sdk\platforms[android-23]\data\res\layout

现在复制“simple_spinner_item.xml”文件的内容

第二步,在您的项目res\layout文件夹中创建custom_spinner.xml文件

并将刚刚复制的内容粘贴到新创建的文件中

这是一个示例:

res\layout\custom_spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView android:textAlignment="inherit"
    android:ellipsize="marquee"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:singleLine="true"
    android:textColor="@color/dark_gray"
    style="?android:attr/spinnerItemStyle"
    android:id="@android:id/text1" xmlns:android="http://schemas.android.com/apk/res/android"/>

以下是适配器代码:

Spinner ddlArea = (Spinner) findViewById(R.id.ddlArea);

ddlArea.setAdapter(new ArrayAdapter<String>(this, R.layout.custom_spinner, areaList));

areaList是一个列表

谢谢, Ejaz Waquif


这不是对问题的答案。 - Olivier de Jonge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接