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

时间:2015-12-10 09:26:22

标签: android spinner android-spinner

我想将Spinner下拉项目的文字颜色更改为黑色。

我有一个自定义Spinner,列出了不同的语言。它有一个ImageView和一个TextView。旋转器显示在带有白色文本的蓝色背景上,看起来很好。

问题是当它被点击时,下拉显示但文字几乎看不见,因为下拉的浅灰色和文本的白色。

这是activity_main.xml

中微调器的代码
<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;
        }
    }

请注意TextViewgetDropDownView的颜色设置为黑色,而getView中的颜色设置为白色。

3 个答案:

答案 0 :(得分:5)

我做过那样的事情,但在function fncInputDateTimeOnly(textBox) { var textLength = textBox.value.length; ...

BaseAdapter

答案 1 :(得分: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));
                }

            }

答案 2 :(得分:0)

您应该为微调器和getDropDownView创建自定义适配器(扩展BaseAdapter),并根据您的意愿对自定义spinner_item_dropdown.xml进行充气。