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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
带你了解如何使用Spring基于ProxyFactoryBean创建AOP代理
超详细讲解Java异常
![](https://img.aspxhome.com/file/2023/3/80853_0s.png)
获取wince mac地址与IP地址解决方案
Java截取字符串的方法
java 查找替换pdf中的指定文本
![](https://img.aspxhome.com/file/2023/0/73110_0s.png)
springboot+spring data jpa实现新增及批量新增方式
java判断回文数示例分享
Android组合控件自定义标题栏
Java 异步编程实践_动力节点Java学院整理
Java享元设计模式优化对象创建提高性能和效率
![](https://img.aspxhome.com/file/2023/3/65633_0s.png)
java进阶之了解SpringBoot的配置原理
![](https://img.aspxhome.com/file/2023/0/64000_0s.png)
.net的命名空间类库的简单介绍
Android自定义分段式进度条
![](https://img.aspxhome.com/file/2023/6/92586_0s.jpg)
Sonar编译问题对应:File [...] can't be indexed twice.
![](https://img.aspxhome.com/file/2023/3/58903_0s.png)
springboot之如何获取项目目录路径
![](https://img.aspxhome.com/file/2023/6/60796_0s.jpg)
Android学习教程之悬浮窗菜单制作(9)
![](https://img.aspxhome.com/file/2023/7/108257_0s.jpg)
Android绘制仪表盘指针刻度
![](https://img.aspxhome.com/file/2023/2/123322_0s.gif)
springsecurity 企业微信登入的实现示例
![](https://img.aspxhome.com/file/2023/0/57490_0s.jpg)