首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android自定义Shape 加上影子shadow之方法

2012-07-25 
Android自定义Shape 加上阴影shadow之方法Android支持自定义Shape, 以画出需要的形状,可以作为TextView, E

Android自定义Shape 加上阴影shadow之方法

Android支持自定义Shape, 以画出需要的形状,可以作为TextView, EditText, Button的背景drawable资源。Shape很简单,就是一个XML文件,SDK文档里描述其格式如下:

?

?

其支持的属性没有shadow, 做Web前端开发的同学写CSS可以很方便地加一个shadow属性值,如何给Android Shape加一个shadow,以得到类似的效果呢?

?

答案是使用layer-list !?? 直接上代码如下:

?

?

将以上xml存成btn_test, 放到res/drawable/目录下。 将该drawable xml设为一个TextView的backgroiund,

?

?

?

其效果如下图所示:

?

Android自定义Shape 加上影子shadow之方法

?

?

关于layer-list的进一步解释见SDK文档,如下:

?

?

Layer List

A LayerDrawable is a drawable objectthat manages an array of other drawables. Each drawable in the list is drawn in the order of thelist—the last drawable in the list is drawn on top.

Each drawable is represented by an <item> element inside a single <layer-list> 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/filenamesyntax:

?

?

elements:<layer-list>Required. This must be the root element. Contains one or more <item> elements.

?

To avoid scaling, the following example uses a <bitmap> element with centeredgravity:

?example:XML file saved at res/drawable/layers.xml:


Notice that this example uses a nested <bitmap> element to define the drawableresource for each item with a "center" gravity. This ensures that none of the images are scaled tofit the size of the container, due to resizing caused by the offset images.

This layout XML applies the drawable to a View:

?

This layout XML applies the drawable to a View:

?

?

The result is a stack of increasingly offset images:

Android自定义Shape 加上影子shadow之方法see also:LayerDrawable?

热点排行