Spinner with arrow in left

2020-03-10 05:37发布

问题:

I'm developing an app for RTL language and want to change the position of arrow to the left of Spinner ! Is there anyway to do this without creating a custom spinner ?

回答1:

You must write a custom spinner. Sample code is below. You can edit as you wish.

<Spinner
            style="@style/spinner_style"
            android:layout_width="match_parent"
            android:layout_height="wrap content"
             />

@style/spinner_style:

 <style name="spinner_style">
    <item name="android:background">@drawable/background_spinner</item>
</style>

@drawable/background_spinner

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item><layer-list>
        <item><shape>
               <solid android:color="@android:color/white" />
                <corners android:radius="4dp" />
                <padding android:left="8dp"/>
            </shape></item>
        <item><bitmap android:gravity="left"
              android:src="@drawable/ic_arrow_drop_down_grey600_36dp"/>                
        </item>
    </layer-list></item>
</selector>


回答2:

An easy and simple way to set arrow in left side is,You must create .xml file in drawable folder for that as written below:

<?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="@color/colorWhite" />
        </shape>
   </item>
   <item
        android:drawable="@drawable/ic_arrow_drop_down_black_24dp"
        android:right="320dp"/>
</layer-list>

You can adjust above code as your need. And set this file as background of a spinner widget as below:

<Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout_spinner"
        android:background="@drawable/arrow_left"
        android:elevation="5dp"/>


回答3:

This worked in my case. I have English and Arabic languages in my app

<Spinner
    android:textDirection="locale"
    android:id="@+id/spnGender"
    style="@style/Base.Widget.AppCompat.Spinner.Underlined"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/array_gender"/>

This attribute is important.

android:textDirection="locale"

NOTE : I have used the default spinner without any customizations.



回答4:

dummy.xml (drawable should be of very low size-24dp)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list android:opacity="transparent">
            <item android:width="100dp" android:end="300dp">
                <bitmap android:src="@drawable/down_button_dummy_dummy" android:gravity="center"/>
            </item>
        </layer-list>
    </item>
</selector>

layout file snippet

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardUseCompatPadding="true"
        app:cardElevation="5dp"
        >
     <Spinner
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@drawable/dummy">

     </Spinner>
    </android.support.v7.widget.CardView>

Click here for rendered layout



回答5:

What about this ? First of all make sure that you enabled rtl support in AndroidManifest.xml

<application
...
android:supportsRtl="true"
...>

after that use this in your Spinner definition :

android:layoutDirection="rtl"
android:textDirection="rtl"