欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android setButtonDrawable()的兼容问题解决办法

程序员文章站 2023-11-21 10:42:11
android  setbuttondrawable()的兼容问题解决办法 setbuttondrawable()的兼容问题 api16实现 /*...

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)); 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!