Android setButtonDrawable()的兼容问题解决办法
作者:ihrthk 发布时间:2021-12-11 04:33:52
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));
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/ihrthk/article/details/61442658


猜你喜欢
- 样式效果 还是先来看效果: 这是一个仿雷达扫描的效果,是之前在做地图sdk接入时就想实现的效果,但之前由于赶着毕业设
- 1.springBoot的依赖确定项目中包含可以注解的依赖<dependency> <group
- C#重绘checkbox生成滑动开关,供大家参考,具体内容如下通过调用checkbox控件的paint事件,在重绘事件里判断checked属
- 前面我们讲到了Spring在进行事务逻辑织入的时候,无论是事务开始,提交或者回滚,都会触发相应的事务事件。本文首先会使用实例进行讲解Spri
- 参考dubbo和shenyu网关实现自定义的SPISPI标注注解标注提供SPI能力接口的注解@Documented@Retention(Re
- 数组array和集合的区别:(1) 数值是大小固定的,同一数组只能存放一样的数据。(2) java集合可以存放不固定的一组数据(3) 若程序
- JSON Web Token(JWT)是目前最流行的跨域身份验证解决方案。为了网络应用环境间传递声明而执行的一种基于JSON的开发标准(RF
- 前言之前写过 Mybatis Plus 的基本配置和使用。Mybatis-Plus 看这一篇就够了当初在进行查询时,虽然没有写硬SQL进行查
- 一般的 Executors 的 execute以及submit并发包下 Executors 创建的线程存在 一个 execute(),以及三
- 在前面仿华为加载动画、仿网易音乐听歌识曲-麦克风动画中,我们通过绘图的基础知识完成了简单的绘制。在本例中,我们将绘制常见的验证码。一、效果图
- Java获取环境变量Java 获取环境变量的方式很简单: System.getEnv() 得到所有的环境变量Syste
- android 在webView里面截图大概有四种方式,具体内容如下1.获取到DecorView然后将DecorView转换成bitmap然
- 一、findById(ID id)通过id进行单条查询,先看看 findById(ID id) 的源码@Overridepublic Opt
- SpringBoot入门Demo,一次深夜踩坑记录。springboot小项目开启后,访问端口无反应。首先看我的项目目录:项目的pom文件内
- 程序中的错误分为编译时的错误和运行时的错误。编译时的错误主要是语法错误,比如:句尾没有加分号,括号不匹配,关键字错误等,这类错误比较容易修改
- C# char类型有自带的大小写转换方法:ToUpper和ToLowerchar str1 = 'a';char str2
- 零、学习目标1、掌握application.properties配置文件2、掌握application.yaml配置文件3、掌握使用@Con
- 前言上一篇做了一个滑动折叠的Header控件,主要就是练习了一下滑动事件冲突的问题,控件和文章写的都不怎么样。本来想通过这篇文章的控件,整合
- 前言事务对java开发的同学来说并不陌生,我们使用事务的目的在于避免产生重复数据或者说利用数据存储中间件的事务特性确保数据的精准性,比如大家
- 最近再开发中遇到需要将文件上传到Linux服务器上,至此整理代码笔记。此种连接方法中有考虑到并发问题,在进行创建FTP连接的时候