Android setButtonDrawable()的兼容问题解决办法
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));
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
下一篇: PHP生成二维码的两个方法和实例
推荐阅读
-
android studio 一直卡在Gradle:Build Running的几种解决办法
-
Android setButtonDrawable()的兼容问题解决办法
-
Android 中 android.view.WindowLeaked的解决办法
-
Android Studio 运行时出现的警告信息解决办法
-
Android view更改背景资源与padding消失的问题解决办法
-
Vue中android4.4不兼容问题的解决方法
-
Android studio 下的APK打包失败问题解决办法
-
Android的App启动时白屏的问题解决办法
-
Android Studio虚拟机启动的解决办法
-
Android 使用volley过程中遇到的问题解决办法