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

时间:2014-09-16 11:27:08

标签: android spinner android-spinner

我已经读过几件关于它的事情,但我无法找到我需要的东西。 我想保留灰色箭头,但我想从默认样式中删除水平条并具有白色背景。你知道我怎么做吗?

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

enter image description here

这就是我想要的:

enter image description here

11 个答案:

答案 0 :(得分:64)

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

我们不必在这种情况下添加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>

这是截图

在: enter image description here

在: enter image description here

答案 1 :(得分:27)

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

 <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 :(得分:22)

一个简单的解决方案,不要求你为箭头创建自己的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>

答案 3 :(得分: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。

答案 4 :(得分:2)

嗨,而不是围绕Parent Layouts包装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  
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:padding="5dp"
     android:background="@drawable/spinner_bg" />

答案 5 :(得分:1)

自定义微调器和任何其他Android控件的好方法是使用Android Asset Studio网站并选择Android Holo Colors Generator。这将创建您可能需要的所有资产,包括&#34;下划线&#34;。它还会生成实现更改的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文件,请参阅我的相关答案:

答案 6 :(得分: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"

这项工作对我来说

答案 7 :(得分:1)

下面的布局将在旋转器中创建一个带有欲望颜色下拉箭头的背景

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

答案 8 :(得分:1)

总是在使用微调器,按钮和EditTexts时,并且还需要插入可绘制对象时,我经常将元素(微调器,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>

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

答案 9 :(得分:0)

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

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

修改

请查看此链接,该链接可让您了解相关信息。 Customize spinner background

OR

你可以做这样的事情。

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

答案 10 :(得分:0)

我认为不进行复杂布局的最佳方法是:

使用此xml作为您的微调框背景,您就可以上手了!!!

setState