Android自定义Shape 加上阴影shadow之方法
来源:LinuxIDC.com 作者:LinuxIDC.com
Android支持自定义Shape, 以画出需要的形状,可以作为TextView, EditText, Button的背景drawable资源。Shape很简单,就是一个XML文件,SDK文档里描述其格式如下:
- xml version="1.0" encoding="utf-8"?>
- <shape
- xmlns:Android="http://schemas./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./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./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>
- elements:
-
- Required. This must be the root element. Contains one or more
elements.
attributes:
xmlns:Android
- String . Required. Defines the XML namespace, which must be
"http://schemas./apk/res/android" .
- Defines a drawable to place in the layer drawable, in a position defined by its attributes. Must be a child of a
element. Accepts child elements.
attributes:
Android:drawable
- Drawable resource . Required . Reference to a drawable resource.
android:id
- Resource ID . A unique resource ID for this drawable. To create a new resource ID for this item, use the form:
"@+id/name " . The plus symbol indicates that this should be created as a new ID. You can use this identifier to retrieve and modify the drawable with View.findViewById() or Activity.findViewById() .
android:top
- Integer . The top offset in pixels.
android:right
- Integer . The right offset in pixels.
android:bottom
- Integer . The bottom offset in pixels.
android:left
- Integer . The left offset in pixels.
All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate. To avoid scaling items in the list, use a element inside the element to specify the drawable and define the gravity to something that does not scale, such as "center" . For example, the following defines an item that scales to fit its container View:
- <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>
-
- example:
- XML file saved at
res/drawable/layers.xml :
-
|