android 自定义图形之层叠样式 [layer-list] 的使用

[size=small] 通过使用层叠样式 layer-list 可以将多张图像层叠在一起 形成一张新的图像.
在第一次接触, 是为了解决一个LinearLayout 中包含一个Button ,当点击该LinearLayout和Button 多要改变LinearLayout的背景色. 由于Button无法获取焦点改变事件(OnFocusChangeListener), 而设置Button点击事件我们也只能扑捉按钮的一瞬间, 所以我们还的扑捉手势动作(GestureDetector) . 而且这些多要代码去控制,[/size]
[b][color=red]听说 ImageButton能解决Button的这一弱点哦..[/color][/b]

下面是效果图

[img]http://dl2.iteye.com/upload/attachment/0095/3566/d97375b4-3543-3aeb-b2a2-3c7b448c50a2.png[/img]

[img]http://dl2.iteye.com/upload/attachment/0095/3570/09734a60-0a49-3a88-9d8c-3d6ade0d7e93.png[/img]

接下来发的是drawable xml图像文件

1. 默认的图片布局 : layer_main_button_life_n.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:drawable="@drawable/login_btn_normal" />
<item android:drawable="@drawable/main_icon_life"
android:left="20dip"
android:right="150.0dip"
android:top="8dip"
android:bottom="8dip"/>

</layer-list>


2. 按下 或者有焦点的图像布局:layer_main_button_life_p.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:drawable="@drawable/login_btn_focus" />
<item android:drawable="@drawable/main_icon_life"
android:left="20dip"
android:right="150.0dip"
android:top="8dip"
android:bottom="8dip"/>

</layer-list>


3. 为新的图像创建一个选择器布局 :selector_main_button_life.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/layer_main_button_life_p" android:state_pressed="true"/>
<item android:drawable="@drawable/layer_main_button_life_p" android:state_selected="true"/>
<item android:drawable="@drawable/layer_main_button_life_n"/>

</selector>


4. 最后在布局文件中 main.xml中的Button直接引用该选择器就OK了

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/selector_main_button_life"
android:paddingLeft="100dip"
android:text=" 生 活 资 讯 "
android:textSize="20sp"/>

</RelativeLayout>



注: layer-list 对应的Java类为LayerDrawable ,大家也可能使用该类在类中创建自己的图像
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值