安卓实现xml文件长方形背景颜色渐变。shape标签使用介绍。

本文详细介绍了如何使用Android中的Shape XML来定义各种图形样式,包括矩形、圆形等,并深入探讨了如何通过不同的属性如gradient、corners等来定制这些图形的表现形式。

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

一、小编使用篇。

在drawable文件夹中创建资源文件,最外层标签为shape标签,标签中设置属性shape:

先上小编个人项目简单用到的,然后做一下各个属性总结:

文件名称:shape_rectangle_gradient.xml。

具体内容:

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

    <gradient
        android:angle="270"
        android:endColor="#b5000000"
        android:startColor="#00000000" />

    <corners
        android:bottomLeftRadius="8px"
        android:bottomRightRadius="8px" />

</shape>

效果如下图:

二、小编个人讲解纪录篇。

①、大的shape标签中的android:shape

属性的值包含:

rectangle:定义长方形。

line:定义线。

oval:定义圆。

ring:定义圆环。

②、shape标签内部子标签介绍。

corners标签

1、corners标签。

<corners
    android:radius="5dp"
    android:topLeftRadius="5dp"
    android:topRightRadius="5dp"
    android:bottomLeftRadius="5dp"
    android:bottomRightRadius="5dp" />

android:radius:设置四个角的弧度分别都是后面设置的数量5dp,小编与后边的分开使用,未尝试优先级。

android:topLeftRadius:设置左上角的角度。

android:topRightRadius:设置右上角的角度。

android:bottomLeftRadius:设置左下角的角度。

android:bottomRightRadius:设置右下角的角度。

2、gradient标签。

<gradient
    android:angle="270"
    android:endColor="#b5000000"
    android:startColor="#00000000" />

android:angle:

          当angle为0时,颜色渐变方向是从左往右

          当angle为90时,颜色渐变方向是从下往上

          当angle为180时,颜色渐变方向是从右往左

          当angle为270时,颜色渐变方向是从上往下

android:startColor:开始处的颜色。

android:endColor:末尾处的颜色。

3、padding标签。小编用的较少。

<padding
    android:left="2dp"
    android:top="2dp"
    android:right="2dp"
    android:bottom="2dp"/>

4、size标签。

<size
    android:width="150dp"
    android:height="150dp"/>

可直接设置宽和高度。

5、solid标签。

<solid android:color="@color/red" />

进行颜色的填充。设置完后,颜色覆盖gradient的颜色设置。

6、stroke标签。

<stroke
    android:width="2dp"
    android:color="@android:color/black"
    android:dashGap="2dp"
    android:dashWidth="1dp" />

给外层添加线边框。

android:width:设置边框的宽度。

android:color:线的颜色。

android:dashGap:虚线的宽度。为0时候,是实现。

android:dashWidth:虚线的间隔。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值