无法在Spinner上更改文本颜色

3

我尝试了很多SO上的答案,但似乎都不适用于我。我不确定我做错了什么。

这是我的xml:

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/product_category_spinner"
    android:theme="@style/AppartmentBlue" />

以下是我的Java代码(它是一个扩展DialogFragment的类):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_product_dialog, container, false);
    Spinner productCategory = (Spinner) v.findViewById(R.id.product_category_spinner);
    ArrayAdapter<String> spinnerAdapter =
            new ArrayAdapter<String>(getActivity().getApplicationContext(),
                    android.R.layout.simple_spinner_dropdown_item,
                    getResources().getStringArray(R.array.category_array));
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    productCategory.setAdapter(spinnerAdapter);

    // setCancelable(false);
    return v;
}

事实上,这段文字是白色的,放在非常明亮的灰色背景上。这是默认设置,如果前景颜色为黑色,我就不需要修改它了。为什么会这样呢?我的主题继承自Theme.Holo.Light.DarkActionBar
我不想在运行时设置它,我将有很多旋转器,并希望它在整个应用程序中全局使用,并具有以后添加主题的选项。
1个回答

4
希望这能帮到你:
我的选择器(Spinner):
<Spinner
    android:id="@+id/spinner_cat"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingBottom="0dp"
    android:paddingTop="0dp"
    android:layout_marginStart="@dimen/left_margin_large"
    android:layout_marginLeft="@dimen/left_margin_large"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"/>

安装适配器:

private final int _singleItemRes = R.layout.simple_spinner_item;
private final int _dropdownRes = R.layout.simple_spinner_dropdown_item;

ArrayAdapter<String> adapter = new ArrayAdapter<String>(_handler.getContext(), _singleItemRes, getTitles());
adapter.setDropDownViewResource(_dropdownRes);

simple_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:singleLine="true"
    android:textColor="@color/actionbar_text"
    android:textSize="20sp"
    android:textStyle="normal"/>

simple_spinner_dropdown_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@color/primary_dark"
    android:ellipsize="marquee"
    android:layout_centerVertical="true"
    android:singleLine="true"
    android:paddingRight="@dimen/left_margin_small"
    android:paddingLeft="@dimen/left_margin_small"
    android:textColor="@color/actionbar_text"/>

如果我的回答有遗漏,请告诉我,我会添加进去。


我改成了自己的风格,它就可以工作了!可惜我需要创建一个纯文本视图的资源布局来全局更改颜色。 - casraf
很高兴它能够工作。我同意这是谷歌没有好好考虑的一个令人恼火的问题。 - James Cross

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