Android程序开发中,我们经常会去用到Shape这个东西去定义各种各样的形状,shape可以绘制矩形环形以及椭圆,所以只需要用椭圆即可,在使用的时候将控件比如imageview或textview的高宽设置成一样就是正圆,solid表示远的填充色,stroke则代表远的边框线,所以两者结合可以实现带边缘的圆,当然也可以直接加上size控制高宽。那么我首先带你们了解一下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的用法。例子如下:

1、画椭圆虚线边框背景,资源文件代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <!-- 圆角 -->
  4. <corners
  5. android:bottomLeftRadius="8dp"
  6. android:bottomRightRadius="8dp"
  7. android:radius="15dp"
  8. android:topLeftRadius="8dp"
  9. android:topRightRadius="8dp" />
  10. <!-- 描边 -->
  11. <stroke
  12. android:dashGap="4dp"
  13. android:dashWidth="4dp"
  14. android:width="2dp"
  15. android:color="@color/ellipse_dashed_line_color" />
  16. </shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 圆角 -->
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:radius="15dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" /> <!-- 描边 -->
<stroke
android:dashGap="4dp"
android:dashWidth="4dp"
android:width="2dp"
android:color="@color/ellipse_dashed_line_color" /> </shape>

2、画实线透明边框背景,资源文件代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <!-- 圆角 -->
  4. <corners
  5. android:bottomLeftRadius="6dp"
  6. android:bottomRightRadius="6dp"
  7. android:radius="10dp"
  8. android:topLeftRadius="6dp"
  9. android:topRightRadius="6dp" />
  10. <!-- 描边 -->
  11. <stroke
  12. android:width="1dp"
  13. android:color="@color/ellipse_dashed_line_color" />
  14. </shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 圆角 -->
<corners
android:bottomLeftRadius="6dp"
android:bottomRightRadius="6dp"
android:radius="10dp"
android:topLeftRadius="6dp"
android:topRightRadius="6dp" /> <!-- 描边 -->
<stroke
android:width="1dp"
android:color="@color/ellipse_dashed_line_color" /> </shape>

3、画实线填充颜色边框背景,资源文件代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <!-- 圆角 -->
  4. <corners
  5. android:bottomLeftRadius="6dp"
  6. android:bottomRightRadius="6dp"
  7. android:radius="10dp"
  8. android:topLeftRadius="6dp"
  9. android:topRightRadius="6dp" />
  10. <!-- 描边 -->
  11. <solid
  12. android:width="1dp"
  13. android:color="@color/ellipse_dashed_line_color" />
  14. </shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 圆角 -->
<corners
android:bottomLeftRadius="6dp"
android:bottomRightRadius="6dp"
android:radius="10dp"
android:topLeftRadius="6dp"
android:topRightRadius="6dp" /> <!-- 描边 -->
<solid
android:width="1dp"
android:color="@color/ellipse_dashed_line_color" /> </shape>

4、画实线透明半边椭圆边框,资源文件代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item>
  4. <shape android:shape="rectangle" >
  5. <stroke
  6. android:width="1.2dp"
  7. android:color="#669df3" />
  8. <solid android:color="#00000000" />
  9. <corners
  10. android:bottomRightRadius="10dp"
  11. android:topRightRadius="10dp" />
  12. <padding
  13. android:bottom="8dp"
  14. android:left="12dp"
  15. android:right="12dp"
  16. android:top="8dp" />
  17. </shape>
  18. </item>
  19. </layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item>
<shape android:shape="rectangle" >
<stroke
android:width="1.2dp"
android:color="#669df3" /> <solid android:color="#00000000" /> <corners
android:bottomRightRadius="10dp"
android:topRightRadius="10dp" /> <padding
android:bottom="8dp"
android:left="12dp"
android:right="12dp"
android:top="8dp" />
</shape>
</item> </layer-list>

5、画实线填充颜色半边椭圆边框,资源文件代码如下:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    3. <item>
    4. <shape android:shape="rectangle" >
    5. <solid android:color="#669df3" />
    6. <corners
    7. android:bottomLeftRadius="10dp"
    8. android:topLeftRadius="10dp" />
    9. <padding
    10. android:bottom="8dp"
    11. android:left="12dp"
    12. android:right="12dp"
    13. android:top="8dp" />
    14. </shape>
    15. </item>
    16. </layer-list>

//---------------------------------------------------------------------

TextView组件可指定一个android:background属性,该属性用于为该文本框指定背景。大部分的时候,文本框的背景只是一个简单的图片,或者只是一个简单的颜色。
  如果程序使用ShapeDrawable资源作为文本框的android:background属性,则可以在Android应用中作出各种外观的文本框。

ShapeDrawable资源文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
    android:shape="rectangle">
    <!-- 设置填充颜色 -->
    <solid android:color="#fff"/>
    <!-- 设置四周的内边距 -->
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <!-- 设置边框 -->
    <stroke android:width="3dip" android:color="#ff0" />
</shape>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
    android:shape="rectangle">
    <!-- 定义填充渐变颜色 -->
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <!-- 设置内填充 -->
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <!-- 设置圆角矩形 -->
    <corners android:radius="8dp" />
</shape>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
    android:shape="oval">
    <!-- 定义填充渐变颜色 -->
    <gradient
        android:startColor="#ff0"
        android:endColor="#00f"
        android:angle="45"
        android:type="sweep"/>
    <!-- 设置内填充 -->
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <!-- 设置圆角矩形 -->
    <corners android:radius="8dp" />
</shape>

接下来在界面布局文件中用这三个ShapeDrawable资源作为文本框的背景。界面布局文件代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/my_shape_1"
    />
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/my_shape_2"
    />  
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/my_shape_3"
    />
</LinearLayout>
 
 
 
 

Android中的shape的更多相关文章

  1. Android中的Shape使用总结

    参考:http://www.cnblogs.com/gzggyy/archive/2013/05/17/3083218.html 在Android程序开发中,我们经常会去用到Shape这个东西去定义各 ...

  2. Android中使用shape来定义控件

    本文章转接于:http://kofi1122.blog.51cto.com/2815761/521605 Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对s ...

  3. Android中使用shape实现EditText圆角

    之前看到手机上的百度editText控件是圆角的就尝试做了一下,看了看相关的文章. 因为代码少,看看就知道了.所以下面我就直接贴上代码供大家参考,有其他的好方法记得分享哦~ 整个代码不涉及JAVA代码 ...

  4. Android中使用shape制作一个旋转的progressbar

    public class ZNtestResActivity extends Activity { @Override public void onCreate(Bundle savedInstanc ...

  5. android中自定义shape

    <shape> <!-- 实心 --> <solid android:color="#ff9d77"/> <!-- 渐变 --> & ...

  6. (转)Android中的Shape使用总结

    http://blog.csdn.net/bear_huangzhen/article/details/24488337 在Android程序开发中,我们经常会去用到Shape这个东西去定义各种各样的 ...

  7. Android 中shape的使用(圆角矩形)

    一.在res/drawable文件夹下创建一个名为gradient_box的xml文件: <?xml version="1.0" encoding="utf-8&q ...

  8. 转 Android中shape中的属性大全

    <shape>            <!-- 实心 -->            <solid android:color="#ff9d77"/&g ...

  9. 【Android 界面效果19】Android中shape的使用

    Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对shape有了大体的了解,稍作总结: 先看下面的代码:         <shape>      ...

随机推荐

  1. 在mac安装numpy matplotlib scipy

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #fffff ...

  2. HTML5本地存储详解

    HTML5storage提供了一种方式让网站能够把信息存储到你本地的计算机上,并再以后需要的时候进行获取.这个概念和cookie相似,区别是它是为了更大容量存储设计的.Cookie的大小是受限的,并且 ...

  3. super在构造函数中的运用

    package example;//在子类构造对象时,访问子类构造函数时候,父类也运行.//原因是:在子类的构造函数中第一行有一个默认的隐式语句super();/* 构造函数不能覆盖 子类的实例化过程 ...

  4. mac版tomcat修改端口无法访问,80端口无法访问

    在mac上安装好了tomcat,修改了端口为80,没想到关闭tomcat时提示出错,而且无法访问,原来我犯了两个错误. 1.我用的是mac上的文本编辑.app打开然后修改的,重新修改为8080也不行, ...

  5. Debian系Linux的dpkg命令

    dpkg "是"Debian Packager "的简写.为 "Debian" 专门开发的套件管理系统,方便软件的安装.更新及移除.所有源自" ...

  6. 昨天面试遇到的一道C语言题

    楼主之前是做C/C++开发的,今年转到java,hadoop方向了,所以很多C/C++的细节都有些模糊了,碰巧这次面试题中,就出了一道C指针的问题. 问题不算难,但楼主一时之间竟也想不起来答案了... ...

  7. Busy Beavers(暴力模拟)

    由于排版问题,题目无法显示,可以到 http://7xjob4.com1.z0.glb.clouddn.com/e4872a15819b6bf9d1e5250bacc2a30b  查看 题目大意是有只 ...

  8. jquery选择器之基本筛选选择

    1.基本选择器 2.内容筛选选择器 3.可见性筛选选择器 4.属性筛选选择器 5.子元素筛选选择器 6.表单元素选择器 7.表单对象属性筛选器

  9. sublime从官网纯净版到插件完整版

    01.纯净版下载地址:www.sublimetext.com/ 02.下载Package Control插件管理工具,网址:https://packagecontrol.io/ 安装介绍:https: ...

  10. iOS开发下架在AppStore中销售的app

    1.登陆开发者账号 2.选择itunes connect 选择我的app 3.选择要下架的app 4.价格与销售范围 5.销售范围 6.点击存储 //如果想要重新在AppStore中进行销售只需要选择 ...