如何在Android中更改Spinner选择的背景颜色

时间:2018-04-20 06:15:32

标签: java android spinner android-spinner

enter image description here

在上面的图片中选择了GAIN 3但是它不能正确显示,所以如何将该颜色更改为更暗的颜色。 基本上我想以较暗的颜色更改所选的文本背景。

我正在使用com.jaredrummler.materialspinner.MaterialSpinner Spinner。

这是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" />

4 个答案:

答案 0 :(得分:2)

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

<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" />

在drawable中创建一个名为selector_gray_white_spinner.xml

的选择器
<?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>

答案 1 :(得分:1)

可以使用某些属性以及该特定库的实现。请查看列出属性的readme.md部分。

enter image description here

我认为你可以考虑在你声明了微调器的布局中使用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>

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

答案 2 :(得分:0)

使用这种方式可以帮助您:

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;
}
};

答案 3 :(得分:0)

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

>>> inc(a, (1,1)) True >>> inc(a, (0,0)) True >>> inc(a, (3,4)) True