如何更改微调器的文本颜色

时间:2016-03-21 18:54:37

标签: java android xml android-spinner

作为一个菜鸟,我发现这个问题的其他答案很难理解。我像这样初始化一个微调器:

mMembership = (Spinner) findViewById(R.id.RegisterMembershipSpinner);
    ArrayAdapter<CharSequence> member_adapter = ArrayAdapter.createFromResource(this,
                    R.array.membership_array, android.R.layout.simple_spinner_item);
            // Specify the layout to use when the list of choices appears
            member_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            // Apply the adapter to the spinner
            mMembership.setAdapter(member_adapter);

我在layout文件夹中创建了名为spinner_items.xml的XML,其中包含以下内容:

     <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="#FFFFFF"
        android:padding="5dip"
        android:id="@+id/textSpinner"
        />

我应该从这里做些什么才能让我更改微调器项目的文本颜色?

1 个答案:

答案 0 :(得分:4)

android.R.layout.simple_spinner_item替换为R.layout.spinner_items

并将android:id="@android:id/text1"添加到您的XML文件

编辑: 如果要更改微调器选定的项目文本颜色。

Spinner mMembership = (Spinner) findViewById(R.id.RegisterMembershipSpinner);
    ArrayAdapter<CharSequence> member_adapter = ArrayAdapter.createFromResource(this,
            R.array.membership_array, R.layout.spinner_items);
    // Specify the layout to use when the list of choices appears
    member_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    mMembership.setAdapter(member_adapter);

spinner_items.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"
    android:id="@android:id/text1"
/>

如果您想更改微调器下拉项文本颜色,则您的java代码为:

mMembership = (Spinner) findViewById(R.id.RegisterMembershipSpinner);
    ArrayAdapter<CharSequence> member_adapter = ArrayAdapter.createFromResource(this,
            R.array.membership_array, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    member_adapter.setDropDownViewResource(R.layout.spinner_items);
    // Apply the adapter to the spinner
    mMembership.setAdapter(member_adapter);

spinner_items.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:textColor="#00FF00"/>