在我的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"

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


当前回答

你可以有这种类型的转轮适配器,完全定制:

 ArrayAdapter<String> genderAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, genderList) {

        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            ((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
            ((TextView) v).setTextColor(Color.parseColor("#676767"));
            ((TextView) v).setTypeface(vrFont);
            return v;
        }

        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View v = super.getDropDownView(position, convertView, parent);
            ((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
            ((TextView) v).setTypeface(vrFont);
            ((TextView) v).setTextColor(Color.parseColor("#676767"));

            if (position == 0) {
                ((TextView) v).setTextColor(Color.parseColor("#979797"));
            }

            return v;
        }

而出来。spinner_text是:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical|left"
android:ellipsize="marquee"
android:maxLines="1"
android:textColor="@color/whiteThree" />

其他回答

如果您使用的是android.support.v7.widget。AppCompatSpinner是使用样式的最简单的测试解决方案:

 <android.support.v7.widget.AppCompatSpinner
                    android:id="@+id/spefcialFx"
                    style="@style/Widget.AppCompat.Spinner.Underlined"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="4dp"
                    android:theme="@style/Spinner"
                    android:entries="@array/special_fx_arrays"
                    android:textSize="@dimen/text_size_normal"></android.support.v7.widget.AppCompatSpinner>

以及风格:

<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
        <item name="android:paddingStart">0dp</item>
        <item name="android:paddingEnd">0dp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:backgroundTint">@color/red</item>
        <item name="android:textSize">14sp</item>
    </style>

唯一的缺点是android:backgroundTint为下拉箭头和下拉背景设置颜色。

    String typeroutes[] = {"Select","Direct","Non Stop"};
    Spinner typeroute;

    typeroute = view.findViewById(R.id.typeroute);

    final ArrayAdapter<String> arrayAdapter5 = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_spinner_item, typeroutes) {
            @Override
            public boolean isEnabled(int position) {
                if (position == 0) {
                    // Disable the first item from Spinner
                    // First item will be use for hint
                    return false;
                } else {
                    return true;
                }
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                ((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
                ((TextView) v).setTextColor(Color.parseColor("#ffffff"));
                return v;
            }         ---->in this line very important so add this

            @Override
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                View view = super.getDropDownView(position, convertView, parent);
                TextView tv = (TextView) view;
                if (position == 0) {
                    // Set the hint text color gray
                    tv.setTextColor(Color.GRAY);
                } else {
                    tv.setTextColor(Color.BLACK);
                }
                return view;
            }
        };

        arrayAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        typeroute.setAdapter(arrayAdapter5);

这就是所有的享受你的编码…

我们可以像这样改变旋转器的textview设置主题的样式:

styles.xml:

<style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textSize">@dimen/_11ssp</item>
    <item name="android:textColor">@color/blue</item>
    <item name=...</item>
</style>

then

<Spinner
                    android:theme="@style/mySpinnerItemStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

如果你想改变纺纱机的textview属性编程式:

编程:

val textView = (view.getChildAt(0) as TextView)
textView.setTextColor(resources.getColor(R.color.dark_mode))

可以通过重写getView方法更改文本颜色,如下所示:

 new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, list()){
                @Override
                public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
                    //change the color to which ever you want                    
                    ((CheckedTextView) view).setTextColor(Color.RED);
                    //change the size to which ever you want                    
                    ((CheckedTextView) view).setTextSize(5);
                    //for using sp values use setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    return view;
                }
    }

首先,我们必须为textview创建简单的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"
    android:textSize="20sp"
    android:gravity="left"  
    android:textColor="#FF0000"         
    android:padding="5dip"
    />   

然后保存。在你的适配器列表上设置后。