【问题标题】:Style android spinner样式 android 微调器
【发布时间】:2012-12-04 12:45:18
【问题描述】:

我正在尝试让我的 android 应用程序更时尚一点,并取得了一些进展,但微调器下拉菜单给我带来了麻烦。我有一个屏幕截图可以向您展示问题:

我想要的是背景中的白色框是透明的,就像在后屏幕上的灰色覆盖层一样,与下拉菜单之外的屏幕的其余部分一样。

如果我没记错的话,Android 4.0,API 15。

按照要求,这是我目前的做法。这些是我认为相关的 sn-ps,但我可能遗漏了一些东西。

这是微调器的 xml:

<Spinner
    android:id="@+id/edit_countrySpinner"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:entries="@array/edit_countryArray"
    android:gravity="center"
    android:prompt="@string/country_prompt" />

在我的 values/style.xml 中。如果我在这里更改背景的颜色,对话框中的背景会发生变化,但所有其余的背景也会发生变化。我还没有弄清楚如何在下拉对话框中更改背景。

<style name="appcin" parent="@android:style/Theme.NoTitleBar.Fullscreen">
    <item name="android:spinnerStyle">@style/spinnerStyle</item>
    <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
    <item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItem</item> -->
    <item name="android:background">#FFFFFF</item>
</style>
<style name="spinnerStyle">
    <item name="android:background">@drawable/pink_white_dropdown</item>
    <item name="android:clickable">true</item>
</style>
<style name="SpinnerItem">
    <item name="android:textColor">#993399</item>
    <item name="android:background">@drawable/pink_white_dropdown</item>
    <item name="android:maxHeight">10dip</item>
</style>
<style name="SpinnerDropDownItem">
    <item name="android:textColor">#993399</item>
    <item name="android:background">#FFFFFF</item>
</style>

我尝试将其添加到应用主题中,但它们都没有任何区别,背景仍然是白色的。

<...theme....>
   <item name="android:dropDownListViewStyle">@style/DropDownStyle</item>
   <item name="android:dropDownSpinnerStyle">@style/DropDownStyle</item>
   <item name="android:dropDownSelector">@style/DropDownStyle</item>
</... theme ...>
<style name="DropDownStyle">
    <item name="android:background">#FFF000</item>
    <item name="android:popupBackground">#FFF000</item>
    <item name="android:cacheColorHint">#FFF000</item>
</style>

在 drawable/pink_white_dropdown 中,只为所有情况显示相同的图像。 pink_white_arrow 是我制作的 9patch 图像。有很多指南,here's one I found by 30sec on google

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

我认为在这些文件中的某处应该将某些内容设为透明,但我不知道在哪里。

【问题讨论】:

  • 你能分享一下你到目前为止是怎么做的吗?
  • 添加了当前的解决方案,我认为应该是所有相关的东西,但我不确定。

标签: android


【解决方案1】:

删除SpinnerStyle 背景属性。

删除这个:

<style name="spinnerStyle">
    <item name="android:background">@drawable/whitish_rectangle</item>
</style>

这是绘制背景白色矩形的原因。

以您问题中的代码为基础,下面是一个关于如何设置 Spinner 样式的基本示例:

styles.xml 文件为SpinnerItemSpinnerDropDownItem 设置样式:

<resources>
    <style name="customtheme" parent="@android:style/Theme.Light">
        <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
        <item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItem</item>
    </style>
    <style name="SpinnerItem">
        <item name="android:textColor">#993399</item>
        <item name="android:background">@drawable/my_rectangle</item>
    </style>
    <style name="SpinnerDropDownItem">
        <item name="android:textColor">#993399</item>
        <item name="android:background">@drawable/my_rectangle</item>
    </style>
</resources>

为了测试,我创建了一个颜色鲜艳的可绘制形状,称为my_rectangle.xml。用你自己的drawable替换它:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ff0000" />
    <stroke
        android:width="1dp"
        android:color="#888888" />
</shape>

Spinner 添加到Activity's 布局中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:padding="40dip">
    <Spinner
        android:id="@+id/edit_countrySpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/location_names"/>
</LinearLayout>

这会产生:

背景中没有白色方块。

【讨论】:

  • 非常接近,实际上是我的主题中的背景创建了后面的白框。但你带领我走上了正确的道路,现在看起来应该如此。好吧,至少这一点可以:} 现在为对话框设置样式...非常感谢您的帮助。
【解决方案2】:

添加

<item name="android:popupBackground">@null</item>

你的微调器风格。

【讨论】:

  • 没用。我还尝试将该行添加到与微调器和应用程序主题相关的所有样式中,但这也不起作用。
  • 这帮助我找到了解决方案。微调器放置在渐变灰色视图上,因此我只希望下拉菜单的背景为浅灰色 - 但它默认为全息深色。所以我将微调器的样式设置为 ​​#DDDDDD 并且效果很好。谢谢@faylon!
  • 你能解释一下你是如何设置微调器的样式的吗?我在我的主题 @style/SpinnerItemAppTheme 中使用它,其中 。但是,当我尝试将您建议的项目添加到 SpinnerItemAppTheme 时,什么也没有发生。
【解决方案3】:

style android spinner

Spinner Widget 需要两种不同的自定义布局,一种用于显示视图,另一种用于下拉列表!

在这个关于自定义微调器 Android 的示例博客中,我们将指导您如何在 android studio 中创建一个自定义微调器,它将具有:

  1. 所选项目的自定义背景
  2. 自定义所选项目文本颜色
  3. 下拉菜单的文本颜色
  4. 下拉列表的背景
  5. 微调器动画

链接:https://androiddvlpr.com/custom-spinner-android/

【讨论】:

  • 解决方案的链接很有帮助,但可能会消失。您应该考虑添加链接中的正文作为您的答案。
【解决方案4】:

我遇到了同样的问题,下面的代码对我有用:

<item name="android:popupBackground">@null</item> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多