更改Spinner文本颜色

3

我想要将Spinner中的默认文本颜色更改为其他颜色,但我还没有找到解决方案。我只能通过类似于此链接如何更改Spinner文本大小和文本颜色?所述的TextView的解决方案。

请问是否有其他直接针对Spinner的方法呢?

注意:请提供Xamarin解决方案。谢谢!


使用自定义适配器。继承ArrayAdapter并重写getViewgetDropDownView方法。 - Saurabh
这个解决方案有什么问题吗?我想这已经是最简单的了。 - ADM
@ADM 只是好奇想知道,为什么他们会提供这个 Spinner,而我们却无法自定义它呢?顺便说一下,我是 Android 的新手。 - musigh
请抑制好奇心并在链接中阅读答案。所有的“AdapterView”都是为自定义项而制作的,Spinner也是如此。 - ADM
你可以编写自定义样式并添加到Spinner中。 - Mani Vasagam
5个回答

21

只针对选定项更改颜色,请尝试以下方法:

spinnerObject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
    ((TextView)parentView.getChildAt(0)).setTextColor(Color.RED);
}
});

另一种方式:

<style name="spinnerTheme">
<item name="android:textColor">@color/gray_dark</item>
</style>

<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:theme="@style/spinnerTheme"/>

1
帮我解决一下 Xamarin 的问题,谢谢。 - musigh
谢谢,它在弹出窗口文本颜色中起作用,但现在唯一剩下的问题是旋转器中的默认文本颜色是黑色。如何更改? - musigh
编写自定义样式以更改文本颜色。 - Mani Vasagam

2
创建一个Spinner代码
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:textSize="20sp"
android:entries="@array/planets"/>

您需要创建自己的布局文件,并使用自定义定义来定义下拉列表项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" />

如果您想自定义下拉列表项,您需要创建一个新的布局文件。 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:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>

最后,在 spinner 的声明中还有一个变化:

 ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
 R.array.planets_array, R.layout.spinner_item);
 adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);  
 spinner.setAdapter(adapter);

它可能在(Java)Android中工作,但我想在Xamarin中实现。我已经尝试了两种方法:1.将其放置在Resources/drawable/下,并使用“.xml”扩展名;2.将其放置在Resources/layout下,并使用“.axml”扩展名。但是我失败了。请帮助我理解它。谢谢。 - musigh
非常抱歉,我没有检查您的 Xamarin 标签,我不知道 :( 我只是想帮助您,但对于 Java 也可以工作。 - user8427383

0
创建一个新的XML布局:
<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:ellipsize="marquee"
android:textColor="#962b2b"
android:textAlignment="inherit"/>

现在在集合中,使用自定义的XML布局设置旋转器适配器,如下所示:

ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.custom_spinner,array);
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner.setAdapter(adapter);

帮我处理Xamarin。谢谢。 - musigh

0

您可以通过将以下代码添加到style.xml中来实现:

<resources>
<style name="MySpinnerLook" 
    parent="@android:style/Widget.TextView.SpinnerItem">
    <item name="android:textSize">22sp</item>    //any size
</style>

在使用spinner的xml中,使用style标签添加对MyStyleLook的引用,如下:

style="@style/MySpinnerLook"


0
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_5"
android:ellipsize="marquee"
android:layoutDirection="ltr"
android:padding="@dimen/padding_5"
android:singleLine="true"
android:textSize="@dimen/textsize_base" />




adapter?.setDropDownViewResource(R.layout.spinner_text)

spSourceAccount.adapter = adapter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接