Android Layer-List Drawable有两个项目未正确绘制且尺寸错误

时间:2017-01-03 08:17:26

标签: android xml android-layout drawable layer-list

我创建了一个简单的图层列表drawable,我想用它作为指标天气或不是设备在线。可拉伸件由外圈和内圈组成:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:width="14dp"
        android:height="14dp"
        android:gravity="center">
        <shape android:shape="oval">
            <stroke
                android:width="1dp"
                android:color="@color/colorLightGray" />
        </shape>
    </item>
    <item
        android:width="10dp"
        android:height="10dp"
        android:gravity="center">
        <shape android:shape="oval">
            <solid android:color="@color/colorLightGray" />
        </shape>
    </item>
</layer-list>

enter image description here

在我的布局xml中,当我将其指定为图像视图的源时,drawable似乎是正确的:

enter image description here

但是,如果我在设备上运行代码,它看起来像这样:

enter image description here

此外,我必须在布局中设置宽度和高度,否则图像根本无法显示。

问题:

  • 我该怎么做才能在布局xml中使用widht / height的wrap_content并在图层列表中配置尺寸?

2 个答案:

答案 0 :(得分:1)

  

试试这个layerlist drawable:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="center">
        <shape android:shape="oval">
            <size
                android:width="14dp"
                android:height="14dp" />
            <stroke
                android:width="1dp"
                android:color="@color/colorLightGray" />
        </shape>
    </item>
    <item
        android:bottom="3dp"
        android:left="3dp"
        android:right="3dp"
        android:top="3dp">
        <shape android:shape="oval">
            <size
                android:width="10dp"
                android:height="10dp" />
            <solid android:color="@color/colorLightGray" />
        </shape>
    </item>
</layer-list>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用此:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="oval">
            <solid
                android:color="@android:color/transparent"/>
            <padding
                android:bottom="4dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp"/>
            <stroke
                android:width="1dp"
                android:color="@color/colorLightGray"/>
        </shape>
    </item>
    <item>
        <shape
            android:shape="oval">
            <solid
                android:color="@color/colorLightGray"/>
            <size
                android:width="10dp"
                android:height="10dp"/>
        </shape>

    </item>
</layer-list>