Android setButtonDrawable()的兼容问题解决办法
发布时间 - 2026-01-11 00:08:20 点击率:次Android setButtonDrawable()的兼容问题解决办法

setButtonDrawable()的兼容问题
API16实现
/**
* Set the background to a given Drawable, identified by its resource id.
*
* @param resid the resource id of the drawable to use as the background
*/
public void setButtonDrawable(int resid) {
if (resid != 0 && resid == mButtonResource) {
return;
}
mButtonResource = resid;
Drawable d = null;
if (mButtonResource != 0) {
d = getResources().getDrawable(mButtonResource);
}
setButtonDrawable(d);
}
/**
* Set the background to a given Drawable
*
* @param d The Drawable to use as the background
*/
public void setButtonDrawable(Drawable d) {
if (d != null) {
if (mButtonDrawable != null) {
mButtonDrawable.setCallback(null);
unscheduleDrawable(mButtonDrawable);
}
d.setCallback(this);
d.setState(getDrawableState());
d.setVisible(getVisibility() == VISIBLE, false);
mButtonDrawable = d;
mButtonDrawable.setState(null);
setMinHeight(mButtonDrawable.getIntrinsicHeight());
}
refreshDrawableState();
}
API23实现
/**
* Sets a drawable as the compound button image given its resource
* identifier.
*
* @param resId the resource identifier of the drawable
* @attr ref android.R.styleable#CompoundButton_button
*/
public void setButtonDrawable(@DrawableRes int resId) {
final Drawable d;
if (resId != 0) {
d = getContext().getDrawable(resId);
} else {
d = null;
}
setButtonDrawable(d);
}
/**
* Sets a drawable as the compound button image.
*
* @param drawable the drawable to set
* @attr ref android.R.styleable#CompoundButton_button
*/
@Nullable
public void setButtonDrawable(@Nullable Drawable drawable) {
if (mButtonDrawable != drawable) {
if (mButtonDrawable != null) {
mButtonDrawable.setCallback(null);
unscheduleDrawable(mButtonDrawable);
}
mButtonDrawable = drawable;
if (drawable != null) {
drawable.setCallback(this);
drawable.setLayoutDirection(getLayoutDirection());
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
drawable.setVisible(getVisibility() == VISIBLE, false);
setMinHeight(drawable.getIntrinsicHeight());
applyButtonTint();
}
}
}
结论
RadioButton和CheckBox都是Android app中常用的Widget,它们派生于CompoundButton,允许使用者自行设置背景和按钮的样式,不过,有时我们仅希望简单的设置一个有状态的背景,并隐藏其默认样式。可是,当我们调用setButtonDrawable(null)或setButtonDrawable(0)时,却发现完全没有效果。原来,CompoundButton的setButtonDrawable的代码实现中屏蔽了null或resid为0的Drawable,迫使我们必须传入有效的Drawable对象。
这时候,透明颜色就可以派上用场了:
button.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
参考:
隐藏RadioButton, CheckBox图片 setButtonDrawable:
RadioButton和CheckBox都是Android app中常用的Widget,它们派生于CompoundButton,允许使用者自行设置背景和按钮的样式,不过,有时我们仅希望简单的设置一个有状态的背景,并隐藏其默认样式。可是,当我们调用setButtonDrawable(null)或setButtonDrawable(0)时,却发现完全没有效果。原来,CompoundButton的setButtonDrawable的代码实现中屏蔽了null或resid为0的Drawable,迫使我们必须传入有效的Drawable对象。
这时候,透明颜色就可以派上用场了:
button.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# setButtonDrawable()的兼容问题
# Android
# setButtonDrawable()兼容的问题
# Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法
# android虚拟键盘弹出遮挡登陆按钮问题的解决方法
# Android防止按钮过快点击造成多次事件的解决方法
# Android Button 自带阴影效果另一种解决办法
# Android中EditText+Button组合导致输入板无法收起的原因分析及解决办法
# 实例详解Android解决按钮重复点击问题
# Android开发软键盘遮挡登陆按钮的完美解决方案
# Android优雅的方式解决软键盘遮挡按钮问题
# Android编程出现Button点击事件无效的解决方法示例
# 都是
# 当我们
# 我们必须
# 就可以
# 派上
# 这时候
# 希望能
# 谢谢大家
# 解决办法
# return
# null
# false
# getResources
# mButtonResource
# void
# public
# amp
# int
# getDrawableState
# setVisible
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
百度输入法全感官ai怎么关 百度输入法全感官皮肤关闭
海南网站制作公司有哪些,海口网是哪家的?
利用JavaScript实现拖拽改变元素大小
BootStrap整体框架之基础布局组件
开心动漫网站制作软件下载,十分开心动画为何停播?
Laravel怎么生成二维码图片_Laravel集成Simple-QrCode扩展包与参数设置【实战】
C++用Dijkstra(迪杰斯特拉)算法求最短路径
Laravel怎么配置.env环境变量_Laravel生产环境敏感数据保护与读取【方法】
Laravel怎么配置S3云存储驱动_Laravel集成阿里云OSS或AWS S3存储桶【教程】
今日头条微视频如何找选题 今日头条微视频找选题技巧【指南】
Laravel如何正确地在控制器和模型之间分配逻辑_Laravel代码职责分离与架构建议
linux top下的 minerd 木马清除方法
Win11怎么关闭资讯和兴趣_Windows11任务栏设置隐藏小组件
昵图网官方站入口 昵图网素材图库官网入口
怎么制作网站设计模板图片,有电商商品详情页面的免费模板素材网站推荐吗?
微信小程序 input输入框控件详解及实例(多种示例)
如何选择PHP开源工具快速搭建网站?
如何为不同团队 ID 动态生成多个“认领值班”按钮
Laravel怎么判断请求类型_Laravel Request isMethod用法
Laravel Seeder怎么填充数据_Laravel数据库填充器的使用方法与技巧
夸克浏览器网页跳转延迟怎么办 夸克浏览器跳转优化
Laravel Livewire是什么_使用Laravel Livewire构建动态前端界面
Laravel怎么在Controller之外的地方验证数据
深圳网站制作平台,深圳市做网站好的公司有哪些?
Laravel项目如何进行性能优化_Laravel应用性能分析与优化技巧大全
如何实现javascript表单验证_正则表达式有哪些实用技巧
网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?
Laravel distinct去重查询_Laravel Eloquent去重方法
图册素材网站设计制作软件,图册的导出方式有几种?
C语言设计一个闪闪的圣诞树
Python并发异常传播_错误处理解析【教程】
php485函数参数是什么意思_php485各参数详细说明【介绍】
javascript读取文本节点方法小结
js实现获取鼠标当前的位置
移动端脚本框架Hammer.js
Laravel Vite是做什么的_Laravel前端资源打包工具Vite配置与使用
Bootstrap整体框架之JavaScript插件架构
Laravel Eloquent模型如何创建_Laravel ORM基础之Model创建与使用教程
Laravel如何与Docker(Sail)协同开发?(环境搭建教程)
悟空浏览器如何设置小说背景色_悟空浏览器背景色设置【方法】
如何用虚拟主机快速搭建网站?详细步骤解析
浅述节点的创建及常见功能的实现
Laravel如何创建自定义Facades?(详细步骤)
香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化
html5audio标签播放结束怎么触发事件_onended回调方法【教程】
百度浏览器如何管理插件 百度浏览器插件管理方法
利用python获取某年中每个月的第一天和最后一天
怎么用AI帮你为初创公司进行市场定位分析?
Laravel怎么实现一对多关联查询_Laravel Eloquent模型关系定义与预加载【实战】
如何生成腾讯云建站专用兑换码?

