如何在 Android 中更改微调器选择的背景颜色

How to Change the background color of Spinner selection in Android

在上图中,GAIN 3 被选中,但它不可见,所以我该如何将其颜色更改为更深的颜色。 基本上我想将选定的文本背景更改为更深的颜色。

我正在使用 com.jaredrummler.materialspinner.MaterialSpinner 微调器。

这是 java 的实现。

spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {

    @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
        text = spinner.getText().toString();
        Log.e("Spinner Listener",text);

        if(text.contains("GAIN 0")){
            sendToDevice("F");
        } else if(text.contains("GAIN 1")){
            sendToDevice("G");
        } else if(text.contains("GAIN 2")){
            sendToDevice("H");
        } else if(text.contains("GAIN 3")){
            sendToDevice("I");
        }
    }
});

布局项如下所示。

<com.jaredrummler.materialspinner.MaterialSpinner
    android:id="@+id/spinner"
    app:ms_dropdown_max_height="350dp"
    app:ms_dropdown_height="wrap_content"
    android:textColorHighlight="#000000"
    android:layout_width="130dp"
    style="@style/spinner_style"
    android:popupTheme="@android:style/ThemeOverlay.Material"
    android:textColor="@color/blue"
    android:layout_below="@+id/testmodetitle"
    android:layout_height="wrap_content"
    android:layout_marginTop="55dp"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_toEndOf="@+id/button1"
    android:layout_marginStart="30dp" />

这样使用对你有帮助:

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
android.R.layout.simple_spinner_item, list) {

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
   View v = null;
   v = super.getDropDownView(position, null, parent);
   // If this is the selected item position
   if (position == selectedItem) {
       v.setBackgroundColor(Color.BLUE);
   }
   else {
       // for other views
       v.setBackgroundColor(Color.WHITE);

   }
   return v;
}
};

有一些属性随该特定库的实现一起可用。请查看列出属性的 readme.md 部分。

我认为您可以考虑在您声明微调器的布局中使用 ms_background_selector 属性。

因此布局声明将如下所示。

<com.jaredrummler.materialspinner.MaterialSpinner
    android:id="@+id/spinner"
    app:ms_background_selector="@drawable/your_darker_selector"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

添加一个名为 your_darker_selector.xml 的文件并将以下代码放入文件中。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@android:color/darker_gray"/>
    <item android:state_checked="false" android:drawable="@android:color/white" />
</selector>

根据需要修改选择器文件中的颜色。

为微调器的第一项提供 html 颜色代码。

String styledText = "This is <font color='red'>simple</font>."; textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);

为了改变背景颜色和其他颜色,这个库提供了一些属性。要更改所选项目的背景颜色,请使用以下代码。

<com.jaredrummler.materialspinner.MaterialSpinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:ms_background_selector="@drawable/selector_gray_white_spinner"
        app:ms_dropdown_height="wrap_content"
        app:ms_dropdown_max_height="350dp" />

在名称为 selector_gray_white_spinner.xml

的 drawable 中创建一个选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/darkGray"/>
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@color/darkGray"/>
    <item android:state_focused="true" android:drawable="@android:color/white"/>
    <item android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/white"/>

</selector>

在您的 color.xml 文件中添加深色

<color name="darkGray">#acacac</color>