Android– 如何为Layouts创建选择器(如ImageButton选择器)

Android– 如何为Layouts创建选择器(如ImageButton选择器),第1张

概述我有一个ImageButton和一个LinearLayout包装该按钮,如下所示:<LinearLayoutandroid:id="@+id/homeButtonLayout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="

我有一个Imagebutton和一个linearLayout包装该按钮,如下所示:

<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>

我在linearLayout中围绕Imagebutton放置了一个背景,如上所示.

我希望能够在用户单击Imagebutton时更改该背景(即linearLayout的背景,而不是Imagebutton的背景).我可以通过编程方式做到这一点,但我想通过XML来做到这一点.我可以通过编写一个选择器轻松更改Imagebutton的背景,如下所示:

    <?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背景?

先感谢您!! =)

解决方法:

将选择器设置为linearLayout并将androID:descendantFocusability =“blocksDescendants”添加到它.

在选择器中,用布局背景替换所有按钮背景.

<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>
总结

以上是内存溢出为你收集整理的Android – 如何为Layouts创建选择器(如ImageButton选择器)全部内容,希望文章能够帮你解决Android – 如何为Layouts创建选择器(如ImageButton选择器)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1096084.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存