了解Android的<层列表> [英] Understanding Android's <layer-list>

查看:169
本文介绍了了解Android的<层列表>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白层名单是如何工作的。我读了官方文档的一些范例但它确实对我来说像预期无法正常工作。我想要四个正方形,应与1DP补齐,但没有什么是一样的预期。下面是截图比例由500%:


(错误的颜色并不重要)
正如你所看到的大小是完全错误的,垫丢失。我试图设置类似宽度/高度和左/右/上/ BUTTOM实际值,以确保机器人得到我想要的地步。

下面是我的xml:

 < XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目的android:顶部=0dp机器人:左边=0dp安卓底=0dp机器人:右=0dp>
        <形机器人:形状=矩形>
            <大小机器人:宽=9dp
                机器人:身高=9dp/>
            [固体机器人:颜色=#F000/>
        < /形状>
    < /项目>

    <项目的android:顶部=1DP机器人:左边=1DP安卓底=5DP机器人:右=5DP>
        <形机器人:形状=矩形>
            <大小机器人:宽=3DP
                机器人:身高=3DP/>
            [固体机器人:颜色=#F00/>
        < /形状>
    < /项目>
    <项目的android:顶部=1DP机器人:左边=5DP安卓底=5DP机器人:右=1DP>
        <形机器人:形状=矩形>
            <大小机器人:宽=3DP
                机器人:身高=3DP/>
            [固体机器人:颜色=#0f0/>
        < /形状>
    < /项目>

    <项目的android:顶部=5DP机器人:左边=1DP安卓底=1DP机器人:右=5DP>
        <形机器人:形状=矩形>
            <大小机器人:宽=3DP
                机器人:身高=3DP/>
            [固体机器人:颜色=#0f0/>
        < /形状>
    < /项目>
    <项目的android:顶部=5DP机器人:左边=5DP安卓底=1DP机器人:右=1DP>
        <形机器人:形状=矩形>
            <大小机器人:宽=3DP
                机器人:身高=3DP/>
            [固体机器人:颜色=#F00/>
        < /形状>
    < /项目>
< /层列表>
 

解决方案

左,上,右,下是从它们各自的边缘测得的值。

那么剩下= 0dp,顶= 0dp,底部= 0dp和放大器;右= 50dp会给你一个矩形,它是(match_parent - 50dp)宽,而不是50dp宽。对于正确的,实际上给你一个小矩形,因此更大的值

这同样适用于其他的值,但这些将像预期的那样,在大多数情况下,它只是右,可能会引起混淆。

I don't understand how the layer-lists work. I read the official documentation with some examples but it does not work for me like expected. I want four squares which should be padded with 1dp, but nothing is like expected. Here is a screenshot scaled by 500%:


(The wrong colors does not matter)
As you can see the size is completely wrong and the paddings are missing. I tried to set real values like width/height and right/left/top/buttom to be sure that android get the point what I want.

Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
        <shape android:shape="rectangle">
            <size android:width="9dp"
                android:height="9dp"/>
            <solid android:color="#f000"/>
        </shape>
    </item>

    <item android:top="1dp" android:left="1dp" android:bottom="5dp" android:right="5dp">
        <shape android:shape="rectangle">
            <size android:width="3dp"
                android:height="3dp"/>
            <solid android:color="#f00"/>
        </shape>
    </item>
    <item android:top="1dp" android:left="5dp" android:bottom="5dp" android:right="1dp">
        <shape android:shape="rectangle">
            <size android:width="3dp"
                android:height="3dp"/>
            <solid android:color="#0f0"/>
        </shape>
    </item>

    <item android:top="5dp" android:left="1dp" android:bottom="1dp" android:right="5dp">
        <shape android:shape="rectangle">
            <size android:width="3dp"
                android:height="3dp"/>
            <solid android:color="#0f0"/>
        </shape>
    </item>
    <item android:top="5dp" android:left="5dp" android:bottom="1dp" android:right="1dp">
        <shape android:shape="rectangle">
            <size android:width="3dp"
                android:height="3dp"/>
            <solid android:color="#f00"/>
        </shape>
    </item>
</layer-list>

解决方案

The values for left, top, right and bottom are measured from their respective edge.

So left=0dp, top=0dp, bottom=0dp & right=50dp will give you a rectangle that is (match_parent - 50dp) wide and not 50dp wide. Therefore larger values for "right" will actually give you a smaller rectangle.

The same would apply to the other value, but these would behave as expected in most cases, its just "right" that might cause confusion.

这篇关于了解Android的&LT;层列表&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆