Android - How can I create a selector for Layouts

2020-03-29 02:43发布

问题:

I have an ImageButton and a LinearLayout wrapping that button, like this:

<LinearLayout
        android:id="@+id/homeButtonLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/p_buttons_background"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/homeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10px"
            android:background="@drawable/home_icon"
            android:onClick="goBackToCameraScreen" />
    </LinearLayout>

I put a background around the ImageButton in the LinearLayout, as you can see above.

I want to be able to change that background (that is, the background of the LinearLayout, not the background of the ImageButton) when the user clicks on the ImageButton. I could do that programmatically, but I want to do that via XML. I could easily change the background of the ImageButton by writing a selector, like this:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
       android:drawable="@drawable/button_pressed" /> <!-- pressed -->
     <item android:state_focused="true"
       android:drawable="@drawable/button_focused" /> <!-- focused -->
     <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

That's EXACTLY what I want, except that I want to change the LinearLayout background, and not the ImageButton background. How can I create a XML selector just like the one above, but that actually changes the background of the LinearLayout instead of the ImageButton background??

Thank you in advance!! =)

回答1:

Set the selector to your LinearLayout and add android:descendantFocusability="blocksDescendants" to it.

In selector, replace all button backgrounds with layout backgrounds.

<LinearLayout
    android:id="@+id/homeButtonLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/layout_bg_selector"
    android:orientation="vertical" 
    android:descendantFocusability="blocksDescendants">

    <ImageButton
        android:id="@+id/homeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10px"
        android:background="@drawable/home_icon"
        android:onClick="goBackToCameraScreen" />
</LinearLayout>

selector layout_bg_selector.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
       android:drawable="#F0F0F0" /> <!-- pressed -->
     <item android:state_focused="true"
       android:drawable="#00F000" /> <!-- focused -->
     <item android:drawable="#0000F0" /> <!-- default -->
</selector>