更改Spinner背景颜色但保留箭头

74

我读了很多关于它的东西,但找不到我需要的东西。我想保留灰色箭头,但是想要从默认样式中删除水平条,并拥有白色背景。你有任何想法如何做到这一点吗?

以下是我现在所拥有的(默认旋转器样式):

输入图像描述

以下是我想要的:

输入图像描述


你的意思是什么?我已经创建了一个ArrayAdapter来填充数据,但现在更多的是关于样式,而不是数据。 - Mansur Khan
你需要创建自定义的ArrayAdapter和自定义的布局文件,然后在该适配器类的getView()方法中填充.xml文件。 - Rustam
我的解决方案有没有成功? - Kishan Dhamat
1
创建一个 image 并将该图像设置为该旋转器的 background - Kaushik
将前景或背景添加为白色。 - blackHawk
11个回答

108

我根据 @Mansur Khan 的答案做了一点修改。

在这种情况下,我们不必添加一个 ImageView,因为 spinner 已经有一个三角形箭头。所以请查看下面的代码:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:background="#FFFFFF">
        <Spinner
            style="@style/Widget.AppCompat.DropDownItem.Spinner"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:id="@+id/sign_up_country"
            />
    </RelativeLayout>

这是屏幕截图

之前: 输入图片说明

之后: 输入图片说明


android:background="@drawable/bg_spinner" -- 这里需要一个背景图片吗? - Karan Thakkar
没有这个,它会看起来像第一张图片。 - Chandler
你有这张图片吗?因为我在互联网上找不到类似的东西。 - Karan Thakkar
bg_spinner 是白色背景,我只是为它添加了圆角和填充。同时也粘贴了答案。 - Chandler
这个 spinner 的背景颜色能否改成蓝色?如果可以,我该如何更改它? - Karan Thakkar
显示剩余3条评论

41

记录一下,我找到了一个简单的解决方法:将你的选项卡包装在一个相对布局中,并添加一张图片:

 <RelativeLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="@drawable/borderbottom_white"<!-- white background with bottom border -->
     android:layout_marginTop="15dp"  >
        <Spinner
        android:id="@+id/postfield_category"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:background="@null"
        android:minHeight="0dp" />
        <ImageView 
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/arrowspinner" />
    </RelativeLayout>

2
我知道这很老了,但你能告诉我在哪里找到箭头旋转的可绘制对象吗? - Steve W
Android SDK自带的Spinner图片可以在<Android SDK文件夹>\platforms<version>\data\res\drawable-xhdpi(hdpi、mdpi等)找到。 - darwin
不是最好的解决方案,TAD(下面)和CHANDLER(上面)是正确的。 - Christian

37

一个简单的解决方案是不需要创建自己的箭头drawable,而是将下拉框包装在一个RelativeLayout中,并在RelativeLayout中设置背景颜色,而不是在下拉框中设置:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#f00" >
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

这是一个非常聪明的解决方案。谢谢分享! - Nick

4
使用这个:
yourspinner.setOnItemSelectedListener(this);
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    ((TextView) yourspinner.getSelectedView()).setBackgroundColor(getResources()
            .getColor(R.color.your_color));
}

你的类应该实现OnItemSelectedListener接口。


1
你能否提供更多关于如何实现这个的细节?难道没有一种只使用xml和drawable的方法吗,因为它只是样式。 - Mansur Khan
如果您在XML中使用了TextView并设置了背景颜色,那么箭头将会消失。这是实现的最简单方法,我已经在许多应用程序中实现过了。 - Kishan Dhamat
是的,但如果我们只更改背景颜色,我也会有带箭头的水平条。我只需要箭头。 - Mansur Khan
请参考此答案:https://dev59.com/v2Up5IYBdhLWcg3wS2HP#15380215,在这个答案中,他/她想要改变文本颜色。 - Kishan Dhamat

3

我认为在不进行复杂布局的情况下,最好的方法是:

使用此xml作为您的下拉框背景,您就可以开始了!!!

<item android:state_pressed="true">
    <shape>
        <solid android:color="@color/materialBlueGray600" />
        <corners android:radius="3dp" />
    </shape>
</item>
<item android:state_selected="true">
    <shape>
        <solid android:color="@color/materialGray50" />
        <corners android:radius="3dp" />
    </shape>
</item>
<item>
    <layer-list>
        <item>
            <shape>
                <solid android:color="@color/materialGray50" />
                <corners android:radius="3dp" />
            </shape>
        </item>

        <item android:gravity="right">
            <bitmap android:antialias="true"  android:gravity="right" android:src="@drawable/ic_expand_small" />
        </item>
    </layer-list>
</item>


这段代码出现了错误。 - Md Imran Choudhury

3

您可以不必将Spinner组件包裹在LinearLayout、RelativeLayout等父布局中,从而增加布局解析次数。您只需在drawable文件夹下创建一个名为spinner_bg.xml的drawable即可。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:src="@drawable/icn_dropdown_arw" />
    </item>
</layer-list>

将spinner_bg设置为您的spinner的背景,它会完美运作:

<Spinner  
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:padding="5dp"
     android:background="@drawable/spinner_bg" />

2

以下是自定义下拉列表的代码,请查看并告诉我。我还没有测试过,所以请在检查后通知我是否解决了您的问题。

<Spinner                                
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/spinner_background"
    android:gravity="center"
    android:paddingRight="10dp"
    android:spinnerMode="dropdown"
    android:textColor="your text color"
    android:textSize="your text size" />

这里是用于创建下拉列表背景的drawable(spinner_background.xml)。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="border color" />

            <corners android:radius="3dp" />
        </shape>
    </item>
    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp">
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/white" />

            <corners android:radius="3dp" />
        </shape>
    </item>

</layer-list>

编辑:

请查看此链接,了解如何进行操作。 自定义旋转器背景

或者您也可以尝试这样做。

<RelativeLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="spinner background image">

        <Spinner       
        android:layout_width="match_parent"
        android:layout_height="match_parent"        
        android:background="@null"/>

        <ImageView 
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="arrow image" />

    </RelativeLayout>

好的,那箭头呢?是否有办法使用 <shape> 元素创建右下方的小灰色箭头? - Mansur Khan
请检查编辑并确认您的问题是否已解决。 - RIJO RV

2
以下布局将为选择器创建一个带有期望颜色下拉箭头的背景:
  <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".6"
        android:background="@drawable/edit_text_rounded_shape"
        android:gravity="center|center_vertical">
    <Spinner
        android:id="@+id/spinerComanyName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:layout_weight=".6"
        android:layout_gravity="center"

        android:entries="@array/spinner_item"
        android:spinnerMode="dropdown"
        android:theme="@style/Spinner"

        />
   </LinearLayout>

   <style name="Spinner">
    <!-- Used for the bottom line when not selected / focused -->
    <item name="colorControlNormal">@color/black</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
    <!-- colorControlActivated & colorControlHighlight use the colorAccent          color by default -->
   </style>

edit_text_rounded_shape提供背景颜色和圆角

   <?xml version="1.0" encoding="utf-8"?>

   <shape xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="rectangle" android:padding="10dp">
         <solid android:color="@color/white"/>
         <stroke android:color="@color/grey"/>
     <corners
     android:bottomRightRadius="15dp"
     android:bottomLeftRadius="15dp"
     android:topLeftRadius="15dp"
     android:topRightRadius="15dp"/>
  </shape>

1

       android:layout_alignParentRight="true"
        android:layout_width="@dimen/spinner_width"
        android:layout_height="wrap_content"
        android:id="@+id/spnLocation"
        android:entries="@array/labelFamily"
        android:layout_centerVertical="true"

        android:backgroundTint="@color/color_gray"

this work for me


1
一个定制Spinner和其他Android控件的好方法是使用Android Asset Studio网站并选择Android Holo Colors Generator。这将创建所有可能需要的资源,包括“下划线”。它还会生成实现更改的XML文件。
从该网站下载ZIP文件后,只需将图像和XML文件复制到您的项目中即可。
然后,您可以编辑所需的图像文件以删除下划线。它们是称为9-Patch文件的文件。
  • apptheme_spinner_default_holo_light.9.png
  • apptheme_spinner_disabled_holo_light.9.png
  • apptheme_spinner_focused_holo_light.9.png
  • apptheme_spinner_pressed_holo_light.9.png

如果您想了解更完整的过程,并查看一些示例XML文件,请参考我的相关答案:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接