2010-12-05 216 views
22

我遇到了Droid X手机的问题,用户说这个字体颜色在微调器中变成了白色,除非用户突出显示这些项目,否则它将不可见。没有其他手机似乎有这个问题。我试图强制字体变黑,看看是否有帮助。我怎样才能做到这一点?如何更改Spinner字体颜色?

下面是我目前如何填充微调框。 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); 

回答

45

我打算使用来自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内置的布局资源,它也知道,文本将被放置在TextView id为android.R.id.text1。这就是为什么它可以拦截在下拉列表中创建视图并将文本颜色更改为任何需要的颜色。


截图:

enter image description here

+0

更好的解决方案,修改此代码:http://stackoverflow.com/questions/10409871/how-to-increase-spinner-item-font-size – Gerry 2015-04-12 17:51:48

-1

让自己的布局xml文件,并给予机器人:文字颜色=“#000”的黑色文本

2

为了增加sasad的答复,作出这样的文件,你可以找到副本您的Android文件夹,在您的项目中,更改该文件中TextView的文本颜色,并在初始化适配器而不是Android时使用该布局。

7

简洁明快......

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

这将导致的TextView到在旋转设备时为空。 – tolgap 2012-11-14 13:46:05

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

如果使用entries属性从加载微调项目,你如何使用它? – TheRealChx101 2015-09-30 16:14:28

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 /布局添加新的XML文件:类型布局,微调

(在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" /> 
-1

这是更合适的方式家伙,

首先发现在你的系统中的 “simple_spinner_item.xml” 文件, 按照下面的路径, C:\用户[用户名] \应用程序数据\本地\ Android的\ SDK \平台[机器人-23] \ DATA \资源\布局

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

二创建custom_spinner.xm在项目资源l文件\布局文件夹

并粘贴到最近创建的文件

这里复制的内容是样本:

资源\布局\ 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)); 

where areaList is List

感谢, 埃贾兹Waquif

+0

这不是问题的答案 – 2016-11-19 06:38:18