Android add arrow image to spinner

后端 未结 5 802
梦谈多话
梦谈多话 2020-12-14 08:49

I have created a background drawable that I use for a spinner. I would like to now but an arrow image inside this background. I have tried things like android:drawableRight=

相关标签:
5条回答
  • 2020-12-14 09:04

    Create an XML in a drawable folder with any name for example spinner_bg.xml and add this following lines

    <?xml version="1.0"
          encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
        <layer-list>
          <item>
            <shape>
              <gradient android:angle="90"
                        android:endColor="#ffffff"
                        android:startColor="#ffffff"
                        android:type="linear" />
    
              <stroke android:width="1dp"
                      android:color="#504a4b" />
    
              <corners android:radius="5dp" />
    
              <padding android:bottom="3dp"
                       android:left="3dp"
                       android:right="3dp"
                       android:top="3dp" />
            </shape>
          </item>
          <item>
            <bitmap android:gravity="bottom|right"
                    android:src="@drawable/spinner_ab_default_holo_dark_am" />
            // you can use any other image here, instead of default_holo_dark_am
          </item>
        </layer-list>
      </item>
    </selector>
    

    Add the following lines to your styles.xml which is inside values folder

    <style name="spinner_style" >
            <item name="android:background">@drawable/spinner_bg</item>
            <item name="android:layout_marginLeft">10dp</item>
            <item name="android:layout_marginRight">10dp</item>
            <item name="android:layout_marginBottom">10dp</item>
            <item name="android:paddingLeft">8dp</item>
            <item name="android:paddingTop">5dp</item>
            <item name="android:paddingBottom">5dp</item>
    </style>
    

    Now add this style to your spinner as

    <Spinner android:id="@+id/spinner1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             style="@style/spinner_style"
             android:popupBackground="#cccccc" />
    
    0 讨论(0)
  • 2020-12-14 09:06

    This has worked for me:

    <Spinner
          android:id="@+id/spinner"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:theme="@style/ThemeOverlay.AppCompat.Light"
          android:spinnerMode="dropdown" />
    
    0 讨论(0)
  • 2020-12-14 09:08

    Aniruddha's solution worked fine for me, when I used JellyBean (4.2.2) or Kitkat (4.4). But unfortunately on Marshmallow (6.0) I have an exception, possible cause explained here: Using drawable resources

    I ended up just making a trick and place spinner to LinearLayout with ImageView like this:

    <LinearLayout
        android:id="@+id/spinner_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
    
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/your_arrow_drawable"/>
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-14 09:14

    This works for me. You may be missing the default state. Without posting your code it's hard to tell.

    <!-- theme -->
    <item name="android:dropDownSpinnerStyle">@style/mySpinnerStyle</item>
    
    <!-- style -->
    <style name="mySpinnerStyle" parent="android:Widget.Holo.Spinner">
          <item name="android:background">@drawable/mySpinner_background</item>
    </style>
    
    <!-- mySpinner_background -->
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="false"
              android:drawable="@drawable/mySpinner_disabled" />
        <item android:state_pressed="true"
              android:drawable="@drawable/mySpinner_pressed" />
        <item android:state_pressed="false" android:state_focused="true"
              android:drawable="@drawable/mySpinner_focused" />
        <item android:drawable="@drawable/mySpinner_default" />
    </selector>
    
    0 讨论(0)
  • 2020-12-14 09:17

    Set your Image ICON On Spinner :

    <item>
    
        <layer-list>
    
            <item>
                <color android:color="@android:color/white" />
            </item>
    
            <item>
                <bitmap android:gravity="center_vertical|right" android:src="@drawable/down_arrow" />
            </item>
    
        </layer-list>
    
    </item>
    

    0 讨论(0)
提交回复
热议问题