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

所属分类: 软件编程 / Android 阅读数: 15
收藏 0 赞 0 分享

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

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

更多精彩内容其他人还在看

Android实现悬浮窗体效果

这篇文章主要为大家详细介绍了Android实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多