Android资源文件-Shape

本文介绍使用XML进行图形绘制的各种技巧,包括纯色填充、圆角、渐变色、边框及阴影效果等。通过实例展示了如何利用不同的XML元素来实现这些视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. solid
    这里写图片描述
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!--绿色填充-->
    <solid android:color="#0f0" />

</shape>

2.corner
这里写图片描述

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#0f0" />
    <!--半径为10px的圆角-->
    <corners android:radius="10px" />

</shape>

3.gradient
这里写图片描述

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#0f0" />

    <corners android:radius="10px" />
     <!--渐变色:开始的颜色,中间的颜色,结束的颜色-->
    <gradient
        android:startColor="#D8FC83"
        android:centerColor="#41F114"
        android:endColor="#0f0"
        />

</shape>

4.stroke
这里写图片描述

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#0f0" />

    <corners android:radius="10px" />

    <gradient
        android:centerColor="#41F114"
        android:endColor="#0f0"
        android:startColor="#D8FC83" />
    <!--边框:宽度,颜色-->
    <stroke
        android:width="1px"
        android:color="#f0f" />

</shape>

5.阴影效果
这里写图片描述

需要3个图层:
a:最底层和整体背景颜色一致
b:中间层left和top错开2dp
c:最上层和阴影颜色一致的边框,和最底层一样颜色的填充色,bottom和right方向错开2dp
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <!--最底层-->
    <item>
        <shape>
            <solid android:color="#fff" />
        </shape>
    </item>
  <!--中间层-->
    <item
        android:left="2dp"
        android:top="2dp">
        <shape>
            <solid android:color="#ccc" />
        </shape>
    </item>
  <!--最上层-->
    <item
        android:bottom="2dp"
        android:right="2dp">
        <shape>
            <solid android:color="#fff" />

            <stroke
                android:width="1px"
                android:color="#ccc" />
        </shape>
    </item>
</layer-list>

6.底部边框颜色和其它边框颜色不同。
图层1-最底部–>图层2-中间–>图层3-最上边

a:最底层黄色填充色。
b:中间层灰色边框1px,白色填充色,bottom 5px,可以露出5px的底层的黄色。
c:最上层left,right,top透明边框1px,白色填充色,正好显示出中间层的left,right,top边框。bottom 4px正好覆盖中间层的底部边框。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!--第一层-->
    <item>
        <shape>
            <solid android:color="#ff0" />
        </shape>
    </item>
<!--第二层-->
    <item android:bottom="5px">
        <shape>
            <solid android:color="#fff" />
            <stroke
                android:width="1px"
                android:color="#ccc" />
        </shape>
    </item>
为了去掉底部灰色边框,再覆盖一个图层。(白色背景,透明边框,和第二层边框的width基本一致,但bottom的值一定要小于第二层的bottom值)
<!--第三层-->
    <item
        android:bottom="4px"
        android:left="1px"
        android:right="1px"
        android:top="1px">
        <shape>
            <solid android:color="#fff" />
            <stroke
                android:width="1px"
                android:color="#0000" />
        </shape>
    </item>
</layer-list>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值