setBackgroundResource的一个问题


一,<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android

    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:background="#CC9933" >//给父控件设置了一个全局背景, spa


二,<RelativeLayout
                android:id="@+id/navigation_home_btn"
                android:layout_width="wrap_content"
                android:layout_height="52.0dip"
                android:layout_weight="1.0"

             android:background="@drawable/navigation_homebutton" >//而后在子控件设置了一个selector的选择器背景 xml

结果,点击子控件中的背景没有任何效果。 ip

原来,在给子控件设置背景时,须要将其父控件上的背景清空,这样子控件中的背景才会有效果,在代码中作以下设置: io

relativeHome.setBackgroundResource(0);//先清空上面的背景
relativeHome.setBackgroundResource(R.drawable.navigation_homebutton);//在设置新的背景 select

而后在点击时,就ok了 layout

相关文章
相关标签/搜索