setButtonDrawable()的兼容问题
来源:互联网 发布:javascript随机整数 编辑:程序博客网 时间:2024/06/06 02:05
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(http://blog.csdn.net/changemyself/article/details/44809411)
1 0
- setButtonDrawable()的兼容问题
- 隐藏RadioButton, CheckBox的drawable图片 setButtonDrawable
- RadioButton在Android4.4调用setButtonDrawable(null)无效的问题
- 隐藏RadioButton, CheckBox图片 setButtonDrawable
- 隐藏RadioButton, CheckBox图片 setButtonDrawable
- RadioButton调用setButtonDrawable(null)无效
- 一个浏览器的兼容问题
- CSS的兼容问题
- css+div的兼容问题
- IE6的几点不兼容问题
- FrameWork4.0的兼容问题
- 浏览器兼容问题的解决方案
- 浏览器的兼容问题
- FrameWork4.0的兼容问题 .
- 浏览器的兼容问题?
- IE6、IE7的兼容问题
- nodeValue的兼容问题
- IE8的兼容问题
- window下VI编辑器的使用
- bzoj1060[ZJOI2007] 时态同步
- MQTT---HiveMQ源码详解(十三)Netty-MQTT消息、事件处理(源码举例解读)
- LeetCode 24. Swap Nodes in Pairs
- String的特点--一旦被创建就不能被改变
- setButtonDrawable()的兼容问题
- MySql中的varchar类型
- 好久没有更新文章了
- javascript作用域和闭包之我见
- Layout动画:在android布局发生变化时添加动画效果
- servlet学习一
- hdu 1301 最小生成树
- 蓝桥杯ALGO-113——算法训练 数的统计
- HDU 2099