在Android中删除ImageButton的隐藏背景但保持onClick突出显示

在Android中删除ImageButton的隐藏背景但保持onClick突出显示,第1张

概述我的应用程序中有一些ImageButtons,我删除了默认的灰色背景android:background=“@android:color/transparent”或android:background=“@null”问题是,它还删除了onClick高亮背景(API8中的橙色和API16中的蓝色)我在这里阅读了很多Q&A,人们都建议使用选择器.而不是为每个按

我的应用程序中有一些Imagebuttons,我删除了默认的灰色背景

androID:background =“@ androID:color / transparent”或androID:background =“@ null”

问题是,它还删除了onClick高亮背景(API8中的橙色和API16中的蓝色)

我在这里阅读了很多Q& A,人们都建议使用选择器.而不是为每个按钮制作另一个图像,我想只有背景颜色突出显示.有没有一种简单的方法来实现这一目标?

解决方案:以编程方式

import androID.vIEw.VIEw;import androID.vIEw.VIEw.OntouchListener;// ...btn.setontouchListener(new OntouchListener() {    @OverrIDe    public boolean ontouch(VIEw v, MotionEvent event) {        if(event.getAction() == (MotionEvent.ACTION_DOWN)) {            // set background highlight color            btn.setBackgroundResource(R.color.blue);        }        if(event.getAction() == (MotionEvent.ACTION_UP)) {            // restore transparent            btn.setBackgroundResource(                getResources().getcolor(androID.R.color.transparent));        }        return false;    }});

它不是那么简单,但是做了工作并节省了我很多时间为每个按钮制作另一个图像,而我真的不需要花哨的onclick样式.

解决方法:

你真的需要使用一个选择器.

您可以在选择器中使用背景颜色,它不需要是PNG资源(只需将背景设置为颜色,而不是可绘制资源).

您将此代码放在具有特定名称(如button_sel.xml)的drawable文件夹中.

然后将其设置为XML中按钮的背景,如下所示:

androID:background="@drawable/button_sel"

选择器看起来像这样:

<selector    xmlns:androID="http://schemas.androID.com/apk/res/androID">    <item        androID:drawable="@color/ab_background"        androID:state_focused="true"        androID:state_pressed="true"/>    <item        androID:drawable="@color/ab_background"        androID:state_focused="false"        androID:state_pressed="true"/>    <item        androID:drawable="@color/ab_background_on"        androID:state_focused="true"        androID:state_pressed="false"/>    <item        androID:drawable="@color/ab_background_on"        androID:state_focused="false"        androID:state_pressed="false"/></selector>
总结

以上是内存溢出为你收集整理的在Android中删除ImageButton的隐藏背景但保持onClick突出显示全部内容,希望文章能够帮你解决在Android中删除ImageButton的隐藏背景但保持onClick突出显示所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存