【问题标题】:How to Use View.isInEditMode() in custom views to skip code?如何在自定义视图中使用 View.isInEditMode() 来跳过代码?
【发布时间】:2014-02-02 18:57:53
【问题描述】:

我正在使用自定义视图来显示渐变颜色填充的 TextView,如下所示:

public class GradientTextView extends TextView {
public GradientTextView(Context context) {
    super(context, null, -1);
}

public GradientTextView(Context context,
                        AttributeSet attrs) {
    super(context, attrs, -1);
}

public GradientTextView(Context context,
                        AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    if (!isInEditMode()) {
        GradientTextView.getTextColor(context, null, defStyle);
    }
}

@Override
protected void onLayout(boolean changed,
                        int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    if (changed) {
        getPaint().setShader(new LinearGradient(
                0, 0, 0, getHeight(),
                Color.parseColor(Color.WHITE), Color.parseColor(Color.BLACK),
                Shader.TileMode.CLAMP));
    }
}

}

它在设备或模拟器上运行良好,但在 Android Studio 的 xml 预览器中出现渲染问题,建议在自定义视图中使用 View.isInEditMode() 来跳过代码或在 IDE 中显示示例数据并显示此错误堆栈:

java.lang.NullPointerException

谁能帮我解决这个问题?

【问题讨论】:

    标签: xml layout android-custom-view


    【解决方案1】:

    View.isInEditMode:

    指示此视图当前是否处于编辑模式。在开发人员工具中显示时,视图通常处于编辑模式...

    您的自定义视图布局文件可能如下所示:

    <com.example.android.customviews.GradientTextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    还有你对应的视图类:

    package com.example.android.customviews;
    
    public class GradientTextView extends TextView {
    
        public GradientTextView(Context context) {
            this(context, null);
        }
    
        public GradientTextView(Context context, AttributeSet attrs) {
            this(context, attrs, -1);
        }
    
        public GradientTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
            if (!isInEditMode()) {
                GradientTextView.getTextColor(context, null, defStyle);
            }
        }
    
        @Override
        protected void onLayout(boolean changed,
                                int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
            if (changed) {
                getPaint().setShader(
                        new LinearGradient(0, 0, 0, getHeight(),
                                Color.WHITE, Color.BLACK, Shader.TileMode.CLAMP));
            }
        }
    }
    

    注意:我省略了您的其他构造函数,但请看我如何在提供的构造函数中将内容包装在 isInEditMode() 检查中。

    【讨论】:

    • Tnx,实际上不确定您在代码中所做的更改?无论如何,问题仍然存在
    • 请注意,我省略了您的前两个构造函数,因为它们看起来是多余的。如果您必须拥有它们,请按照我上面编辑的方式将它们链接起来。
    • 您还需要提供一个有意义的默认 defStyle,因为在我的测试中 -1 似乎是您的 NullPointerException 的根
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    相关资源
    最近更新 更多