42

如何更改微调器中所选项目的字体颜色?

我可以更改所选项目的背景颜色、下拉项目的颜色等,但不能更改所选项目的文本颜色……我该怎么做?

我的代码是:这是我正在使用的微调器--:

<Spinner
                android:id="@+id/spinner1"
                android:layout_width="wrap_content"
                android:layout_height="32dip"
                android:background="@drawable/mybg"
                android:divider="@drawable/list_divider"
                android:drawSelectorOnTop="true"
                android:popupBackground="#D3D5D3"
                android:prompt="@string/activityy_prompt" 
                />

这是 mybg.xml

<!-- <item android:drawable="@drawable/blue" android:state_pressed="false"/> -->
<!-- <item android:drawable="@drawable/back11"/> -->

<item android:drawable="@drawable/greenyellow1" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/greenyellow1" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/greenyellow1" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/greenyellow1" android:state_selected="true"/>
<item android:drawable="@drawable/back11"/>

使用这些我无法更改所选项目的文本颜色...

4

14 回答 14

82

Define OnItemSelectedListener like this:

  private AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(0x00000000);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };

and then Set OnItemSelectedListener to spinner like this:

spinner.setOnItemSelectedListener(listener);
于 2013-03-13T08:27:32.807 回答
18

可绘制/mybg:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true">
        <color android:color="@color/black" />
    </item>
</layer-list>

这将更改弹出窗口中所选项目的颜色。

于 2013-06-18T08:03:35.843 回答
17

尝试在 OnItemSelectedListener 中实现 onItemSelected 以更改微调器所选项目的文本颜色

public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
    int index = adapterView.getSelectedItemPosition();
    ((TextView) spinner.getSelectedView()).setTextColor(getResources().getColor(R.color.Blue)); //<----
于 2014-05-08T05:15:13.773 回答
17

您可以通过添加OnItemSelectedListenerspinner

qtySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        ((TextView) view).setTextColor(Color.BLACK); //Change selected text color
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});
于 2017-02-09T09:12:34.160 回答
7

使用选择器作为文本颜色。

在drawable中创建color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:color="#000000" /> <!-- pressed -->
     <item android:state_focused="true"
           android:color="#000000" /> <!-- focused -->
     <item android:color="#FFFFFF" /> <!-- default -->
 </selector>

在文本视图中

<TextView 
   android:textColor="@drawable/color_selector"/>
于 2013-03-13T08:11:52.787 回答
4

只需使用这一行 onItemSelected 列表器 -

public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,long arg3) 
     {

       item = (String) parent.getItemAtPosition(arg2);


       ((TextView) parent.getChildAt(0)).setTextColor(0x00000000);

 }
于 2015-12-02T06:29:05.007 回答
4

((TextView) view).setTextColor(getResources().getColor(R.color.black)); onItemSelected(...)侦听器方法中设置将起作用。它为您选择的微调器文本设置颜色。

于 2018-11-08T10:34:19.043 回答
3

如果您的微调器使用 ArrayAdapter,您可以简单地为微调器项传递自定义布局。该布局可以是一个简单的 textView 但具有 android:textColor 属性。

MainActivity.java onCreate()

adapter = new ArrayAdapter<>(this, R.layout.spinner_custom_textcolor, data);
spinner.setAdapter(adapter);

spinner_custom_textcolor.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle" 
android:textColor="@color/YOUR_COLOR_HERE"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"/>

*上述布局中除了android:textColor之外的所有内容都是从android.R.layout.simple_spinner_item直接复制的

于 2016-06-15T02:49:19.550 回答
2

我知道这是个老问题,但是我在 TabLayout 中更改微调器中选定项目的颜色时遇到了很大问题,这对我来说确实有用:

spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE); //change to your color
            }
        });
于 2018-01-21T17:37:22.510 回答
1

你们中的一些人使用 MaterialBetterSpinner 和绑定你的布局,以上所有方法都无济于事,试试这个,希望它对你有帮助:

public class MyAdapter extends ArrayAdapter<String> {      

        public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
            super(context, textViewResourceId, objects);           

        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final YourXMLBinding rowBinding = DataBindingUtil.inflate(inflater, R.layout.yourXML, parent,false);
            rowBinding.tv1.setText(mMy.getValues().get(position));
            if(position == mMy.getCurrentIndex()) {
                rowBinding.tv1.setTypeface(Typer.set(getContext()).getFont(Font.ROBOTO_BOLD));//change font
                rowBinding.tv1.setTextColor(ContextCompat.getColor(getContext(), R.color.yourColor));//change color
            }
            return rowBinding.getRoot();
        }
    }

yourXML 是这样的:

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/colorBackgroundStart">
        <TextView
            android:id="@+id/tv1"
            android:layout_width="0dp"
            android:layout_weight="0.7"
            android:layout_height="30dp"
            android:textColor="#fff"
            android:textSize="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="8dp"/>

</layout>

使用此适配器和 yourXML 创建一个微调器:

final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues());

final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext());
spinner.setAdapter(adapter);
于 2017-07-04T05:36:47.060 回答
0

在此处查看我对类似问题的回答。我的答案与 Priya 的类似,除了它也正确设置了默认选定项目的文本颜色(因此在等待微调器自动选择默认项目时不会出现错误颜色的滞后,这发生在用户界面已经打开之后 -屏幕)。

于 2015-11-25T09:05:05.953 回答
0

在 Android 2.3v 中,您不需要用于背景颜色更改的 java 代码。只需 android:background="#F0F8FF"在 xml 文件中添加到您的微调器。

于 2017-04-25T06:29:09.160 回答
0

对我有用的解决方案是将 aCheckedTextView用于下拉资源视图,然后使用颜色选择器更改选中项目的颜色。

spinner_dropdown_item.xmllayout文件夹中:

<?xml version="1.0" encoding="utf-8"?>

<!-- A `CheckedTextView` allows the color of the text to be changed when it is selected (checked). -->
<CheckedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner_item_textview"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:maxLines="1"
    android:ellipsize="end"
    android:paddingStart="20dp"
    android:paddingEnd="20dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:textSize="18sp"
    android:textColor="@color/spinner_color_selector"
    android:background="@color/spinner_background" />

spinner_color_selectorcolor文件夹中:

<?xml version="1.0" encoding="utf-8"?>

<!-- Highlight the selected (checked) item when the spinner is open. -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:color="@color/white" />
    <item android:color="@color/blue_100" />
</selector>

spinner_dropdown_item.xml必须设置为DropDownResourceViewAdapter。就我而言,我使用的ResourceArrayAdapter是从多个来源提取信息的。

// Setup a `MatrixCursor` for the static entries.
String[] matrixCursorColumnNames = {DatabaseHelper._ID, DatabaseHelper.NAME};
MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
matrixCursor.addRow(new Object[]{-2, getString(R.string.first_spinner_item)});
matrixCursor.addRow(new Object[]{-1, getString(R.string.second_spinner_item)});

// Get a `Cursor` with the list of additional items from the database.
Cursor cursor = DatabaseHelper.getCursor();

// Combine `matrixCursor` and `cursor`.
MergeCursor mergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});

// Create a `ResourceCursorAdapter` for the spinner with `this` context.  `0` specifies no flags.;
ResourceCursorAdapter resourceCursorAdapter = new ResourceCursorAdapter(this, R.layout.spinner_item, mergeCursor, 0) {
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // Get a handle for the spinner item `TextView`.
        TextView spinnerItemTextView = (TextView) view.findViewById(R.id.spinner_item_textview);

        // Set the `TextView` to display the name.
        spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.NAME)));
    }
};

// Set the `ResourceCursorAdapter` drop drown view resource.
resourceCursorAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

// Get a handle for the `Spinner`.
Spinner spinner = (Spinner) findViewById(R.id.spinner);

// Set the adapter for the folder `Spinner`.
spinner.setAdapter(resourceCursorAdapter);

因为在打开和关闭时ResourceCursorAdapter使用相同来填充微调器,所以in和的 id必须相同。bindViewTextViewspinner_dropdown_item.xmlspinner_item.xml

spinner_item.xmllayout文件夹中:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner_item_textview"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:maxLines="1"
    android:ellipsize="end"
    android:paddingStart="10dp"
    android:paddingEnd="10dp"
    android:textSize="18sp"
    android:textColor="@color/primaryTextColor" />
于 2017-10-24T08:16:55.173 回答
0
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
           ((TextView) adapterView.getChildAt(0)).setTextColor(Color.WHITE);
}
于 2017-10-24T07:42:50.623 回答