如何更改Spinner下拉项的文本颜色?

3
我想将Spinner下拉项的文本颜色更改为黑色。
我有一个自定义的Spinner,列出不同的语言。它有一个ImageView和一个TextView。该Spinner在蓝色背景上显示白色文本,看起来很好。
问题是当它被点击时,下拉菜单会出现,但由于下拉菜单的浅灰色和文本的白色,文本几乎不可见。
这是在activity_main.xml中用于Spinner的代码:
<Spinner
    android:id="@+id/languageSpinner"
    style="@android:style/Widget.Material.Light.DropDownItem.Spinner"
    android:layout_width="140dp"
    android:layout_height="40dp"
    android:prompt="@string/selectLanguageSpinner"
    android:spinnerMode="dropdown" />

这是spinner_row_layout.xml文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" />

        <TextView
            android:id="@+id/language"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="5dp"
            android:gravity="center_vertical"
            android:lineSpacingExtra="20dp"
            android:minHeight="40dp"
            android:textColor="@color/white"
            android:textSize="20sp" />
    </LinearLayout>
</LinearLayout>

这里文字颜色设置为白色,因为当未选择任何内容时,我需要它是白色。

这是在styles.xml中的代码:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/myAppPrimaryColor</item>
        <item name="colorPrimaryDark">@color/myAppPrimaryDarkColor</item>
        <item name="colorAccent">@color/myAppAccentColor</item>
        <item name="android:dropDownListViewStyle">@style/TestSpinnerStyle</item>
        <item name="android:spinnerItemStyle">@style/mySpinnerItemStyle</item>
    </style>

    <style name="mySpinnerItemStyle">
        <item name="android:paddingLeft">5dp</item>
        <item name="android:paddingBottom">3dp</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">@color/white</item>
    </style>

    <!-- ^^ Here I need to set the color to white, because I also have default
             spinner on the activity that needs to have a default white color. 
             But when the default spinner is clicked, the color of the 
             drop down items is black. How can I do that with my custom spinner? -->

    <style name="TestSpinnerStyle" parent="android:style/Widget.ListView.DropDown">
        <item name="android:textSize">22sp</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:height">20dp</item>
        <item name="android:divider">@color/gray</item>
        <item name="android:dividerHeight">1dp</item>
        <item name="android:dividerPadding">5dp</item>
        <item name="android:background">@color/default_gray</item>
    </style>

    <!-- ^^ The text color property here is not reflected.. -->

有没有人有关于如何做这个的建议?
编辑:在评论和答案的帮助下,我成功地完成了。我以为它可以用XML实现...无论如何,在Java代码后面,这就是我所做的:
当自定义适配器被定义时,我们需要重写getDropDownView方法,当点击下拉菜单并显示时会触发该方法。还应该重写getView方法,以便我们可以处理Spinner未被点击时的默认颜色。换句话说,这是代码:
public class CustomSpinnerAdapter extends ArrayAdapter<String> {
        public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] objects, TypedArray image){
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent){
            LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(R.layout.spinner_row_layout, parent, false);

            TextView label = (TextView) row.findViewById(R.id.language);
            label.setText(languages_list[position]);

            label.setTextColor(getResources().getColor(android.R.color.black));

            ImageView icon = (ImageView) row.findViewById(R.id.icon);
            icon.setImageDrawable(icons_list.getDrawable(position));

            return row;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(R.layout.spinner_row_layout, parent, false);

            TextView textView = (TextView) row.findViewById(R.id.language);
            textView.setText(languages_list[position]);
            textView.setTextColor(getResources().getColor(android.R.color.white));

            ImageView icon = (ImageView) row.findViewById(R.id.icon);
            icon.setImageDrawable(icons_list.getDrawable(position));

            return row;
        }
    }

请注意,在getDropDownView中,TextView的颜色设置为黑色,而在getView中则设置为白色。


你是否正在使用自己实现的适配器来显示Spinner项目? - flyingAssistant
@flyingAssistant 我已经按照这个教程完成了:http://www.edureka.co/blog/custom-spinner-in-android/ - Apostrofix
3个回答

6

我已经做过那种类似的事情,但是在BaseAdapter中。

public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                view = inflater.inflate(R.layout.spinner_item, null);
            }

            TextView txtTestText = (TextView) view.findViewById(R.id.txtSpinnerRowText);
            txtTestText.setText(list.get(position).getText());
            if (getSelectedPosition() == position) {
                txtTestText.setTextColor(context.getResources().getColor(android.R.color.holo_blue_dark));
            } else {
                txtTestText.setTextColor(context.getResources().getColor(android.R.color.primary_text_light));
            }
            return view;
        }

getSelectedPosition() 是什么? - Apostrofix
这是一个在BaseAdapter中定义为字段的所选Spinner行项目的索引。每当选择(OnItemSelectedListener.onItemSelected)或取消选择(OnItemSelectedListener.onNothingSelected)Spinner下拉项时,我都会更改此索引。 - flyingAssistant
谢谢,我已经做到了。我会编辑我的问题并附上完整的解决方案。我能够在getDropDownViewgetView中更改颜色,所以现在看起来很好。 - Apostrofix

0

试试这个,希望它能够工作。

((Spinner)findViewById(R.id.spinnergender)).setAdapter(adapter);
        ((Spinner)findViewById(R.id.spinnergender)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.e("Value",""+position);
                ((TextView) ((Spinner)findViewById(R.id.spinnergender)).getSelectedView()).setTextColor(getResources().getColor(R.color.cyan));
                if(parent.getItemAtPosition(position).toString().equals(getResources().getString(R.string.Gender))) {
                    ((TextView) ((Spinner)findViewById(R.id.spinnergender)).getSelectedView()).setTextColor(getResources().getColor(R.color.Text));
                }

            }

0

您应该为Spinner创建自定义适配器(继承BaseAdapter),并在getDropDownView上填充自定义的spinner_item_dropdown.xml,该文件可以根据您的需求进行设置。


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