Android的 - 我怎样才能创造一个布局选择器(类似的ImageButton选择器) [英] Android - How can I create a selector for Layouts (like an ImageButton selector)
问题描述
我有一个ImageButton的和包装的LinearLayout该按钮,就像这样:
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>
我把周围的ImageButton的背景中的LinearLayout ,因为你可以在上面看到。
I put a background around the ImageButton in the LinearLayout, as you can see above.
我希望能够改变这种背景下(即的的LinearLayout 的背景下,ImageButton的不是背景),当用户点击的ImageButton。我能做到这一点编程,但我想这样做,通过XML。我可以很容易通过编写一个选择,这样的改变的ImageButton 的背景
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>
这就是 完全的我想要什么,但我想的修改的LinearLayout的背景,而不是ImageButton的背景。如何创建一个XML选择,就像上面的那个,但实际上改变的LinearLayout的背景,而在后台的ImageButton ??
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??
感谢你在前进! =)
推荐答案
选择设置为您的的LinearLayout
并添加的android:descendantFocusability =blocksDescendants
来了。
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>
选择 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>
这篇关于Android的 - 我怎样才能创造一个布局选择器(类似的ImageButton选择器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!