Android支持自定义Shape, 以画出需要的形状,可以作为TextView, EditText, Button的背景drawable资源。Shape很简单,就是一个XML文件,SDK文档里描述其格式如下:
- xml version="1.0" encoding="utf-8"?>
- <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:height="integer" />
- <solid
- Android:color="color" />
- <stroke
- Android:width="integer"
- Android:color="color"
- Android:dashWidth="integer"
- Android:dashGap="integer" />
- shape>
其支持的属性没有shadow, 做Web前端开发的同学写CSS可以很方便地加一个shadow属性值,如何给Android Shape加一个shadow,以得到类似的效果呢?
答案是使用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">
- <solid Android:color="#792a03" />
- <corners Android:radius="19dp" />
- shape>
- item>
- <item Android:top="1px">
- <shape Android:shape="rectangle">
- <gradient Android:startColor="#ffdb8f" android:endColor="#ffdb8f"
- Android:angle="270" />
- <padding Android:left="5dp" android:top="3dp" android:right="5dp"
- Android:bottom="3dp" />
- <corners Android:radius="20dp" />
- shape>
- item>
- layer-list>
将以上xml存成btn_test, 放到res/drawable/目录下。 将该drawable xml设为一个TextView的backgroiund,
- <TextView
- Android:background="@drawable/btn_test"
- Android:layout_marginTop="20dip"
- Android:layout_marginLeft="5dip"
- Android:textColor="#792a03"
- Android:text="1天2小时14分20秒"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content" />
其效果如下图所示:
关于layer-list的进一步解释见SDK文档,如下:
Layer List
A LayerDrawable
is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
Each drawable is represented by an element inside a single
element.
-
file location:
-
res/drawable/filename .xml
The filename is used as the resource ID.
compiled resource datatype:
-
Resource pointer to a
LayerDrawable
.
resource reference:
-
In Java:
R.drawable.filename
In XML:@[package :]drawable/filename
syntax:
- xml version="1.0" encoding="utf-8"?>
- <layer-list
- xmlns:Android="http://schemas.android.com/apk/res/android" >
- <item
- Android:drawable="@[package:]drawable/drawable_resource"
- Android:id="@[+][package:]id/resource_name"
- Android:top="dimension"
- Android:right="dimension"
- Android:bottom="dimension"
- Android:left="dimension" />
- layer-list>
="http://schemas.android.com/apk/res/android" >
-
elements:
- <item Android:drawable="@drawable/image" />
To avoid scaling, the following example uses a
element with centered gravity:
- <item>
- <bitmap Android:src="@drawable/image"
- Android:gravity="center" />
- item>
:src="@drawable/image" android:gravity="center" />
-
example:
-
XML file saved at
res/drawable/layers.xml
: