Android使用shape属性绘制边框内渐变色

目录

先上效果图

这是使用AndroidStudio绘制的带有渐变色的边框背景色

实现方法

项目中由于UI设计需求,需要给按钮、控件设置带有背景色效果的。以下是UI效果图。

这里我们使用shape属性来绘制背景效果。

shape属性介绍

复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] > // 定义形状
    <corners //圆角属性
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient //渐变属性
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding //边距属性
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size //大小属性
        android:width="integer"
        android:height="integer" />
    <solid //填充属性
        android:color="color" />
    <stroke //描边属性
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

Shape可以定义控件的一些展示效果,例如圆角,渐变,填充,描边,大小,边距;shape子标签就可以实现这些效果,shape子标签有下面几个属性:
corners,
gradient,
padding,
size,
solid,
stroke:
corners
(圆角)

代码

layer-list 是用来创建 图层列表的,通过它能创建出一些特殊的 drawable

复制代码
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--顶部的渐变色-->
    <item
        android:gravity="top">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient
                android:type="linear"
                android:angle="90"
                android:startColor="#0077b3c7"
                android:endColor="#9077b3c7"
                android:useLevel="false"/>

            <size
                android:width="100dp"
                android:height="10dp" />
        </shape>
    </item>

    <!--左侧的渐变色-->
    <item
        android:gravity="left">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient
                android:type="linear"
                android:angle="0"
                android:startColor="#9077b3c7"
                android:endColor="#0077b3c7"
                android:useLevel="false"/>
            <size
                android:width="10dp"
                android:height="100dp" />
        </shape>
    </item>

    <!--右侧的渐变色-->
    <item
        android:gravity="right">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android">
            <gradient
                android:type="linear"
                android:angle="180"
                android:startColor="#9077b3c7"
                android:endColor="#0077b3c7"
                android:useLevel="false"/>

            <size
                android:width="10dp"
                android:height="100dp"/>
        </shape>
    </item>

    <!--底部的渐变色-->
    <item
        android:gravity="bottom">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android">
            <gradient
                android:type="linear"
                android:angle="90"
                android:centerX="0"
                android:centerY="0"
                android:startColor="#9077b3c7"
                android:endColor="#0077b3c7"
                android:useLevel="false"/>

            <size
                android:width="100dp"
                android:height="10dp" />
        </shape>
    </item>

    <!--边框线-->
    <item>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/button_text_color"/>
        </shape>
    </item>
</layer-list>

绘制完毕后,直接到代码中引用即可

结果

相关推荐
Kapaseker5 小时前
你不看会后悔的2025年终总结
android·kotlin
alexhilton8 小时前
务实的模块化:连接模块(wiring modules)的妙用
android·kotlin·android jetpack
航Hang*9 小时前
Photoshop 图形与图像处理技术——第8章:图像的色彩与色彩调整和图像的输出与优化
图像处理·笔记·ui·photoshop
ji_shuke9 小时前
opencv-mobile 和 ncnn-android 环境配置
android·前端·javascript·人工智能·opencv
小桥流水---人工智能9 小时前
风电机组故障诊断与状态监测方法的研究局限性整理(背景笔记)
笔记
菩提小狗10 小时前
小迪安全笔记_第4天|扩展&整理|30+种加密编码进制全解析:特点、用处与实战识别指南|小迪安全笔记|网络安全|
笔记·安全·web安全
xian_wwq10 小时前
【学习笔记】OSI安全架构体系
网络·笔记·学习
love530love10 小时前
Windows 11 下再次成功本地编译 Flash-Attention 2.8.3 并生成自定义 Wheel(RTX 3090 sm_86 专属版)
人工智能·windows·笔记·编译·flash_attn·flash-attn·flash-attention
sunnyday042611 小时前
Spring Boot 项目中使用 Dynamic Datasource 实现多数据源管理
android·spring boot·后端
中屹指纹浏览器12 小时前
2025 硬核技术:中屹指纹浏览器进程级沙箱隔离,筑牢多开防关联壁垒
经验分享·笔记