刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用。请注意,不要自行向xml
资源文件内添加注释,本文加上注释只是方便演示,编写代码的过程中请不要随意添加!否则会报错!
首先在res/drawable
文件夹下,新建一个文件,命名为:shape_radius.xml
内容是这样的:(先不需要理解,先看shape怎么用)
<?xmlversion="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android">
<!-- 边角弧度(圆角) -->
<cornersandroid:radius="20dip"/>
<!-- 背景颜色 -->
<solidandroid:color="#ff00ff"/>
</shape>
在定义好shape文件后,下一步就是将其添加到控件中,添加到控件中,一般是使用设置background属性,将其为控件背景,下面,我们将其设置为MainActivity
对应的布局中(activity_main.xml
),将其设为TextView的背景,看显示出来 是什么样子的。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="50dip"
android:text="@string/hello_world"
android:background="@drawable/shape_radius"/>
显示出来的结果是这样的:
上面给大家简单的讲了下shape标签组的简单使用方法,下面就具体讲讲shape标签里所具有的几个子标签及所具有的属性。
<corners//定义圆角
android:radius="dimension"//全部的圆角半径
android:topLeftRadius="dimension"//左上角的圆角半径
android:topRightRadius="dimension"//右上角的圆角半径
android:bottomLeftRadius="dimension"//左下角的圆角半径
android:bottomRightRadius="dimension"//右下角的圆角半径/>
Corners标签是用来字义圆角的,其中radius与其它四个并不能共同使用
在控件布局中使用:
<?xmlversion="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dip"/>
<solid android:color="#ffff00"/>
</shape>
效果:
solid用以指定内部填充色
只有一个属性:
<solid android:color="color"/>
在上面的例子中,我们就将填充色指定为#ffff00了,如果我们不加圆角,只使用填充色,即将shape变成这样子:
<?xmlversion="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffff00"/></shape>
那效果就是这样的:
gradient用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式,它的属性有下面几个:
<gradient android:type=["linear"|"radial"|"sweep"]//共有3中渐变类型,线性渐变(默认)
android:angle="integer"//渐变角度,必须为45的倍数,0为从左到右,90为从上到下
android:centerX="float"//渐变中心X的相当位置,范围为0~1android:centerY="float"//渐变中心Y的相当位置,范围为0~1
android:startColor="color"//渐变开始点的颜色
android:centerColor="color"//渐变中间点的颜色,在开始与结束点之间
android:endColor="color"//渐变结束点的颜色
android:gradientRadius="float"//渐变的半径,只有当渐变类型为radial时才能使用
android:useLevel=["true"|"false"]//使用LevelListDrawable时就要设置为true。/>
首先有三种渐变类型,分别是:linear(线性渐变)、radial(放射性渐变)、sweep(扫描式渐变),不过只需要记住线性渐变就行。
<?xmlversion="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:type="linear"
android:startColor="#ff0000"
android:centerColor="#00ff00"
android:endColor="#0000ff"/>
</shape>
这是描边属性,可以定义描边的宽度,颜色,虚实线等
<stroke
android:width="dimension" //描边的宽度
android:color="color" //描边的颜色
// 以下两个属性设置虚线
//虚线的宽度,值为0时是实线
android:dashWidth="dimension"
//虚线的间隔
android:dashGap="dimension" />
上面各个属性的意义如下:
我们使用绿色虚线描边,虚线高度是20dp,虚线宽度为10dp虚线间距为1dp
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="20dp"
android:color="#00ff00"
android:dashWidth="10dp"
android:dashGap="1dp" />
</shape>
从效果图中,我们也能清晰的看出这三个参数(width、dashwidth、dashGap)之间的区别
这两个基本上不怎么用,因为他们所具有的功能,控件本身也能实现。 size:是用来定义图形的大小的
<size
android:width="dimension"
android:height="dimension" />
//padding用于设置内边距
<padding
android:left="dimension"
android:top="dimension"
android:right="dimension"
android:bottom="dimension" />
上面我们讲了Shape的子标签的的作用,但Shape本身还没讲,Shape自已是可以定义当前Shape的形状的,比如上面的矩形,还有椭圆形,线形和环形;这些都是通过Shape标签的 shape属性来定义的,Shape标签总共有下面几个属性,我们一个个讲:
在控件中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/try_shape_3"/>
</LinearLayout>
shape代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff00ff"/>
</shape>
控件代码不变,下面是shape代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#ff00ff"/>
</shape>
控件代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/try_shape_2"/>
</LinearLayout>
shape代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadius="20dp"
android:thickness="50dp"
android:useLevel="false">
<solid android:color="#ff00ff"/>
</shape>
到此结束
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。