自定义带有圆角、描边和选择器图标的下拉列表控件

9

我希望我的Spinner有一个黑色渐变背景,左侧是白色文本,右侧是选择器图标(一个向下的白色三角形)。从我的看法来看,有两种方法可以实现这个效果:

  1. If I set the background to an xml drawable resource I can make my Spinner look perfect, but then I need to somehow add the white triangle on the right, which I don't know how to go about:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:startColor="#343434"
                android:endColor="#171717"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#ffffff" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="3dp"
                android:top="3dp"
                android:right="3dp"
                android:bottom="3dp" />
        </shape>
    </item>
    </selector>
    
  2. I create a 9-patch image which includes the triangle and then use xml to round the corners and add a stroke to the image. I have had a go at this but was unable to make it work:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/spinnerblack" >
            <stroke
                android:width="1dp"
                android:color="#ffffff" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="3dp"
                android:top="3dp"
                android:right="3dp"
                android:bottom="3dp" />
        </item>
    </selector>
    

有人能告诉我如何实现方法1,或者在方法2中我做错了什么吗?我不想在我的9-patch图像中添加描边和圆角,因为我认为效果不太好。另外,我更喜欢方法1而不是方法2。非常感谢任何帮助。

1个回答

13

我的应用程序中做过类似于方法1的事情。基本上,您需要将选择器与一个图层列表结合起来:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <item>
                <shape>
                    <gradient
                        android:startColor="#343434"
                        android:endColor="#171717"
                        android:angle="270" />
                    <stroke
                        android:width="1dp"
                        android:color="#ffffff" />
                    <corners
                        android:radius="4dp" />
                    <padding
                        android:left="3dp"
                        android:top="3dp"
                        android:right="3dp"
                        android:bottom="3dp" />
                </shape>
            </item>
            <item
                android:top="12dp"
                android:right="15dp">
                <bitmap android:src="@drawable/arrow_bitmap"
                        android:gravity="top|right" />
            </item>
        </layer-list>
    </item>
</selector>

在我的xml中,我还添加了第三层,其中包含一个不可见的<shape>(即其alpha设置为0),但添加了填充。


谢谢!按照这种方法,我需要使用位图吗?因为我想要一个纯白的三角形,我需要它具有透明背景,所以想要使用.png。我会尝试一下,看看能得到什么结果。 - Matt Harris
@Aaron Klotz 在我的情况下,箭头图像被绘制在顶部|右侧。相反,我想看到带有右填充的箭头图像,但不起作用。 - Narendra Pal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接