当前位置:   article > 正文

Android 兼容性介绍之setBackground_android setbackground

android setbackground

简介

相信大家都使用过各种布局,而对于各种布局都会使用到设置背景图片这个功能

市场上现在Android手机基本上大多数都是在4.4.4版本或以上,但是老一些的手机还是存的

所以针对Android的兼容性问题,我们还是要好好处理的。


处理方案

我们如何处理Android的兼容性问题了?

根本:就是对sdk版本所缺少的类或者方法做出相应处理即可


案例分析

案例一:setBackground

相信大家都或多或少的使用过,LinearLayout.setBackgroud(Drawable background) 但是有没有发现问题?

/**
     * Set the background to a given Drawable, or remove the background. If the
     * background has padding, this View's padding is set to the background's
     * padding. However, when a background is removed, this View's padding isn't
     * touched. If setting the padding is desired, please use
     * {@link #setPadding(int, int, int, int)}.
     *
     * @param background The Drawable to use as the background, or null to remove the
     *        background
     */
    public void setBackground(Drawable background) {
        //noinspection deprecation
        setBackgroundDrawable(background);
    }

    /**
     * @deprecated use {@link #setBackground(Drawable)} instead
     */
    @Deprecated
    public void setBackgroundDrawable(Drawable background) {
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

@Deprecated 发现了么,这里调用setBackgroundDrawable其实已经过时了,不推荐使用该方法

使用Android Studio 的 windows+f2可以查看到如下信息

这里写图片描述

这个的意思是,在Api 16之后才有这个方法

也就是Android 4.1 Jelly Bean (API level 16) 之后才有这个方法,

如果在这之前的手机,调用这个方法,就会出现NoSuchMethodError错误

所以如果使用该方法就要做兼容处理,当然,你也可以使用setBackgroundResource方法代替

这个方法是从Api 1开始就有,所以就不用担心了。


案例二:Android 6.0删除HttpClient的相关类

解决方案:http://blog.csdn.net/yangqingqo/article/details/48214865


总结

以上就是setBackground兼容性的问题,解决,当然还有其他很多问题,暂时不举例了,碰到了,在依样画葫芦就可以直接解决,thanks。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/250723?site
推荐阅读
相关标签
  

闽ICP备14008679号