Android中shape属性详解:(设置圆角、设置渐变)

一、使用:

1、新建shape文件

首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.xml:
内容:

先简单介绍下shape是如何使用的,下面会有各个属性的详细解释

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

2、添加到控件中

在定义好shape文件后,下一步就是将其添加到控件中或者添加到布局中,添加时,一般是使用设置background属性,将其为控件背景。

android:background="@drawable/window_dialog_border"

二、基本属性

corners:定义圆角
gradient:定义渐变色
padding:内边距(控件本身也可以实现)
size:大小(控件本身也可以实现)
solid:指定内部填充色
stroke:描边属性,可以定义描边的宽度,颜色,虚实线

详解:

Corners:

注:dimension:dp值

Corners标签里的子标签及其所具有的属性:

    //右下角的圆角半径 

Corners标签是用来字义圆角的,其中radius与其它四个并不能共同使用

例如,该布局定义四个圆角半径为20,背景颜色为#ffffffff

<?xml version="1.0" encoding="utf-8"?>        

使用:

就能设置TextView四个角设置为半径20dp圆角,控件内部填充色为黑色。

2、solid

solid用以指定内部填充色

只有一个属性:
使用:设置为全黑的背景

 

3、gradient

gradient用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式,它的属性有下面几个:

  //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果   

4、stroke

这是描边属性,可以定义描边的宽度,颜色,虚实线等

使用

      //虚线的间隔 

5、size和padding

这两个基本上不怎么用,因为他们所具有的功能,控件本身也能实现。 size:是用来定义图形的大小的。

padding:用来定义内部边距:

 

三、Shape的属性
Shape文件本身也具有一些属性

shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)android:shape=["rectangle" | "oval" | "line" | "ring"]    下面的属性只有在android:shape="ring时可用:  android:innerRadius         尺寸,内环的半径。  android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,  android:thickness           尺寸,环的厚度  android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",  android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false. 

可见,只有第一个shape是可用的,其它五个都是shape等于ring时所特有的。

注:
1、当不指定shape形状时,默认是矩形
2、当shape形状指定为环形(ring)时,一定要设置 android:useLevel = false 不然没有效果

更多相关文章

  1. 【Android】android:windowSoftInputMode属性详解
  2. android 布局特殊的属性介绍
  3. 【Android布局】在程序中设置android:gravity 和 android:layout
  4. android的属性
  5. Android(安卓)EditText属性总结
  6. android drawable中的state属性说明
  7. Android:RelativeLayout布局的相关属性介绍
  8. android shape实现边框圆角
  9. android:elevation属性,控制View底部渐变阴影

随机推荐

  1. Android studio finished with non-zero
  2. Edittext输入框限制字母数字
  3. Android开发之Java集合类性能分析
  4. Android下多页显示技巧
  5. android用sharepreference保存输入框中的
  6. Android一键锁屏开发全过程【源码】【附
  7. android backgroud alpha
  8. 2010.12.26——— android 获得手机号码
  9. Android加速度传感器数值的过滤
  10. Android 之 setTextColor 写法