Android 将箭头图像添加到微调器

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

Android add arrow image to spinner

androidandroid-layoutimageviewandroid-spinner

提问by user3781214

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="arrowimage", but this does not seem to show anything. Does anyone know how I can include an image in my custom background for a spinner?

我创建了一个用于微调器的背景可绘制对象。我现在想在这个背景中添加一个箭头图像。我尝试过类似 android:drawableRight="arrowimage" 的方法,但这似乎没有显示任何内容。有谁知道如何在我的自定义背景中为微调器添加图像?

回答by Aniruddha

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

在 drawable 文件夹中创建一个具有任何名称的 XML,例如spinner_bg.xml并添加以下几行

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

将以下行添加到您的styles.xml 中,它位于values 文件夹中

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

回答by takahawk

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

当我使用 JellyBean (4.2.2) 或 Kitkat (4.4) 时,Aniruddha 的解决方案对我来说效果很好。但不幸的是,在 Marshmallow (6.0) 上我有一个例外,可能的原因在这里解释:使用可绘制资源

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

我最终只是制作了一个技巧并将微调器放置到带有 ImageView 的 LinearLayout 中,如下所示:

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

回答by Rob

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>

回答by siddharth patel

Set your Image ICON On Spinner :

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

回答by Jasmine John

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