Android -- isInEditMode

简介:

解释

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.

如果在自定义控件的构造函数或者其他绘制相关地方使用系统依赖的代码,会导致可视化编辑器无法报错并提示:Use View.isInEditMode() in your custom views to skip code when shown in Eclipse

code

复制代码
public class LockRelativeLayout extends RelativeLayout {
    private Handler mainHandler = null; //与主Activity通信的Handler对象

    public LockRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        mContext = context;
        if (isInEditMode()) { return; }
        mainHandler = ((MainActivity)mContext).getMHandler();
    }
}
复制代码

如果不加上if (isInEditMode()) { return; },标红处代码会导致可视化编辑报错

image

我是天王盖地虎的分割线

 

 




本文转自我爱物联网博客园博客,原文链接:http://www.cnblogs.com/yydcdut/p/4456722.html,如需转载请自行联系原作者

相关文章
|
Android开发
Android--图片集
一. 实现效果   安卓系统中的相册集效果图,左右滑动可以查看上一张或者下一张图片       二. 布局代码     三. 自定义Adapter    package com.
782 0
|
数据库 Android开发 数据库管理
|
物联网 Android开发
|
Android开发 容器