如何设置图层列表的宽度整体?

时间:2017-02-05 17:05:09

标签: android xml android-drawable xml-drawable

如何在使用layer-list时指定XML drawable的宽度?我希望drawable的宽度(作为一个整体)设置为20dp,但我不知道在哪里/如何设置这个+预览窗格也没有那么多帮助。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/blue" />
        </shape>
    </item>
    <item android:left="8dp" android:right="8dp">
        <shape>
            <solid android:color="#fff" />
        </shape>
    </item>
</layer-list>

enter image description here

2 个答案:

答案 0 :(得分:2)

你可以试试这个:

    <shape>
        <solid android:color="#fff"/>
        <size android:height="10dp" android:width="20dp"/>
    </shape>

答案 1 :(得分:1)

良好做法是不直接在文件中配置XML drawable的大小。您应该在布局文件中设置视图的大小。例如,如果代码中有一些按钮并且可绘制shape.xml,则代码可能如下所示:

<Button 
 .....
  android:drawable="@drawable/shape" >
<Button> 

但如果您仍然不想设置尺寸,可以使用此代码:

<size android:width="60dp"
    android:height="40dp" />