Drawable之Shape drawable
来源:互联网 发布:c语言与数据库 编辑:程序博客网 时间:2024/05/18 05:01
在Android中,Drawable就是一个可以显示在屏幕之上绘图对象。从API的角度来看,所有的Drawable对象都是继承于Drawable类。与标准的控件相比,它们不具备交互性,这意味着当用户touch它们的时候,它们不起反应。
Shape drawable是通用的形状。我们可以通过XML文件来定义该图形。通过android:shape来确定我们要定义的形状。形状有:rectangle(矩形)、oval(椭圆)、line(线条)、ring(环形)。
实例一:定义一个红色背景的椭圆
首先,在drawable目录下定义一个oval.xml。
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="#FF0000" />//背景色 <size android:height="100dp" android:width="120dp" /></shape>在Android Studio中定义drawable文件有专门的方式来创建。右击-》新建New-》Drawable Resource File
然后,在Layout文件中引用oval.xml文件
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:src="@drawable/oval" />
实例二,为按钮添加渐变的背景色
首先,定义round_corner.xml文件
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <stroke //指定边界形状 android:width="2dp" android:color="#00FF00" /> <gradient //背景渐变方式 android:angle="-90" android:endColor="#FFFFFF" android:startColor="#00FF00" android:type="linear" /> <corners android:radius="3dp" />//指定边角半径 <padding //内间距 android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp" />然后在Layout文件中引用该文件
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/round_corner" android:text="Click me" />运行结果,我将两个案例在一个程序中运行,使用RelativeLayout布局。
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/round_corner" android:text="Click me" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button" android:layout_centerHorizontal="true" android:src="@drawable/oval" /></RelativeLayout>运行结果如下所示:
0 0
- Drawable之Shape drawable
- Drawable资源之shape drawable
- Drawable简介之-------shape
- Drawable Shape
- Shape Drawable
- Shape Drawable
- Android Drawable Resources 之 Shape Drawable 使用详解
- Android xml 效果之 Shape Drawable
- Android 学习之Drawable-shape使用方法
- drawable自定义图形资源之shape属性
- 自定义drawable(shape)
- Shape Drawable Resources
- Shape Drawable的学习
- Shape Drawable样式
- shape drawable 翻译
- 自定义drawable(shape)
- Android Shape Drawable Resources
- Android Shape Drawable Resources
- HDU1299&&SPOJ - KPEQU(找因子来找解的个数)
- 频率与频点
- Functional Programming Principles in Scala
- matlab基础学习
- android学习笔记三 ---- android项目文件结构介绍
- Drawable之Shape drawable
- win7回归崎岖路——UEFI+GPT装双系统arch+Windows7折腾记
- git学习记录
- POJ 1061 扩展gcd 线性同余方程
- Android之ListView设置
- 学习笔记(五)接口与内部类(一)
- Monads之关系选择器
- UVA 11995 I Can Guess the Data Structure!
- *******************网络图片查看器*************