全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

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


# setButtonDrawable()的兼容问题  # Android  # setButtonDrawable()兼容的问题  # Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法  # android虚拟键盘弹出遮挡登陆按钮问题的解决方法  # Android防止按钮过快点击造成多次事件的解决方法  # Android Button 自带阴影效果另一种解决办法  # Android中EditText+Button组合导致输入板无法收起的原因分析及解决办法  # 实例详解Android解决按钮重复点击问题  # Android开发软键盘遮挡登陆按钮的完美解决方案  # Android优雅的方式解决软键盘遮挡按钮问题  # Android编程出现Button点击事件无效的解决方法示例  # 都是  # 当我们  # 我们必须  # 就可以  # 派上  # 这时候  # 希望能  # 谢谢大家  # 解决办法  # return  # null  # false  # getResources  # mButtonResource  # void  # public  # amp  # int  # getDrawableState  # setVisible 


相关文章: c# await 一个已经完成的Task会发生什么  如何通过建站之星自助学习解决操作问题?  如何通过老薛主机一键快速建站?  网站制作的步骤包括,正确网址格式怎么写?  阿里云网站制作公司,阿里云快速搭建网站好用吗?  如何选择高性价比服务器搭建个人网站?  胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?  大连网站设计制作招聘信息,大连投诉网站有哪些?  企业网站制作费用多少,企业网站空间一般需要多大,费用是多少?  湖州网站制作公司有哪些,浙江中蓝新能源公司官网?  保定网站制作方案定制,保定招聘的渠道有哪些?找工作的人一般都去哪里看招聘信息?  上海网站制作网页,上海本地的生活网站有哪些?最好包括生活的各个方面的?  建站之星免费模板:自助建站系统与智能响应式一键生成  北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?  太原网站制作公司有哪些,网约车营运证查询官网?  网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?  网站制作公司排行榜,抖音怎样做个人官方网站  如何在云虚拟主机上快速搭建个人网站?  网站制作哪家好,cc、.co、.cm哪个域名更适合做网站?  建站之星如何防范黑客攻击与数据泄露?  如何在云主机快速搭建网站站点?  Android滚轮选择时间控件使用详解  如何确保FTP站点访问权限与数据传输安全?  盘锦网站制作公司,盘锦大洼有多少5G网站?  常州企业建站如何选择最佳模板?  如何通过cPanel快速搭建网站?  想学网站制作怎么学,建立一个网站要花费多少?  公司网站制作需要多少钱,找人做公司网站需要多少钱?  建站之星好吗?新手能否轻松上手建站?  建站之星多图banner生成与模板自定义指南  如何通过NAT技术实现内网高效建站?  北京的网站制作公司有哪些,哪个视频网站最好?  如何通过IIS搭建网站并配置访问权限?  如何选择高效便捷的WAP商城建站系统?  如何高效配置IIS服务器搭建网站?  如何使用Golang table-driven基准测试_多组数据测量函数效率  早安海报制作网站推荐大全,企业早安海报怎么每天更换?  c++怎么实现高并发下的无锁队列_c++ std::atomic原子变量与CAS操作【详解】  如何在建站之星绑定自定义域名?  网站制作和推广的区别,想自己建立一个网站做推广,有什么快捷方法马上做好一个网站?  c++怎么用jemalloc c++替换默认内存分配器【性能】  微信小程序制作网站有哪些,微信小程序需要做网站吗?  如何选择建站程序?包含哪些必备功能与类型?  建站主机如何选?高性价比方案全解析  如何在腾讯云免费申请建站?  宁波免费建站如何选择可靠模板与平台?  成都品牌网站制作公司,成都营业执照年报网上怎么办理?  seo网站制作优化,网站SEO优化步骤有哪些?  我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?  专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。