Android:使用选择器更改按钮背景图像和颜色

Android:使用选择器更改按钮背景图像和颜色,第1张

概述我有一个按钮,将其背景设置为特定的选择器.选择器当前更改按钮背景,并将图像更改为背景.我还希望更改背景色(图像是带有透明空间的图标).这是选择器:<?xmlversion="1.0"encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/

我有一个按钮,将其背景设置为特定的选择器.
选择器当前更改按钮背景,并将图像更改为背景.
我还希望更改背景色(图像是带有透明空间的图标).
这是选择器:

    <?xml version="1.0" enCoding="utf-8"?>        <selector             xmlns:androID="http://schemas.androID.com/apk/res/androID" >        <!-- default -->        <item             androID:state_pressed="false"             androID:state_focused="false"            androID:drawable="@drawable/menu_button_collapsed" >        </item>        <!-- button focused -->        <item             androID:state_pressed="false"             androID:state_focused="true"            androID:drawable="@drawable/menu_button_collapsed_highlight"             androID:drawable="@drawable/button_background" >        </item>       <!-- button pressed -->        <item             androID:state_pressed="true"             androID:state_focused="false"            androID:drawable="@drawable/menu_button_collapsed_highlight"            androID:drawable="@drawable/button_background" >        </item>    </selector>

如您所见,我两次设置了drawable属性,这是非法的,但这实际上是我想要的.
注意@ drawable / button_background只是一种颜色

解决方法:

创建一个新的< @R_491_3419@>可绘制

custom_button.xml

<?xml version="1.0" enCoding="utf-8"?>                <@R_491_3419@ xmlns:androID="http://schemas.androID.com/apk/res/androID">           <!-- Your background color goes first -->           <item              androID:ID="@androID:ID/background"             androID:drawable="@drawable/button_background" />           <!-- Your button icon image -->           <item              androID:ID="@androID:ID/button_image"             androID:drawable="@drawable/menu_button_collapsed_highlight" />        </@R_491_3419@>

并在您的选择器可绘制文件中引用它

<?xml version="1.0" enCoding="utf-8"?>    <selector xmlns:androID="http://schemas.androID.com/apk/res/androID" >        <!-- default -->        <item             androID:state_pressed="false"             androID:state_focused="false"            androID:drawable="@drawable/menu_button_collapsed"          />                    <!-- button focused -->        <item             androID:state_pressed="false"             androID:state_focused="true"            androID:drawable="@drawable/custom_button"             />        <!-- button pressed -->        <item             androID:state_pressed="true"             androID:state_focused="false"            androID:drawable="@drawable/custom_button"          />    </selector>
总结

以上是内存溢出为你收集整理的Android:使用选择器更改按钮背景图像和颜色全部内容,希望文章能够帮你解决Android:使用选择器更改按钮背景图像和颜色所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1077491.html

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

发表评论

登录后才能评论

评论列表(0条)

保存