在我的Android应用程序中,我正在使用旋转器,我已经从SQLite数据库加载数据到旋转器中,它工作正常。这是它的代码。

Spinner spinner = (Spinner) this.findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>  (this,android.R.layout.simple_spinner_item, list);
cursor.moveToFirst();

list.add("All Lists");

if (cursor.getCount() > 0) {
    for (int i = 0; i < cursor.getCount(); i++) {
        keyList[i] = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.KEYWORD));
        list.add(keyList[i]);
        cursor.moveToNext();
    }
}
Database.close();
cursor.close();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

现在我想要改变旋转数据的文本颜色和文本大小。我在XML文件上使用了以下XML行作为旋转标记,但它不起作用。

android:textColor="@android:color/white"
android:textSize="11dp"

我如何改变我的转轮的文本颜色和文本大小?


当前回答

更改微调文本的颜色:

 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);}

其他回答

只是想对上面的正确答案做个小改动。 为布局目录中的spinner项创建一个自定义XML文件。

spinner_style.xml:

将自定义的颜色和大小设置为该文件中的文本。

 <?xml version="1.0" encoding="utf-8"?>

     <TextView  
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            style="?android:attr/spinnerItemStyle"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:textAlignment="inherit"
            android:textSize="15sp"
            android:textColor="#FF0000"         
            android:padding="5dp"
            />

现在使用这个文件在你的java文件中显示你的spinner项:

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.spinner_style,list);
adapter.setDropDownViewResource(R.layout.spinner_style);

而不是做一个自定义布局来获得一个小尺寸,如果你想使用Android内部的小尺寸布局,你应该使用:

“android.R.layout。simple_gallery_item而不是“android. r.b ayout.simple_spinner_item”。

ArrayAdapter<CharSequence> madaptor = ArrayAdapter
            .createFromResource(rootView.getContext(),
                                R.array.String_visitor,
                                android.R.layout.simple_gallery_item);

它可以减小纺纱器的布局尺寸。这只是个简单的把戏。

如果你想减小下拉列表的大小,使用以下命令:

madaptor.setDropDownViewResource(android.R.layout.simple_gallery_item);

对于那些想要改变DrowDownIcon颜色的人 你可以这样用

spinner.getBackground().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:ellipsize="marquee"
    android:textAlignment="inherit"/>

就用这个吧:

ArrayAdapter<String> adapter_category = new ArrayAdapter<String>(this,
    R.layout.spinner_list_item, categories);
adapter_category
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

如果所有的旋转框对它们的TextView项可能有相同的文本颜色,另一种方法是为旋转框下拉项使用自定义样式:

在res /价值/ styles.xml:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
    </style>

    <style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
        <item name="android:textColor">@color/my_spinner_text_color</item>
    </style>
</resources>

并在res/values/colors.xml中定义自定义颜色:

<color name="my_spinner_text_color">#808080</color>