点击LinearLayout动态改变其背景

来源:互联网 发布:通用软件无线电平台 编辑:程序博客网 时间:2024/05/26 16:01
也许大家用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的背景便可以动态的进行变化了.

原创粉丝点击