现在的位置: 首页 > 综合 > 正文

android LinearLayout 单击动态改变背景

2012年09月23日 ⁄ 综合 ⁄ 共 801字 ⁄ 字号 评论关闭

也许大家用LinearLayout的时候会希望得到这样的效果:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/buttonbackgroundselected"/>
<item android:state_pressed="true"  android:drawable="@drawable/buttonbackgroundselected"/>
<item android:state_focused="false" android:drawable="@drawable/buttonbackgroundnormal"/>
</selector>

但是呢:

  <LinearLayout
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:background="@drawable/buttonbackground"
  >

你这样做后,会发现,即使你用手触摸了LinearLayout,那么剩下的它也不会按照你锁想的那样,动态的去改变背景的颜色,怎么解决呢?

解决方案其实非常的简单,直接添加属性:android:clickable="true"就行了

因为默认的LinearLayout是不能点击的,因此相关的事件便无法传递到LinearLayout,添加这个属性后,LinearLayout的背景便可以动态的进行变化了.

转贴:http://www.shouyanwang.org/thread-370-1-1.html

抱歉!评论已关闭.