在Android的drawable资源开发中,layer-list可以将多个图形图层按顺序叠加,每个图层的位置和尺寸可以通过item标签的属性进行配置,其中bottom属性就是用来控制当前图层距离layer-list整体底部的内边距距离。

layer-list基本结构
layer-list的根标签是<layer-list>,内部可以包含多个<item>标签,每个item代表一个独立的图层,图层会按照声明的顺序从下到上叠加显示。item标签支持top、bottom、left、right四个位置属性,用来设置图层相对于layer-list边界的偏移量。
bottom属性的作用
bottom属性表示当前item对应的图层距离layer-list底部的距离,单位是dp,数值越大,图层距离底部越远,在垂直方向上会向上偏移。如果同时设置了top和bottom属性,图层的高度会被拉伸以适配这两个属性的限制。
基础使用示例
以下是一个简单的layer-list配置,包含两个图层,第二个图层通过bottom属性控制距离底部的距离:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 底层背景,蓝色全屏 -->
<item>
<shape android:shape="rectangle">
<solid android:color="#FF4081" />
</shape>
</item>
<!-- 上层图层,白色,距离底部30dp -->
<item
android:bottom="30dp"
android:left="20dp"
android:right="20dp"
android:top="20dp">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
上述代码中,第二个item的bottom设置为30dp,意味着该白色矩形图层底部距离整个layer-list的底部有30dp的间距,左右和上部分别有20dp的间距,最终白色矩形会在蓝色背景上方,四周都有对应的内边距。
单独使用bottom属性的效果
如果只设置bottom属性,不设置top、left、right,图层会默认占满除了底部间距之外的所有空间:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#3F51B5" />
</shape>
</item>
<!-- 只设置bottom,图层顶部和左右都贴边 -->
<item android:bottom="50dp">
<shape android:shape="rectangle">
<solid android:color="#FFEB3B" />
</shape>
</item>
</layer-list>
此时黄色图层会占满整个layer-list除了底部50dp的区域,顶部和左右两侧都与layer-list边界对齐。
注意事项
- bottom属性的数值必须是带单位的有效尺寸,比如30dp,不能直接写数字,否则会编译报错。
- 如果bottom的数值超过了layer-list的高度,该图层会被完全挤出可视区域,无法显示。
- bottom属性和top属性同时设置时,图层高度会被强制拉伸为layer-list高度减去top和bottom的数值之和,即使图层内容本身不需要这么高。
- 如果需要引用其他drawable作为图层内容,可以在item内部使用
<bitmap>或者<shape>等标签,bottom属性依然生效。
常见使用场景
bottom属性常用于实现底部有留白的背景效果,比如卡片背景底部需要留出空间放置阴影或者装饰元素,或者页面背景顶部是渐变色,底部需要留出固定距离的纯色区域等场景,通过配置bottom属性可以快速实现这类效果,不需要额外嵌套布局控制位置。
Androidlayer-listitem_bottomXML_drawable修改时间:2026-07-11 09:48:12