首页 > 解决方案 > 如何从微调器中的可绘制设置下拉箭头?

问题描述

我正在尝试将可绘制设置为 Spinner 中的箭头,但不支持位图。

我使用了 svg 文件(ic_arrow.xml)并将其设置在spinner_arrow.xml文件中:

<layer-list  xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <padding
                android:left="4dp"
                android:top="4dp"
                android:right="4dp"
                android:bottom="4dp"
                />

            <solid android:color="@color/white" />
            <stroke android:width="2px" android:color="@color/grey" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <bitmap android:gravity="center|right" android:src="@drawable/ic_arrow"/>
    </item>
</layer-list>

所以我用它作为布局文件的背景

<Spinner
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/spinner_arrow"/>

但我在显示 Spinner 时出错:“我的应用程序中的二进制 XML 文件第 41 行:layout/activity_registration_driver:错误膨胀类 Spinner”。

我认为问题在于在微调器中添加可绘制箭头,但我不知道如何解决它

标签: javaandroidspinnerdrawable

解决方案


请使用以下代码添加带有微调器的箭头:

<RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="@dimen/btn_height_common"
                android:layout_marginLeft="@dimen/_15sdp"
                android:layout_marginTop="@dimen/_5sdp"
                android:layout_marginRight="@dimen/_15sdp"
                android:background="@drawable/bg_editext">

                <Spinner
                    android:id="@+id/sp_gender"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@null"
                    android:entries="@array/gender"
                    android:fontFamily="@font/gotham_narrow_book"
                    android:overlapAnchor="false"
                    android:paddingLeft="@dimen/_10sdp"
                    android:paddingRight="@dimen/_10sdp"
                    android:textColor="@color/gray_text_color"
                    android:textSize="@dimen/normal_textsize" />

                <ImageView
                    android:layout_width="@dimen/_15sdp"
                    android:layout_height="@dimen/_15sdp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="@dimen/_15sdp"
                    android:padding="@dimen/_2sdp"
                    android:src="@mipmap/ic_down_gray" />

            </RelativeLayout>

输出:

在此处输入图像描述


推荐阅读