如何使用图像下划线在微调器中设置下拉箭头

时间:2016-09-22 06:49:16

标签: android

  1. 列表项
  2. enter image description here

    我的代码是风格

    这是在style.xml

    <style name="SpinnerTheme">
            <item name="android:background" >@drawable/bg_spinner</item>
        </style>
    

    bg_spinner.xml

    <item>
    
        <layer-list>
    
            <item >
                <bitmap android:gravity="bottom" android:src="@drawable/under_line" />
            </item>
    
            <item android:right="5dp">
                <bitmap android:gravity="center_vertical|right" android:src="@drawable/dropdown" />
    
            </item>
    
        </layer-list>
    
    </item>
    

    <Spinner
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/SpinnerTheme"
                android:layout_marginTop="15dp"
                android:hint="Sex"
                android:id="@+id/spinnerSex" />
    

    日期,月份和年份是全宽度,背景图像也是宽度较小,但是当它出现性别时它没有显示全宽。 我需要性别全宽的底线,请做必要的

2 个答案:

答案 0 :(得分:2)

您可以通过xml代码绘制线条。如下所示

    <item >

    <layer-list>

        <item>
            <shape android:shape="rectangle" >
                <solid android:color="@android:color/transparent" />
                <!-- background color of box -->
            </shape>
        </item>
        <item
            android:left="-2dp"
            android:right="-2dp"
            android:top="-2dp">
            <shape>
                <solid android:color="@android:color/transparent" />

                <stroke
                    android:width="1dp"
                    android:color="#323232" />
                <!-- color of stroke -->
            </shape>
        </item>

        <item android:right="5dp">
            <bitmap android:gravity="center_vertical|right" android:src="@drawable/downlarrow" />

        </item>

    </layer-list>

   </item>

输出看起来像这样 enter image description here

答案 1 :(得分:0)

使用以下代码替换xml代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <!-- Draw a 2dp width border around shape -->
        <stroke
            android:color="#000000"
            android:width="2dp"
            />
    </shape>
</item>
<!-- Overlap the left, top and right border using background color  -->
<item
    android:bottom="2dp"
    >
    <shape android:shape="rectangle">
        <solid android:color="#ffffff"/>
    </shape>
</item>