ShapeDrawable用于定义一个基本的几何图形(如矩形、圆形、线条等),定义ShapeDrawable的XML文件的根元素是<shape.../>元素,该元素可指定如下属性。

  • android:shape=["rectangel"|"oval"|"line"|"ring"]:指定定义哪种类型的集合图形。

定义ShapeDrawable对象的完整语法格式如下:

<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:useslevel=["true" |"false"]/>

    <!-- 定义几何形状的内边框 -->

    <padding

        android:left="integer"

        android:top="integer"

        android:right="integer"

        android:bottom="integer"/>

    <!-- 定义几何图形的大小 -->

    <size

        android:width="integer"

        android:color="color"

        android:dashWidth="integer"

        android:dashGap="integer"/>

    <!-- 定义使用单种颜色填充 -->

    <solid

        android:color="color"/>

    <!-- 定义为几何图形绘制边框 -->

    <stroke

        android:width="integer"

        android:color="color“

        android:dashWidth="integer"

        android:dashGap="integer"/>

</shape>

下面通过示例来介绍ShapeDrawable资源的定义和使用。

 实例:椭圆形、渐变背景的文本框

      前面介绍TextView时知道该组件可指定一个android:background属性,该属性用于为该文本框指定背景。大部分时候,文本框背景只是一个简单的图片,或者只是一个简单的颜色。

如果程序使用ShapeDrawable资源作为文本框的android:background属性,则可以在Android应用中做出各种外观的文本框。下面先定义如下的ShapeDrawable资源。

程序清单:my_shape_1.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 设置填充颜色 -->
<solid android:color="#fff"/>
<!-- 设置四周的内边距 -->
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp"/>
<!-- 设置边框 -->
<stroke android:width="3dp" android:color="#ff0"/>
</shape>

接下来定义如下ShapeDrawable资源

程序清单:my_shape_2.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 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>

在定义如下ShapeDrawable资源。

程序清单:my_shape_3.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 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资源之后,接下来在界面布局文件中用这三个ShapeDrawable资源作为文本框的背景。界面布局文件代码如下。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
> <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>

使用Activity加载、显示上面的界面布局文件,将可以看到如图6.5所示的界面。

使用(Drawable)资源———ShapeDrawable资源的更多相关文章

  1. [C++] 将 mp3 等音乐资源以资源形式嵌入 exe 文件中

    引用:http://www.easyx.cn/skills/View.aspx?id=6 本文讲解怎样将 mp3 等音乐资源以资源形式嵌入 exe 文件中,并通过 mciSendString 调用.嵌 ...

  2. 将 mp3 等音乐资源以资源形式嵌入 exe 文件中

    引用:http://www.easyx.cn/skills/View.aspx?id=6 本文讲解怎样将 mp3 等音乐资源以资源形式嵌入 exe 文件中,并通过 mciSendString 调用.嵌 ...

  3. Spring MVC配置静态资源和资源包

    Spring MVC配置静态资源和资源包 本例映射:css目录: pom.xml <properties> <spring.version>4.3.5.RELEASE</ ...

  4. 十九、dbms_resource_manager(用于维护资源计划,资源使用组和资源计划指令)

    1.概述 作用:用于维护资源计划,资源使用组和资源计划指令;包dbms_resource_manager_privs用于维护与资源管理相关的权限. 2.包的组成 1).dbms_resource_ma ...

  5. java线程共享受限资源 解决资源竞争 thinking in java4 21.3

    java线程共享受限资源 解决资源竞争  具体介绍请參阅:thinking in java4 21.3 thinking in java 4免费下载:http://download.csdn.net/ ...

  6. 3、flink架构,资源和资源组

    一.flink架构 1.1.集群模型和角色 如上图所示:当 Flink 集群启动后,首先会启动一个 JobManger 和一个或多个的 TaskManager.由 Client 提交任务给 JobMa ...

  7. android学习笔记33——资源ShapeDrawable

    ShapeDrawable ShapeDrawable用于定义一个基本的几何图像(如,矩形.圆形.线条.......). 定义ShapeDrawable的XML文件的根元素是<shape.../ ...

  8. 使用(Drawable)资源———AnimationDrawable资源

    AnimationDrawable代表一个动画. 下面以补间动画为例来介绍如何定义AnimationDrawable资源,定义补间动画的XML资源文件已<set.../>元素作为根元素,该 ...

  9. 使用(Drawable)资源——LayerDrawable资源

    与StateListDrawable有点类似,LayerDrawable也可包含一个Drawable数组,因此系统将会按这些Drawable对象的数组顺序来绘制它们,索引最大的Drawable对象将会 ...

随机推荐

  1. 通过ant调用shell脚本执行adb命令

    在Hudson或者Jenkins中利用ant的exec 来调用shell命令,通过shell脚本来执行adb shell命令,可以正常执行,不会出现在ant中直接调用adb shell出现的假死情况. ...

  2. Python3基础 使用list() 生成一个空列表

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

  3. ZooKeeper 的安装和配置---单机和集群

    如题本文介绍的是ZooKeeper 的安装和配置过程,此过程非常简单,关键是如何应用(将放在下节及相关节中介绍). 单机安装.配置: 安装非常简单,只要获取到 Zookeeper 的压缩包并解压到某个 ...

  4. away3d 汽车路线编辑器

    2012年的时候,跟朋友去到一个公司,打算用away3d做一个赛车模拟养成游戏,后来由于种种原因,立项未成,由于朋友已经转行,自己也想对自己做过的事情有一些交代,所以将我负责的部分,赛道编辑器的源码公 ...

  5. rest第一篇

    rest的作用 : 以http请求的格式提供数据 实际项目中,定义一个模块,从http请求中拿参数,然后访问mysql数据库得到数据,返回给http请求.

  6. Spring自学教程-介绍、特点、框架(一)

    一.spring是什么,有什么用? 一句话:面向企业应用,使用javabean代替ejb的java应用或web开发. 侵入式的做法就是要求用户代码"知道"框架的代码,表现为用户代码 ...

  7. mysql优化------2 查看系统性能(表大小,I/o性能)

    三:判断mysql I/0 性能的一种方式(网络搜集供参考) show global status like 'innodb_dblwr%'\G   如果innodb_dblwr_pages_writ ...

  8. POJ 2609 Ferry Loading

    双塔DP+输出路径. 由于内存限制,DP只能开滚动数组来记录. 我的写法比较渣,但是POJ能AC,但是ZOJ依旧MLE,更加奇怪的是Uva上无论怎么改都是WA,其他人POJ过的交到Uva也是WA. # ...

  9. Android之布局大全

    Android布局方式可以归类为ViewGroup的5个类别,即ViewGroup的5个直接子类.其它的一些布局都扩展自这5个类. 上结构图: 转自:http://www.cnblogs.com/de ...

  10. Qt下libusb-win32的使用方法(转)

    源:Qt下libusb-win32的使用方法 之前一直找不到适合WIN7下的Tiny6410的USB下载软件,正好这几天开始学习USB,所以打算自己写一个专门用于Tiny6410的WIN7下的USB下 ...