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
相关文章:
如何自定义建站之星模板颜色并下载新样式?
C++中的Pimpl idiom是什么,有什么好处?(隐藏实现)
成都网站制作公司哪家好,四川省职工服务网是做什么用?
外贸公司网站制作哪家好,maersk船公司官网?
如何在VPS电脑上快速搭建网站?
如何快速生成可下载的建站源码工具?
如何构建满足综合性能需求的优质建站方案?
建站主机选购指南:核心配置与性价比推荐解析
h5在线制作网站电脑版下载,h5网页制作软件?
个人摄影网站制作流程,摄影爱好者都去什么网站?
如何快速搭建高效服务器建站系统?
湖州网站制作公司有哪些,浙江中蓝新能源公司官网?
定制建站流程步骤详解:一站式方案设计与开发指南
网站制作培训多少钱一个月,网站优化seo培训课程有哪些?
建站一年半SEO优化实战指南:核心词挖掘与长尾流量提升策略
建站之星图片链接生成指南:自助建站与智能设计教程
整蛊网站制作软件,手机不停的收到各种网站的验证码短信,是手机病毒还是人为恶搞?有这种手机病毒吗?
c# F# 的 MailboxProcessor 和 C# 的 Actor 模型
如何处理“XML格式不正确”错误 常见XML well-formed问题解决方法
武汉网站制作费用多少,在武汉武昌,建面100平方左右的房子,想装暖气片,费用大概是多少啊?
南京做网站制作公司,南京哈发网络有限公司,公司怎么样,做网页美工DIV+CSS待遇怎么样?
如何在腾讯云服务器快速搭建个人网站?
购物网站制作费用多少,开办网上购物网站,需要办理哪些手续?
如何获取免费开源的自助建站系统源码?
Python如何创建带属性的XML节点
制作证书网站有哪些,全国城建培训中心证书查询官网?
Android自定义listview布局实现上拉加载下拉刷新功能
如何确保FTP站点访问权限与数据传输安全?
如何通过云梦建站系统实现SEO快速优化?
小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化
如何解决VPS建站LNMP环境配置常见问题?
建站之星导航菜单设置与功能模块配置全攻略
高防网站服务器:DDoS防御与BGP线路的AI智能防护方案
香港服务器WordPress建站指南:SEO优化与高效部署策略
专业制作网站的公司哪家好,建立一个公司网站的费用.有哪些部分,分别要多少钱?
制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?
建站10G流量真的够用吗?如何应对访问高峰?
较简单的网站制作软件有哪些,手机版网页制作用什么软件?
佛山企业网站制作公司有哪些,沟通100网上服务官网?
建站主机服务器选购指南:轻量应用与VPS配置解析
青岛网站建设如何选择本地服务器?
如何实现建站之星域名转发设置?
详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
教学论文网站制作软件有哪些,写论文用什么软件
?
平台云上自主建站:模板化设计与智能工具打造高效网站
建站之星在线版空间:自助建站+智能模板一键生成方案
电视网站制作tvbox接口,云海电视怎样自定义添加电视源?
*服务器网站为何频现安全漏洞?
网站制作企业,网站的banner和导航栏是指什么?
建站之星安装路径如何正确选择及配置?
*请认真填写需求信息,我们会在24小时内与您取得联系。