View.isInEditMode()方法的作用

    /**
     * Android源码
     * Indicates whether this View is currently in edit mode. A View is usually
     * in edit mode when displayed within a developer tool. For instance, if
     * this View is being drawn by a visual user interface builder, this method
     * should return true.
     *
     * Subclasses should check the return value of this method to provide
     * different behaviors if their normal behavior might interfere with the
     * host environment. For instance: the class spawns a thread in its
     * constructor, the drawing code relies on device-specific features, etc.
     *
     * This method is usually checked in the drawing code of custom widgets.
     *
     * @return True if this View is in edit mode, false otherwise.
     */
    public boolean isInEditMode() {
        return false;
    }

学习大佬写的代码的时候发现了这个方法。这个方法是andorid.view.View中的代码,它的返回值也是很有意思。看注释是在编辑模式下返回true,否则返回false。

那么问题来了。。。什么叫“edit mode”编辑模式???上面一大段注释看的我一脸懵逼,反正我是没整明白啥叫编辑模式。。。

直到后来经过大佬指导,才知道所谓的“edit mode”就是指AndroidStudio环境下的xml布局文件可视化编辑。。。尤其是在自定义View的时候,当我们把自定义的View通过xml布局文件展示出来的时候,AndroidStudio会调用这个View的构造方法来生成图像,使得我们在写xml文件的时候可以实时看到View的变化。所以只要我们通过xml编辑自定义View的时候,就被称为“edit mode”编辑模式。

另外isInEditMode方法的返回值就会告诉我们是否处于这样一个模式下,很多时候处于“edit mode”的情况下,有些代码是不需要执行的。因为xml编辑器的可视化只是方便我们编写布局而已,并不是一个Andorid 虚拟机,我们可以通过这个方法来避免执行多余的代码。用以提高AndoridStudio的效率

if(isInEditMode()) return;

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值