【问题标题】:How to edit Widget.Spinner press effect for color change in Android如何在 Android 中编辑 Widget.Spinner 按下效果以更改颜色
【发布时间】:2014-09-09 05:33:55
【问题描述】:

我正在使用 以下样式以显示具有自定义样式的微调器

         <TableRow
                android:id="@+id/tblRwSpn11"
                style="@style/TableRowStyle" >

                <Spinner
                    android:id="@+id/btnDashCust1"
                    style="@style/ButtonStyleSpinner1"
                    android:layout_marginTop="5dp"
                    android:layout_span="2"
                    android:textSize="10sp" />
            </TableRow>

 <style name="ButtonStyleSpinner1" parent="@android:style/Widget.Spinner">
        <item name="android:textColor">@color/black</item>
        <item name="android:layout_height">35dp</item>
        <item name="android:layout_marginLeft">10dp</item>
        <item name="android:layout_marginRight">10dp</item>
        <item name="android:paddingLeft">15dp</item>
        <item name="android:textSize">@dimen/twelveTextSize</item>        
    </style>
in my SDK style.xml


 <style name="Widget.Spinner">
        <item name="android:background">@android:drawable/btn_dropdown</item>
        <item name="android:clickable">true</item>
        <item name="android:spinnerMode">dialog</item>

        <item name="android:dropDownSelector">@android:drawable/list_selector_background</item>
        <item name="android:popupBackground">@android:drawable/spinner_dropdown_background</item>
        <item name="android:dropDownVerticalOffset">-10dip</item>
        <item name="android:dropDownHorizontalOffset">0dip</item>
        <item name="android:dropDownWidth">wrap_content</item>
        <item name="android:popupPromptView">@android:layout/simple_dropdown_hint</item>
        <item name="android:gravity">center</item>
    </style>

@android:drawable/btn_dropdown如下

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_dropdown_normal" />
    <item
        android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_dropdown_disabled" />
    <item
        android:state_pressed="true"
        android:drawable="@drawable/btn_dropdown_pressed" />
    <item
        android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_dropdown_selected" />
    <item
        android:state_enabled="true"
        android:drawable="@drawable/btn_dropdown_normal" />
    <item
        android:state_focused="true"
        android:drawable="@drawable/btn_dropdown_disabled_focused" />
    <item
        android:drawable="@drawable/btn_dropdown_disabled" />
</selector>

输出:

我的主题

  <style name="MyTheme" parent="android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/thin_ab_style</item>
</style>

<style name="MyActionBar" parent="android:style/Theme.Holo.Light">
    <item name="android:background">@color/blue</item>
    <item name="android:actionBarSize">2dp</item>
</style>

<style name="thin_ab_style" parent="android:Widget.Holo.ActionBar">
    <item name="android:height">30dp</item>
    <item name="android:background">@color/blue</item>
    <item name="android:textSize">12sp</item>
    <item name="android:textColor">@color/white</item>
</style>

即使我没有设置,它也会为我的微调器显示橙色 那么它从哪里开始设置?请检查我应用于微调器的上述样式 新闻效果应根据主题显示为蓝色或灰色

【问题讨论】:

  • 检查这个stackoverflow.com/questions/3014152/…希望你能找到一些
  • @DigveshPatel 感谢您的帮助,但橙色是由于 parent="@android:style/Widget.Spinner" 在按下橙色时显示的,因此如何更改 SDK style.xml 中按下的微调器的颜色
  • 尝试为微调项制作自定义适配器。
  • @DigveshPatel 如果我删除父属性会向我显示微调器,例如 -select customer- 没有按下效果
  • 嗯,这意味着“选择客户”是您的微调数据之一。所以它的工作是这样的。您的代码没有问题。只是尝试将提示而不是数据设置为微调器

标签: android styles spinner


【解决方案1】:

只需写一个基本的Adapter extends ArrayAdapter&lt;Type&gt; 就像做列表视图一样。

为您要更改的视图覆盖 getView 方法。

覆盖按下微调器时打开的 getDropDown 视图。

最后别忘了致电Spinner.setAdapter(mAdapter)

您可以使用自定义布局来更改颜色或任何您想要的内容。

祝你好运。

【讨论】:

  • 感谢您的帮助,我已经在使用带有自定义样式的自定义微调器,请阅读发布的代码
  • 我在说你不需要自定义样式。您可以通过在背景中有选择器的自定义布局来做到这一点。另外不要忘记在单元格的根布局中添加 clickable=true。
猜你喜欢
  • 2022-01-03
  • 1970-01-01
  • 2023-03-31
  • 2021-02-15
  • 1970-01-01
  • 2015-08-15
  • 2016-03-02
  • 2011-06-24
  • 2014-11-20
相关资源
最近更新 更多