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

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


当前回答

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

其他回答

Ashraf的另一个解决方案是确保你考虑到屏幕尺寸。你需要在onCreate中获得旋转器,并在设置适配器后设置监听器:

//set your adapter with default or custom spinner cell, then://
serverSpinner.setOnItemSelectedListener(spinnerSelector);
serverSpinner.setSelection(defaultServer);

然后你可以开始改变在旋转器被点击之前显示的视图的文本大小:

private AdapterView.OnItemSelectedListener spinnerSelector = new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
        boolean largeTablet = getResources().getBoolean(R.bool.isLargeTablet);
        if (tabletSize) { ((TextView)parent.getChildAt(0)).setTextSize(16); }
        else if (largeTablet) { ((TextView)parent.getChildAt(0)).setTextSize(18); }
        else { ((TextView)parent.getChildAt(0)).setTextSize(12); }
    }
    public void onNothingSelected(AdapterView<?> parent) {

    }
};

你所需要做的就是创建如下布局的特定文件夹:

values-sw360dp values-sw600dp values-sw800dp

然后在每个文件夹中添加一个名为“bol . XML”的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="isTablet">false</bool>
    <bool name="isLargeTablet">false</bool>
</resources>

简单而干脆……

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

    }
};

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

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

首先,我们必须为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"
    />   

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

对于那些只需要样式方式的AppCompat。

结果

styles.xml

<resources>
    ... 
    <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/material_grey_700</item>
        <item name="android:textSize">12sp</item>
    </style>
</resources>

your_spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    ...

    <android.support.v7.widget.AppCompatSpinner
        android:id="@+id/content_spinner"
        style="@style/Widget.AppCompat.Spinner.Underlined"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:entries="@array/shipping_tracking_carrier_names"
        android:spinnerMode="dropdown"
        android:theme="@style/Spinner" />

    <EditText
        android:id="@+id/content_input"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:maxLines="1"
        android:paddingEnd="8dp"
        android:paddingStart="8dp"
        android:textColor="@color/material_grey_700"
        android:textSize="12sp" />

    ...
</LinearLayout>    

+ 如果你想用定义的样式以编程方式设置android:entries。 试试这个。

AppCompatSpinner spinner = findViewById(R.id.content_spinner);
CharSequence[] entries = getResources().getTextArray(R.array.shipping_tracking_carrier_names);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(spinner.getContext(), android.R.layout.simple_spinner_item, entries);
adapter.setDropDownViewResource(android.support.v7.appcompat.R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

在代码中,使用与Spinner相同的Context是最重要的。

spinner.getContext()