在上面的图片中,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" />
最佳答案
为了改变背景颜色和其他颜色,这个库提供了一些属性。要更改所选项目的背景颜色,请使用以下代码。
<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>
关于java - 如何在 Android 中更改 Spinner 选择的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49935163/