Android 更改微调器背景颜色但保留箭头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25867539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:46:44  来源:igfitidea点击:

Change spinner background color but keep arrow

androidspinnerandroid-spinner

提问by Mansur Khan

I've read several thing about it but I can't find what I need. I want to keep the grey arrow but I want to remove the horizontal bar from the default style and have a white background. Do you have any idea of how I can do this ?

我已经阅读了一些关于它的东西,但我找不到我需要的东西。我想保留灰色箭头,但我想从默认样式中删除水平条并使用白色背景。你知道我该怎么做吗?

Here is what I have now (default spinner style) :

这是我现在拥有的(默认微调样式):

enter image description here

在此处输入图片说明

Here is what I want :

这是我想要的:

enter image description here

在此处输入图片说明

采纳答案by Mansur Khan

For the record, I found an easy solution : Wrap your spinner in a relative layout and add an image :

作为记录,我找到了一个简单的解决方案:将您的微调器包裹在相对布局中并添加一个图像:

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

回答by Chandler

I did a little modification based on @Mansur Khan 's answer.

我根据@Mansur Khan 的回答做了一些修改。

We don't have to add an ImageView in this case because the spinner already has a triangle arrow. So check the code below:

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

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

Here is the screenshot

这是屏幕截图

Before: enter image description here

前: 在此处输入图片说明

After: enter image description here

后: 在此处输入图片说明

回答by Tad

A simple solution that doesn't require you to create your own drawable for the arrow is to wrap the spinner with a RelativeLayout, and set the background color in the RelativeLayout, not the spinner:

一个不需要您为箭头创建自己的可绘制对象的简单解决方案是用 a 包裹微调器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>

回答by Kishan Dhamat

Use this:

用这个:

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));
}

and your class should implement OnItemSelectedListener.

并且您的类应该实现 OnItemSelectedListener。

回答by Namrata Bagerwal

Hi instead of wrapping Spinner component around Parent Layouts like LinearLayout, RelativeLayout etc which increases layout parsing simply create a drawable called spinner_bg.xml under drawable folder.

嗨,而不是将 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>

Set spinner_bg as the background of your spinner and it works like charm:

将 spinner_bg 设置为微调器的背景,它的作用就像魅力一样:

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

回答by Hadi Note

I think the best way without doing complex layoutsis this:

我认为不做复杂布局的最好方法是:

Use this xml for your spinner background and you are good to go!!!

将此 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>

回答by Fellipe Tavares

Always while working with spinners, buttons and EditTexts and also needing to insert a drawable, I often wrap the element (spinner, EditText etc.) in a FrameLayout. Check an example:

在使用微调器、按钮和 EditText 并且还需要插入可绘制对象时,我经常将元素(微调器、EditText 等)包装在 FrameLayout 中。检查一个例子:

<FrameLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content">

                                <Spinner
                                    android:id="@+id/spinner_component"                                
                                    android:layout_width="match_parent"
                                    android:layout_height="45dp"
                                    android:background="@drawable/your_rectangle_style"
                                    android:textColor="@color/your_text_color" />

                                <ImageView
                                    android:layout_width="11dp"
                                    android:layout_height="9dp"
                                    android:src="@drawable/arrow_spinner"
                                    android:layout_gravity="right|center_vertical"
                                    android:layout_marginRight="7dp" />
                            </FrameLayout>

Another important tip is that FrameLayout allows you to set OnClick functions on the drawable, for instance.

另一个重要提示是 FrameLayout 允许您在可绘制对象上设置 OnClick 函数,例如。

回答by Sumit Kumar

below layout will create a background in spinner with desire color drop down arrow

下面的布局将在微调器中创建一个带有所需颜色下拉箭头的背景

  <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 which provide background color and rounded corner

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>

回答by RIJO RV

Here is the code of custom spinner. Please check it out and tell me. Not yet I tested this. So please inform me after checking this whether it solves your problem.

这是自定义微调器的代码。请检查并告诉我。我还没有测试过这个。因此,请在检查后通知我是否解决了您的问题。

<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" />

Here is the drawable(spinner_background.xml) to create the background of spinner.

这是用于创建微调器背景的 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>

Edit:

编辑:

please check this link which gives you an idea to do. Customize spinner background

请检查此链接,它为您提供了一个想法。 自定义微调背景

OR

或者

you can do something like this.

你可以做这样的事情。

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

回答by Richard Le Mesurier

A good way to customise spinners and any other Android controls is to use the Android Asset Studiosite and choose the Android Holo Colors Generator. This will create all the assets you might need, including the "underline". It also generates the XML files that implement the changes.

自定义微调器和任何其他 Android 控件的一个好方法是使用Android Asset Studio站点并选择Android Holo Colors Generator。这将创建您可能需要的所有资产,包括“下划线”。它还生成实现更改的 XML 文件。

Once you download the ZIP file from that site, you just copy the images and XML files into your project.

从该站点下载 ZIP 文件后,您只需将图像和 XML 文件复制到您的项目中。

You can then edit the required image files to remove the underline. They are 9-Patch files, called:

然后,您可以编辑所需的图像文件以删除下划线。它们是 9 个补丁文件,称为:

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


For a more complete explanation of the process, and to see some of the sample XML files, please refer to my related answer:

有关该过程的更完整说明,以及查看一些示例 XML 文件,请参阅我的相关答案: