【问题标题】:Change the text color of the spinner text in Android?在Android中更改微调器文本的文本颜色?
【发布时间】:2014-07-02 12:23:36
【问题描述】:

我们正在尝试更改微调器中文本的颜色。

这是我们活动布局中的 XML:

        <Spinner
            android:id="@+id/SpinnerFeedbackType"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:entries="@array/feedbacktypelist"
            android:textColor="@color/white" >
        </Spinner>

这是strings.xml

<color name="white">#FFFFFF</color>

<!-- Other string resources also dfined in this file… -->
<string name="feedbacktype1">www.currycottageferndown.co.uk</string>
<string name="feedbacktype2">192.168.1.1</string>
<string name="feedbacktype3">8.8.8.8</string>
<string name="feedbacktype4">www.bournemouth.ac.uk</string>

颜色保持黑色。

由于某种原因,我们没有数组适配器,这是我们的微调器代码。

Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
            final String feedbackType = feedbackSpinner.getSelectedItem().toString();

有什么想法吗?

【问题讨论】:

  • 为此,您已经为微调器制作了自定义适配器。
  • 或者你可以使用你正在使用的适配器,但你仍然必须覆盖 getView
  • @dhali 发布您的 java 代码,在其中声明 Spinner 并在其中设置要在其上显示的值。
  • @dhali 查看我的答案的更新。我看到数据来自@array/feedbacktypelist,你可以在适配器的声明中看到R.array.feedbacktypelist。我已更新我的答案以符合您的目的。

标签: android string layout spinner


【解决方案1】:

可以试试下面的代码:
styles.xml

中添加以下代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MooTheme" parent="android:Theme">
        <item name="android:spinnerItemStyle">@style/MooSpinnerItem</item>
    </style>

    <style name="MooSpinnerItem" parent="android:Widget.TextView.SpinnerItem">
        <item name="android:textAppearance">@style/MooTextAppearanceSpinnerItem</item>
    </style>

    <style name="MooTextAppearanceSpinnerItem" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
        <item name="android:textColor">#F00</item>
    </style>
</resources>

然后将其添加到您的 AndroidManifest.xml 中的应用程序标签

android:theme="@style/MooTheme"

【讨论】:

  • 很高兴它帮助了你:)
【解决方案2】:

您需要创建自定义布局才能创建自定义Spinner

创建布局文件 spinner_item.xml

使用微调器项目的自定义定义创建您自己的布局文件。这是我的 spinner_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textColor="#ff0000" />

更改您的微调器声明以使用R.layout.spinner_item

// Declare the spinner
Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
// Set the custom layout to the array adapter, and send your array feedbacktypelist with the data
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.feedbacktypelist, R.layout.spinner_item);
feedbackSpinner.setAdapter(adapter);

这是结果

现在,要自定义下拉列表项,您需要为下拉列表创建一个新的布局文件。

创建一个名为 spinner_dropdown_item.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="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee"
    android:textColor="#aa66cc"/>

更改微调器的声明

// Declare the spinner
Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
// Set the custom layout to the array adapter, and send your array feedbacktypelist with the data
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.feedbacktypelist, R.layout.spinner_item);
// Set the custom layout for the drop down items.
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
feedbackSpinner.setAdapter(adapter);

这是结果:

基本上就是这样。

答案的来源。 How to change a Spinner text size, color or overall style

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 2016-08-20
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多