Android <shape>定义图形
来源:互联网 发布:阿里云域名认证失败 编辑:程序博客网 时间:2024/05/17 06:37
shape标签可以用于自定义一些简单的图形
一个简单例子,定义一个圆角矩形:
在res/drawable目录下创建example.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="2dp" /> <size android:height="20dp" android:width="30dp"/> <solid android:color="#ffffff"/> <stroke android:color="#ff6767" android:width="12dp"/></shape>
这就是定义好的圆角矩形
shape标签有一个android:shape属性,包括了四个值:
这个属性定义了图形的类型。
在shape标签下还有五个标签:
然后就可以应用自定义的图形作为控件的背景等。
例如
将上述自定义的圆角矩形指定为布局容器的背景
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.easonchung.test.MainActivity"> <!-- LinearLayout的background属性设置成刚刚定义的图形 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/example" android:padding="16dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!" android:textSize="30sp"/> </LinearLayout></RelativeLayout>
效果如图
也可以用作按钮等控件的自定义样式
将example.xml复制一个副本命名为example_pressed.xml
然后将里面的solid颜色改为描边的颜色,而stroke颜色改为更鲜艳的红色
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="2dp"/> <size android:width="30dp" android:height="20dp" /> <solid android:color="#ff6767" /> <stroke android:width="12dp" android:color="#ff2d2d" /></shape>
再新建一个资源文件button.xml
这里需要用到 selector 标签
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 点击时的drawable --> <!-- 指定为example_pressed.xml --> <item android:drawable="@drawable/example_pressed" android:state_focused="true" /> <!-- 点住不松手时的drawable --> <!-- 也指定为example_pressed.xml --> <item android:drawable="@drawable/example_pressed" android:state_pressed="true" /> <!-- 一般时候的drawable --> <!-- 指定为example.xml --> <item android:drawable="@drawable/example" /></selector>
之后再将Button的background指定为上面写好的button.xml
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:background="@drawable/button"/>
效果如图
0 0
- Android <shape>定义图形
- android:shape 图形处理
- Android 自定义图形 shape
- Android Shape图形
- android shape图形整理
- Android shape 图形
- Android Shape图形
- Android自定义图形-Shape
- Android 自定义shape图形效果
- Android系列: 自定义图形shape
- Android shape 绘制图形笔记
- 实习杂记(20)---Android里面shape定义图形相关属性
- 【Android】Android图形之shape使用
- 【Android】Android图形之shape使用
- android中通过shape定制图形效果
- Android中用Shape绘制空心图形
- Android 编程下 shape 绘制图形
- android中使用shape自定义图形
- hdu-1754 I Hate It(线段树求区间最大值)
- C语言函数sscanf()的用法
- classes could not be found: - android.support.v7.i
- 图论训练一B
- 已有数据库(单机)部署Database Vault
- Android <shape>定义图形
- android button light 流程分析(二) — HAL & JNI
- Android Scroller工具类的简单用法
- html 写的一个登录界面
- Java BigDemical在工作中的应用(主要用于钱的处理)
- JTextArea中光标的操作
- iOS 打开系统打电话 发短信 邮件 浏览器 地图
- com.google.gson.JsonSyntaxException: 4.1.9
- 用js实现跑马灯抽奖效果