【问题标题】:Spinner item pressed blue background微调项目按下蓝色背景
【发布时间】:2014-04-14 20:51:29
【问题描述】:

我使用微调器来显示下拉列表。 我想让列表中的项目有圆角。 所以我使用 9-patch 包含一个带有圆角的图像(在角的外侧是透明的)作为视图项背景和一个选择器,以便在按下时显示不同颜色的 9-patch。

问题:当我按下微调器列表中的项目时,我可以在角落看到蓝色背景,其中 9-patch 是透明的。

我似乎无法摆脱按下项目时出现的蓝色背景。如果我删除 9 补丁和微调器中的任何设置,我可以看到列表中的项目视图默认为灰色,按下时为蓝色。

我也尝试不使用 9 个色块作为背景,而仅使用颜色选择器,并将选择器中的按下颜色设置为透明。然后当我按下该项目时,它不是透明的,而是蓝色的。我觉得list里面的view真的是透明的,但是按下的时候背景还是有蓝色的……

我使用自定义 SpinnerAdapter 来创建项目视图。 这是简化的代码:

   private class MySpinnerAdapter implements SpinnerAdapter {
        @Override
        public View getDropDownView(int i, View recycledView, ViewGroup viewGroup) {
            View view = new View(context);
            view.setBackground(context.getResources().getDrawable(R.drawable.testspinner));
            view.setMinimumHeight(100);
            return (View) view;
        }
} 

用于背景的选择器。这里只有一种颜色,没有 9-patch。按下的颜色应该是透明的:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
            android:state_pressed="true"
            android:drawable="@android:color/transparent" />
    <item
            android:drawable="@android:color/holo_purple" />

</selector>

我在微调器上设置了自定义适配器:

    spinner.setAdapter(new MySpinnerAdapter());

并且微调器是从 XML 布局中获取的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent" android:layout_height="wrap_content">

<Spinner
        android:id="@+id/myDropDown"
        android:spinnerMode="dropdown"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:dropDownWidth="match_parent"/>
</LinearLayout>

我尝试在 Spinner 上设置许多不同的属性,并尝试了一些样式属性,但我无法摆脱这个蓝色背景...

【问题讨论】:

  • 你在哪里设置背景?代码不存在...
  • @ElDuderino:我刚刚添加了上面缺少的代码
  • 尝试自定义微调器并更改 setonitemselectedlistener 上的充气器布局颜色。

标签: android spinner


【解决方案1】:

感谢@Vikram 指出样式的使用。

但是,经过调查发现 Spinner 中被按下的项目的背景不是来自您建议的属性,而是来自android:listSelector。所以我可以这样解决问题:

在styles.xml中定义一个新的样式:

<style name="MyListView">
    <item name="android:listSelector">@android:color/transparent</item>
</style>

在themes.xml中定义一个新主题:

<style name="MyTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
    <item name="android:dropDownListViewStyle">@style/MyListView</item>
</style>

将主题应用到我在 AndroidManifest.xml 中的活动:

<activity android:name=".MyActivity"
...
android:theme="@style/MyTheme">

【讨论】:

    【解决方案2】:

    问题:当我按下微调器列表中的项目时,我可以看到一个 角落的蓝色背景,9-patch 是透明的。

    我认为blue background 来自android:selectableItemBackground 属性。要更改此属性,请将以下内容添加到 styles.xml 中的应用程序主题中:

    <item name="android:selectableItemBackground">@drawable/whicheverDrawable</item>
    

    供参考:默认情况下,selectableItemBackground 指向 API 19 中的以下可绘制对象(用于 Theme.Holo)。你应该以类似的方式定义whicheverDrawable

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
        <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_disabled_holo_dark" />
        <item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/list_selector_disabled_holo_dark" />
        <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_dark" />
        <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_dark" />
        <item android:state_focused="true" android:drawable="@drawable/list_focused_holo" />
        <item android:drawable="@color/transparent" />
    </selector>
    

    在您的情况下,您可以为另一个可绘制对象定义圆角,该圆角用于whicheverDrawable 中的statePressed

    【讨论】:

    • 感谢@Vikram 的回答让我找到了真正的解决方案。看我的回答。
    • @muslidrikk 没问题。很高兴我能帮助你。我意识到我的答案并不完全正确。所以我做了更多的研究。如果将来您确实想设置一个选择器而不是color/transparent,请查看res/values/drawable/list_selector_holo_dark.xml,并根据它创建您的选择器。
    猜你喜欢
    • 2018-11-29
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多