Android Shape 的使用

2023-10-27

目录

什么是Shape?

shape属性

子标签属性

corners (圆角)

solid (填充色)

gradient (渐变)

stroke (描边)

padding (内边距)

size (大小)

特殊属性

rectangle(矩形)

oval(椭圆)

line(线)

ring(圆环)

shape 用法


什么是Shape?

在Android开发中,我们可以使用shape定义各种各样的形状,也可以定义一些图片资源。相对于传统图片来说,使用shape可以减少资源占用,减少安装包大小,还能够很好地适配不同尺寸的手机。

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 (圆角)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners //定义圆角
        android:radius="10dp" //全部的圆角半径;
        android:topLeftRadius="5dp" //左上角的圆角半径;
        android:topRightRadius="5dp" //右上角的圆角半径;
        android:bottomLeftRadius="5dp" //左下角的圆角半径;
        android:bottomRightRadius="5dp" /> //右下角的圆角半径。
</shape>

solid (填充色)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ffff00"/> //内部填充色
</shape>

gradient (渐变)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient
        android:type=["linear" | "radial" | "sweep"] //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变;
        android:angle="90" //渐变角度,必须为45的倍数,0为从左到右,90为从上到下;
        android:centerX="0.5" //渐变中心X的相当位置,范围为0~1;
        android:centerY="0.5" //渐变中心Y的相当位置,范围为0~1;
        android:startColor="#24e9f2" //渐变开始点的颜色;
        android:centerColor="#2564ef" //渐变中间点的颜色,在开始与结束点之间;
        android:endColor="#25f1ef" //渐变结束点的颜色;
        android:gradientRadius="5dp" //渐变的半径,只有当渐变类型为radial时才能使用;
        android:useLevel="false" /> //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果。
</shape>

stroke (描边)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke
        android:width="1dp" //描边的宽度
        android:color="#ff0000" //描边的颜色
        // 以下两个属性设置虚线
        android:dashWidth="1dp" //虚线的宽度,值为0时是实线
        android:dashGap="1dp" />//虚线的间隔
</shape>

padding (内边距)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <padding
        android:left="10dp" //左内边距;
        android:top="10dp" //上内边距;
        android:right="10dp" //右内边距;
        android:bottom="10dp" /> //下内边距。
</shape>

size (大小)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size
        android:width="50dp" //宽度
        android:height="50dp" />// 高度
</shape>

特殊属性

Shape可以定义当前Shape的形状的,比如矩形,椭圆形,线形和环形;这些都是通过 shape 标签属性来定义的, shape 标签有下面几个属性:rectangle,oval,line,ring

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] //shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)
    //下面的属性只有在android:shape="ring"时可用:
    android:innerRadius="10dp" // 内环的半径;
    android:innerRadiusRatio="2" // 浮点型,以环的宽度比率来表示内环的半径;
    android:thickness="3dp" // 环的厚度;
    android:thicknessRatio="2" // 浮点型,以环的宽度比率来表示环的厚度;
    android:useLevel="false"> // boolean值,如果当做是LevelListDrawable使用时值为true,否则为false。
</shape>

rectangle(矩形)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimary"/>
</shape>

oval(椭圆)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/colorPrimary"/>
    <size android:height="100dp"
        android:width="100dp"/>
</shape>

line(线)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="1dp"
        android:color="@color/colorAccent"
        android:dashGap="3dp"//虚线间距
        android:dashWidth="4dp"/>//虚线宽度
    <size android:height="3dp"/>
</shape>

ring(圆环)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false"
    android:innerRadius="20dp" // 内环的半径
    android:thickness="10dp"> // 圆环宽度
    <!--useLevel需要设置为false-->
    <solid android:color="@color/colorAccent"/>
</shape>

shape 用法

1. 在res/drawable下新建 shape_text.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
		android:shape="rectangle">

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

	<gradient
			android:startColor="#FF0000"
			android:endColor="#80FF00"
			android:angle="45" />

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

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

	<solid android:color="#ffff9d" />

	<stroke
			android:width="2dp"
			android:color="#dcdcdc" />
</shape>

2. 在布局中引用 shape_text.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Shape测试"
        android:background="@drawable/shape_text"
        android:textSize="15sp"
        android:textColor="@android:color/black"/>
</LinearLayout>

shape 使用示例

在src-main-res-drawable下,右键 New-Drawable Resource File

会生成一个这样的文件

然后在上面代码中找一个示例

然后在我们的 layout 文件中使用 shape,使用效果图如下

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Shape 的使用 的相关文章

随机推荐

  • mysql 视图的作用

    转自 http blog csdn net fm0517 article details 5625949 视图是从一个或几个基本表 或视图 导出的表 它与基本表不同 是一个虚表 数据库只存放视图的定义 而不存放视图对应的数据 这些数据仍存放
  • JS赋值运算符详解

    赋值运算符左侧的操作数必须是变量 对象属性或数组元素 也称为左值 例如 下面的写法是错误的 因为左侧的值是一个固定的值 不允许操作 1 100 返回错误 赋值运算有以下两种形式 简单的赋值运算 把等号右侧操作数的值直接复制给左侧的操作数 因
  • [下载演讲稿]数字藏品与元宇宙存储—数字新世界的“土壤”

    和上次 下载 元宇宙存储 演讲稿 相比 增加了 1 两厅印发的 关于推进实施国家文化数字化战略的意见 对数字藏品的发展有积极促进作用 2 NFT和数字藏品的分类 新玩法 高质量体验 守诺 受朱嘉明老师 朱嘉明 数字经济和非同质时代 NFT
  • Java BigInteger的使用

    前言 在Java中 由CPU原生提供的整型最大范围是64位 long 型整数 使用 long 型整数可以直接通过CPU指令进行计算 速度非常快 但是如果我们使用的整数范围超过了 long 型怎么办 这个时候 就只能用软件来模拟一个大整数 j
  • unity 3D RPG高级教程(十)

    目录 声明 1 Action Button 快捷栏按键 2 Stats Info 显示 Player 相关信息 3 Change Animator 切换动画控制器 4 Item Tooltip 物品信息显示栏 5 Loot Items 掉落
  • misc.func.php,完美解决 discuz 您的管理面板已经锁定!

    出现 对不起 由于您多次输入错误密码 所以管理面板暂时锁定 您现在无法进入管理面板 15 分钟以后 锁定会自动解除 的提示 是出于安全的考虑 在您连续输入五次密码 仍然没有成功登陆的情况下所提示的 并且会在 15 分钟内禁止此 IP 再次登
  • mysql怎样设置默认值约束_MySQL默认值约束怎么用

    本篇文章将介绍default 默认约束 如何使用和改动后的效果 常用数据库约束 default 默认约束 not null 非空约束 指定某列不为NULL unique 唯一约束 指定某列和几列组合的数据不能重复 primary key 主
  • 【C/C++多线程编程之九】pthread读写锁

    多线程编程之读写锁 Pthread 是 POSIX threads 的简称 是POSIX的 线程标准 pthread读写锁把对共享资源的访问者分为读者和写者 读者只对共享资源进行读访问 写者只对共享资源进行写操作 在互斥机制 读者和写者都需
  • 详解Nodejs中的模块化

    Nodejs是一个基于Chrome V8引擎的JavaScript运行时环境 它允许开发者使用JavaScript在服务器端运行代码 在Nodejs中 模块化是一种组织和重用代码的重要方式 模块化允许我们将代码拆分成小块 使得代码结构更清晰
  • Windows10访问Ubuntu子系统(WSL)的桌面环境

    Windows10访问Ubuntu子系统 WSL 的桌面环境 文章目录 Windows10访问Ubuntu子系统 WSL 的桌面环境 Why Linux Why WSL 开启WSL Ubuntu换源 更新与升级 安装桌面环境xubuntu
  • Pytorch-YOLOV4-火焰目标检测

    首先感谢大佬提供的代码bubbliiiing 0 效果展示 1 所需环境 torch 1 2 0 2 注意事项 代码中的yolo4 weights pth是基于608x608的图片训练的 代码中的默认anchors是基于608x608的图片
  • SystemC在Ubuntu16.04上安装测试

    使用SystemC进行硬件仿真 环境 linux x86 64 bash g 下载解压SystemC SystemC下载地址 解压下载的包 tar zxvf systemc 2 3 3 tar gz 进入解压出来的目录 准备编译安装 cd
  • 自动控制原理快速入门+理解

    用最简单的话认识全貌 PS 默认都是线性系统 即输入和输出之间是线性的 默认你知道什么是线性 初步认识控制 假设你在推箱子 你推的力气是 f f f 箱子位移是 x x x 质量是
  • 09-----检测对方网络是否正常在线的方法

    1 使用ping ping是我们经常检测对方能否正常通信的方法 它使用的协议是传输层中的ICMP 下面是我自己抓包的内容 2 使用telnet telnet一般是检测某个端口是否开放 格式为 telnet ip port 3 使用nc命令
  • 汇编语言DW、DB和DD的区别

    DW 是定义2字节空间的意思 DW属于汇编的一个伪指令 DW定义字类型变量 一个字数据占2个字节单元 读完一个 偏移量加2 DB定义字节类型变量 一个字节数据占1个字节单元 读完一个 偏移量加1 DD定义双字类型变量 一个双字数据占4个字节
  • 7.26 二进制练习题

    给你个礼物你能收到吗 打开这个exe文件后 我们看到了它让我们输入礼物提取码 我们先随便输入数据 按回车显示提取码错误还有输错的次数 我们发现这里存在着一个循环 然后我们在IDA里面打开这个文件 int cdecl main int arg
  • 大模型微调代码解析,哪些方法可以加速训练?

    近期大模型层出不穷 大家对于大模型的微调也在跃跃欲试 像Lijia的BELLE 斯坦福的Alpaca 1 清华的ChatGLM 2 中文的Chinese Vicuna 3 让我这样的普通玩家也能训练自己的微调模型 在微调和推理的时候仍然需要
  • Vim快速教程

    Vim快速教程 Vim教程 vimtutor 命令模式 Esc 视图模式 v 编辑模式 i Vim plug Vim教程 vimtutor 命令模式 Esc 快捷键 全称 功能 H J K L 方向键 前面加数字代表移动多少行或列 synt
  • 使用docker部署mysql

    1 拉取mysql 最新版本mysql docker pull mysql latest 拉取5 7版本mysql docker pull mysql 5 7 2 启动容器镜像 docker run id name mysql p 3307
  • Android Shape 的使用

    目录 什么是Shape shape属性 子标签属性 corners 圆角 solid 填充色 gradient 渐变 stroke 描边 padding 内边距 size 大小 特殊属性 rectangle 矩形 oval 椭圆 line