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源码中常用的接口传参实例详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • 使用RoundedBitmapDrawable生成圆角图片的方法

    使用RoundedBitmapDrawable生成圆角图片的方法

    由于RoundedBitmapDrawable类没有直接提供生成圆形图片的方法,所以生成圆形图片首先需要对原始图片进行裁剪,将图片裁剪成正方形,最后再生成圆形图片,具体实现方法,可以参考下本文
    2016-09-09
  • Android异步下载图片并且缓存图片到本地DEMO详解

    Android异步下载图片并且缓存图片到本地DEMO详解

    这篇文章主要介绍了Android异步下载图片并且缓存图片到本地DEMO详解,需要的朋友可以参考下
    2017-04-04
  • Android RecyclerView布局就这么简单

    Android RecyclerView布局就这么简单

    Android RecyclerView布局就这么简单!RecyclerView比ListView更灵活,更强大,作为一个android开发者如果还不知道如何使用android5.X的RecyclerView未免有点说不过去了,本文就为大家讲解Android RecyclerView布局,需要的朋友可以参考下
    2016-04-04
  • 基于Android平台实现拼图小游戏

    基于Android平台实现拼图小游戏

    这篇文章主要为大家详细介绍了基于Android平台实现拼图小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Android菜单的定义及ActionBar的实现

    Android菜单的定义及ActionBar的实现

    本篇文章主要介绍了Android菜单的定义及ActionBar的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Kotlin launch原理全面分析

    Kotlin launch原理全面分析

    在Android开发中,launch是我们经常用的,今天来看看它是什么原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-11-11
  • Android开发技巧之ViewStub控件惰性装载

    Android开发技巧之ViewStub控件惰性装载

    布局文件中的控件并不一定在程序启动时全都用到,有一些控件只在特定的情况下才会被使用到;我们急需一种机制来改变<include>标签的这种行为,只在需要时装载控件。这种机制就是本节要介绍的ViewStub控件
    2013-01-01
  • Jetpack Compose Text的基本使用

    Jetpack Compose Text的基本使用

    这篇文章主要介绍了Jetpack Compose Text的基本使用,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • Android实现垂直跑马灯效果

    Android实现垂直跑马灯效果

    这篇文章主要为大家详细介绍了Android实现垂直跑马灯效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04

最新评论