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

 更新时间:2017年03月13日 09:50:33   作者:ihrthk  
这篇文章主要介绍了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)); 

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

相关文章

  • 解析android中系统日期时间的获取

    解析android中系统日期时间的获取

    本篇文章是对在android中,如何系统日期时间获取的方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • Kotlin fun函数使用方法

    Kotlin fun函数使用方法

    函数是执行特定任务的一组相互关联的代码块。函数用于将程序分解为不同的子模块。它使代码可重用,并使程序更易于管理,这篇文章主要介绍了Kotlin fun函数使用方法
    2022-12-12
  • Android Touch事件分发过程详解

    Android Touch事件分发过程详解

    这篇文章主要介绍了Android Touch事件分发过程,详细描述了Android Touch事件的主要处理流程,有助于深入理解Android程序设计,需要的朋友可以参考下
    2014-09-09
  • 详解Android Bitmap的常用压缩方式

    详解Android Bitmap的常用压缩方式

    这篇文章主要介绍了详解Android Bitmap的常用压缩方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Android控件之GridView用法实例分析

    Android控件之GridView用法实例分析

    这篇文章主要介绍了Android控件之GridView用法,通过绘制九宫格的实例形式分析了GridView可滚动网格的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • 浅析Android中build.gradle的实用技巧

    浅析Android中build.gradle的实用技巧

    这篇文章主要介绍了浅析Android中build.gradle的实用技巧,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • Android 使用PDF.js浏览pdf的方法示例

    Android 使用PDF.js浏览pdf的方法示例

    这篇文章主要介绍了Android 使用PDF.js浏览pdf的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • 详解Android Webview加载网页时发送HTTP头信息

    详解Android Webview加载网页时发送HTTP头信息

    这篇文章主要介绍了详解Android Webview加载网页时发送HTTP头信息的相关资料,需要的朋友可以参考下
    2017-05-05
  • 详解Android Studio中Git的配置及协同开发

    详解Android Studio中Git的配置及协同开发

    这篇文章主要介绍了详解Android Studio中Git的配置及协同开发,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • App中如何获取gradle的配置信息

    App中如何获取gradle的配置信息

    这篇文章主要给大家介绍了关于App中如何获取gradle的配置信息的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-02-02

最新评论