【问题标题】:How to Change the background color of Spinner selection in Android如何在 Android 中更改 Spinner 选择的背景颜色
【发布时间】:2018-04-20 20:30:12
【问题描述】:

在上图中,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" />

【问题讨论】:

  • 你能分享你试过的代码吗?
  • 我分享了我的
  • 请在下面查看我的答案。

标签: java android spinner android-spinner


【解决方案1】:

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

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

【讨论】:

  • 这件事对我有帮助,实际上我犯了一个错误,使用 compile 'com.jaredrummler:material-spinner:1.1.0' 而不是使用 compile 'com.jaredrummler:material-spinner:1.2.5 ' 非常感谢
【解决方案2】:

这样使用会帮助你:

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

【讨论】:

  • 实际上我正在使用 MaterialSpinner 。如何将其添加到我的代码中?
【解决方案3】:

随着特定库的实现,有一些可用的属性。请查看列出属性的 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>

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

【讨论】:

  • 我应该在“@drawable/your_darker_selector”中添加什么代码?
  • 这件事对我有帮助,实际上我犯了一个错误,使用 compile 'com.jaredrummler:material-spinner:1.1.0' 而不是使用 compile 'com.jaredrummler:material-spinner:1.2.5 ' 非常感谢
【解决方案4】:

为 spinner 的第一项提供 html 颜色代码。

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

【讨论】:

    猜你喜欢
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    相关资源
    最近更新 更多