如何更改Spinner字体颜色?

时间:2022-01-03 19:46:45

I'm having an issue with the Droid X phones where users say that the font color turns out to be white in the spinner, making it invisible unless the users highlight the items. No other phones seem to have this problem. I was going to try to force the font to be black to see if that helps. How can I do that?

我遇到了Droid X手机的问题,用户说旋转器中的字体颜色变成白色,除非用户突出显示项目,否则它将不可见。没有其他手机似乎有这个问题。我打算尝试将字体强制为黑色以查看是否有帮助。我怎样才能做到这一点?

Here's how I'm currently populating the spinner. It seems like the simple_spinner_item is broken on Droid X's.

这就是我目前填充微调器的方式。似乎simple_spinner_item在Droid X上被打破了。

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

7 个解决方案

#1


46  

I'm going to use Spinner project sample from Android SDK for next code examples.

我将使用Android SDK中的Spinner项目示例来获取下一个代码示例。


Code:

First, you need to create you custom adapter which will intercept the creation of views in drop down list:

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

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;

    }
}

Then you create adapter in your code like this:

然后在代码中创建适配器,如下所示:

 String [] spin_arry = getResources().getStringArray(R.array.Planets);        
 this.mAdapter = new CustomArrayAdapter<CharSequence>(this, spin_arry);

Explanation:

Because CustomArrayAdapter knows that we use android's built-in layout resource, it also knows that text will be placed in TextView with id android.R.id.text1. That's why it can intercept the creation of views in drop down list and change text color to whatever color is needed.

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


Screenshot:

如何更改Spinner字体颜色?

#2


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

#3


7  

write a R.layout.simplespinneritem:

写一个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" />

The ID is android:id="@android:id/text1", set the color of font and background.

ID是android:id =“@ android:id / text1”,设置字体和背景的颜色。

ArrayAdapter adapter =
  new ArrayAdapter(this,packagename.R.layout.simple_spinner_item, spin_arry);

#4


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

}  
}

and in xml res/layout add new xml file: type layout, spinner

并在xml res / layout中添加新的xml文件:type layout,spinner

(in 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" />

#5


2  

To add to sasad's reply, make a copy of that file, which you can find in your Android folder, in your project, change the text color of the TextView in that file, and use that layout while initializing the Adapter instead of android's.

要添加到sasad的回复,请在项目中制作该文件的副本(可在Android文件夹中找到),更改该文件中TextView的文本颜色,并在初始化Adapter而不是android时使用该布局。

#6


-1  

make your own layout xml file, and give a android:textColor="#000" for black text

制作自己的布局xml文件,并为黑色文本提供android:textColor =“#000”

#7


-1  

Here is more appropriate way guys,

这是更合适的方式,

First find the "simple_spinner_item.xml" file in your system, Follow the below path, C:\Users[username]\AppData\Local\Android\sdk\platforms[android-23]\data\res\layout

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

Now copy the content of "simple_spinner_item.xml" file

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

Second create the custom_spinner.xml file in your project res\layout folder

其次,在项目res \ layout文件夹中创建custom_spinner.xml文件

and paste the copied content in recently created file

并将复制的内容粘贴到最近创建的文件中

Here is the sample:

这是样本:

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

Here is the set adapter code:

这是设置的适配器代码:

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

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

Where areaList is the List

其中areaList是List

Thanks, Ejaz Waquif

谢谢,Ejaz Waquif

#1


46  

I'm going to use Spinner project sample from Android SDK for next code examples.

我将使用Android SDK中的Spinner项目示例来获取下一个代码示例。


Code:

First, you need to create you custom adapter which will intercept the creation of views in drop down list:

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

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;

    }
}

Then you create adapter in your code like this:

然后在代码中创建适配器,如下所示:

 String [] spin_arry = getResources().getStringArray(R.array.Planets);        
 this.mAdapter = new CustomArrayAdapter<CharSequence>(this, spin_arry);

Explanation:

Because CustomArrayAdapter knows that we use android's built-in layout resource, it also knows that text will be placed in TextView with id android.R.id.text1. That's why it can intercept the creation of views in drop down list and change text color to whatever color is needed.

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


Screenshot:

如何更改Spinner字体颜色?

#2


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

#3


7  

write a R.layout.simplespinneritem:

写一个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" />

The ID is android:id="@android:id/text1", set the color of font and background.

ID是android:id =“@ android:id / text1”,设置字体和背景的颜色。

ArrayAdapter adapter =
  new ArrayAdapter(this,packagename.R.layout.simple_spinner_item, spin_arry);

#4


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

}  
}

and in xml res/layout add new xml file: type layout, spinner

并在xml res / layout中添加新的xml文件:type layout,spinner

(in 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" />

#5


2  

To add to sasad's reply, make a copy of that file, which you can find in your Android folder, in your project, change the text color of the TextView in that file, and use that layout while initializing the Adapter instead of android's.

要添加到sasad的回复,请在项目中制作该文件的副本(可在Android文件夹中找到),更改该文件中TextView的文本颜色,并在初始化Adapter而不是android时使用该布局。

#6


-1  

make your own layout xml file, and give a android:textColor="#000" for black text

制作自己的布局xml文件,并为黑色文本提供android:textColor =“#000”

#7


-1  

Here is more appropriate way guys,

这是更合适的方式,

First find the "simple_spinner_item.xml" file in your system, Follow the below path, C:\Users[username]\AppData\Local\Android\sdk\platforms[android-23]\data\res\layout

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

Now copy the content of "simple_spinner_item.xml" file

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

Second create the custom_spinner.xml file in your project res\layout folder

其次,在项目res \ layout文件夹中创建custom_spinner.xml文件

and paste the copied content in recently created file

并将复制的内容粘贴到最近创建的文件中

Here is the sample:

这是样本:

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

Here is the set adapter code:

这是设置的适配器代码:

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

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

Where areaList is the List

其中areaList是List

Thanks, Ejaz Waquif

谢谢,Ejaz Waquif