Android Shape 详细使用
一、简介
- 在
Android
开发中,可以使用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>
corners
:用来定义圆角。<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" ><corners // 定义圆角android:radius="20dp" // 全部的圆角半径android:topLeftRadius="10dp" // 左上角的圆角半径android:topRightRadius="10dp" // 右上角的圆角半径android:bottomLeftRadius="10dp" // 左下角的圆角半径android:bottomRightRadius="10dp" /> // 右下角的圆角半径</shape>
solid
:用以指定内部填充色。<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" ><solid android:color="#f66"/> // 内部填充色</shape>
gradient
:用以定义渐变色,可以定义两色渐变
、三色渐变
及渐变样式
。<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" ><gradientandroid: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" ><strokeandroid: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" ><paddingandroid: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" ><sizeandroid:width="50dp" // 宽度android:height="50dp" /> // 高度</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"] // 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"><strokeandroid: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
文件新建在res/drawable
文件夹下,例如shape_text.xml
shape_text.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><!-- 设置一个边框 --><stroke android:width="5px" android:color="#f11" /><!-- 背景渐变--><gradientandroid:angle="90"android:endColor="#fcf"android:startColor="#cff"/><!-- 给使用容器添加内间距 --><paddingandroid:left="20dp"android:top="20dp"android:right="20dp"android:bottom="20dp"/> </shape>
布局中使用
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><!-- 流水布局 --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"><!-- 输入框 --><TextViewandroid:id="@+id/dzm"android:layout_width="300dp"android:layout_height="300dp"android:text="@string/app_name"android:textSize="50sp"android:background="@drawable/shape_text" /></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>
Android Shape 详细使用相关推荐
- Android开发:Shape详细解读
日常开发中,我们会遇到一些Button.Textview...等控件的背景是圆角矩形.圆形...等,和android默认的控件背景矩形不一致,此时shape的作用就体现出来了,我们可以根据shape属 ...
- android阴影分割线,android shape的使用及渐变色、分割线、边框、半透明阴影
shape使用.渐变色.分割线.边框.半透明.半透明阴影效果. 首先简单了解一下shape中常见的属性.(详细介绍参看 api文档) xmlns:android="http://schema ...
- Android Shape 的使用
学而时习,温故而知新. 今天复习shape 画各种常见类型的背景图 使用: 当在 java 代码R.drawable.文件的名称 当在布局中时 android:background="@dr ...
- android shape.xml 文件使用
设置背景色可以通过在res/drawable里定义一个xml,如下: <?xml version="1.0" encoding="utf-8"?> ...
- linux安装 Android Studio详细教程,支持性较差,需要安装最新底层库内核的linux
安装 Android Studio详细教程 libc6-i386 lib32stdc++6 lib32gcc1 lib32ncurses5 lib32z1 jdk1.8.0_25 android-st ...
- Android Shape使用
说明 在Android开发中,使用shape可以很方便的帮我们画出想要的背景,相对于png图片来说,使用shape可以减少安装包的大小,而且能够更好的适配不同的手机. 使用 先贴出官网上的说明: &l ...
- android shape的可选参数以及每个参数的含义与用法!
2019独角兽企业重金招聘Python工程师标准>>> <span style="font-size:18px"><?xml version=& ...
- Android Shape Drawable Resources
本文主要介绍Drawable Resources的一种,Shape Drawable Resources的使用.其他Drawable类似 经常需要自己设置某个view的背景,比如类似新浪微博客户端微博 ...
- Android shape的使用(圆角矩形)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
- android shape 按钮背景_Android button, xml文件定义形状,代码中修改背景颜色
1. 首先在drawable文件夹定义一个shape.xml文件,内容如下: xmlns:android="http://schemas.android.com/apk/res/androi ...
最新文章
- About darwin OS
- ZedGraph给LineChart添加数值
- des解密 given final_真相解密创新Aurvana Live SE评测怎么样?【使用一个月后感受实情爆料!!!...
- mqtt连接失败_Flutter通过Mqtt消费ActivieMQ
- 【java】Java 原子性、有序性与Happens-Before
- InfoPath读取数据库
- Makefile-filter和filter-out
- tms intraweb html5,TMS VCL Chart
- Spring面试基础题
- 表单多条相同name数据的获取
- 洛谷 P2578 [ZJOI2005]九数码游戏【bfs+康托展开】
- 2022年第五届中青杯赛题浅评
- 最短路径(信息学奥赛一本通 - T1378)
- University Code
- springMVC 面试题整理
- oracle insert汉字出错,oracle insert中文后,select是乱码
- opencv制作微信小游戏 最强连一连 辅助(2)--dfs深度优先搜索算法
- 修改PyCharm的背景颜色
- 【python机器学习】线性回归--梯度下降实现(基于波士顿房价数据集)
- 八个步骤教你做好会议现场管理