shape类似CSS,用于背景,边框,便于兼容各种屏幕和分辨率:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充色 -->
<solid android:color="#e4e4e4"/>
<!-- 描边 -->
<stroke android:color="#e4e4e4" />
<!-- 圆角半径 -->
<corners android:radius="4dip" />
<!-- 渐变 -->
<gradient
android:angle="45"
android:centerX="20dip"
android:centerColor="#ff0000"
android:startColor="#ffffff"
android:endColor="#000000"/>
<padding
android:left="10dip"/>
<size android:width="60dip"
android:height="30dip"/>
</shape>
shape属性:
rectangle:矩形
oval:椭圆
line:线,需要 stroke 来设置宽度
ring:环形
solid属性:
color:填充颜色
stroke属性:
color:边框颜色
width:边框宽度
dashWidth:虚线框的宽度
dashGap:虚线框的间隔
corners属性:
radius:四个角的半径
topRightRadius:右上角的半径
bottomLeftRadius:右下角的半径
opLeftRadius:左上角的半径
bottomRightRadius:左下角的半径
gradient属性:
startColor:其实颜色
centerColor:中间颜色
endColor:结束颜色
centerX:中间颜色的相对X坐标(0 -- 1)
centerY:中间颜色的相对Y坐标(0 -- 1)
useLevel:(true/false), 是否用作LevelListDrawable的标志
angle是渐变角度,必须为45的整数倍。0从左到右,90从下到上,180从右到左,270从上到下
type:渐变模式。 默认线性渐变,可以指定渐变为radial(径向渐变)或者sweep(类似雷达扫描的形式)
gradientRadius:渐变半径,径向渐变需指定半径。
padding属性:
left:左内边距
top:上内边距
right:右内边距
bottom:下内边距
size属性:
width:宽
height:高
下面是shape的应用:
android中实现自定义画线,画圆,画矩形,使用自定义字体
一、自定义画线
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke android:width="1dp" //线的粗度
android:color="#33ccff" //颜色
android:dashWidth="2dp" //虚线的线段长度
android:dashGap="5dp"/> //虚线的间隔长度
</shape>
布局xml文件中可以使用textview控件,设置背景属性
二、自定义画圆
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#33ccff"/>
<size android:width="50dp" //圆或椭圆
android:height="50dp"/>
</shape>
布局xml文件中使用imageview控件
三、自定义画矩形
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="45" //渐变角度 45的整数倍
android:centerColor="#00ff00" //渐变颜色
android:endColor="#0000ff"
android:startColor="#ff0000" />
<solid android:color="#33ccff" /> //纯色
<size
android:height="100dp"
android:width="50dp" />
<corners android:radius="10dp" /> //圆角
</shape>
四、使用自定义字体
把字体格式文件.ttf,拷贝到assets目录下,读取字体文件Typeface.createFromAsset,设置类型setTypeface
代码中使用字体如下:
TextView textView = (TextView) findViewById(R.id.textView2);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/samplefont.ttf");//读取字体
textView.setTypeface(tf);//设置字体