如何在 Android 的 Spinner 视图中添加左可绘制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23609291/
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 07:18:49  来源:igfitidea点击:

How to add left drawable in Spinner View in Android

androidandroid-layoutandroid-spinner

提问by N Sharma

Hello I am trying to add left drawable in Spinnerbut I didn't find any propery for this as you do same in EditTextusing android:drawableLeft="@drawable/password_drawable". Is there any correct way to achieve same in Spinnerin Android.

您好,我正在尝试添加 left drawable,Spinner但我没有找到任何属性,因为您在EditText使用android:drawableLeft="@drawable/password_drawable". Spinner在Android中是否有任何正确的方法可以实现相同的目标。

Here in my case there should be left drawable only this state which I gave in the screenshot, when user click on the Spinner I don't want to have left drawable there in open drop down list in Spinner

在我的情况下,应该只留下我在屏幕截图中给出的这个状态,当用户点击微调器时,我不想在打开的下拉列表中留下可绘制 Spinner

I'm trying like this where there is no such attribute

我正在尝试这样没有这样的属性

<Spinner
    android:id="@+id/selectSpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/sign_up_views_vertical_top_margin"
    android:background="@drawable/edittext_border"
     />

I want something like below screenshot

我想要像下面的截图

enter image description here

在此处输入图片说明

Currently it is looking like below

目前它看起来像下面

enter image description here

在此处输入图片说明

and when I set the background of the Spinnerthen it looks like

当我设置背景时,Spinner它看起来像

<Spinner
    android:id="@+id/selectSpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/sign_up_views_vertical_top_margin"
    android:background="@drawable/selectone_drawable"
    android:entries="@array/select_type" />

enter image description here

在此处输入图片说明

Thanks in advance

提前致谢

采纳答案by Mark Buikema

Create a nine-patch image and set it as the background of the Spinner, then add a left padding, should do the trick.

创建一个九块图像并将其设置为微调器的背景,然后添加一个左填充,应该可以解决问题。

回答by user829589

create a xml drawable @drawable/gradient_spinner `

创建一个 xml drawable @drawable/gradient_spinner `

<?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="@color/myTextPrimaryColor" android:startColor="@color/myTextPrimaryColor" android:type="linear" />

            <stroke android:width="1dp" android:color="@color/bg_eo" />

            <corners android:radius="4dp" />

            <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
        </shape></item>
        <item ><bitmap android:gravity="right|center" android:src="@drawable/expnd" />
        </item>
    </layer-list></item>

</selector> `

set spinner background android:background="@drawable/gradient_spinner"

设置微调背景 android:background="@drawable/gradient_spinner"

回答by Manmohan Kumar

Try this,

尝试这个,

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
           if(view!=null) {
               ((TextView) view).setPadding(0, 0, 0, 0);//remove extra padding
               ((TextView)view) .setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(context, R.drawable.your_image), null, null, null);
               ((TextView) view).setCompoundDrawablePadding(20);// to set drawablePadding
           }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

回答by RaviTejaSiddabattuni

layout looks like this

布局看起来像这样

dummy.xml

虚拟文件

<?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:gravity="left" android:width="100dp" 
            android:end="300dp">
                <shape>
                    <solid android:color="#269cf7">

                    </solid>
                </shape>
            </item>
            <item android:gravity="right" android:width="300dp" android:start="100dp">
                <shape>
                    <solid android:color="@color/transparent">

                    </solid>
                </shape>
            </item>
            <item android:width="100dp" android:end="300dp">
                <bitmap android:src="@drawable/arrow_bottom" android:gravity="center"/>
            </item>
        </layer-list>
    </item>
</selector>

in layout file add this code

在布局文件中添加此代码

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

回答by TheLittleNaruto

As Mark Said, you should put the following image as a background of the spinner and take required left padding so that the text will be right after the arrow image.

正如马克所说,您应该将以下图像作为微调器的背景并采用所需的左填充,以便文本位于箭头图像之后。

enter image description here

在此处输入图片说明

Following is some code to make you understand more ;)

以下是一些让您了解更多的代码;)

<Spinner
    android:id="@+id/selectSpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/sign_up_views_vertical_top_margin"
    android:background="@drawable/the_attached_drawable"
    android:paddingLeft="@dimen/required_padding_for_each_screen_size"
    android:entries="@array/select_type" />