26

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

我如何摆脱白色背景

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

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

根据要求,这是我到目前为止的做法。这些是我认为相关的片段,但我可能错过了一些东西。

这是微调器的 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" />

在我的价值观/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 图像。有很多指南,这是我在 30sec 在 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>

在这些文件的某个地方,我认为应该使某些内容透明,但我不知道在哪里。

4

4 回答 4

54

删除SpinnerStyle背景属性。

删除这个:

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

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

在您问题中的代码的基础上,这里有一个关于如何设置 Spinner 样式的基本示例:

styles.xml文件为SpinnerItemand设置样式SpinnerDropDownItem

<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. 将其替换为您自己的可绘制对象:

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

添加SpinnerActivity'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>

这会产生:

在此处输入图像描述

背景中没有白色方块。

于 2012-12-16T14:42:24.913 回答
2

添加

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

到您的微调器样式。

于 2012-12-06T07:40:26.400 回答
2

样式 android 微调器

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

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

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

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

于 2019-04-20T16:47:23.890 回答
0

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

<item name="android:popupBackground">@null</item> 
于 2013-09-06T22:44:46.330 回答