无法在 Android Studio 布局编辑器中显示自定义视图

Can't display custom View in Android Studio Layout Editor

我正在努力让我的自定义视图显示在 Android Studio's Layout Editor 中。

我的视图是带有自定义参数的 RecyclerView 的简单子类。我使用 View.isInEditMode() 仅在实际 运行 应用程序时使用给定的 attrs

public class CustomRecyclerView extends RecyclerView {

    public CustomRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (isInEditMode()) {
            return;
        }

        final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomRecyclerView, 0, 0);
        /* ... */
}

但是,预览包含此视图的布局会导致预览窗格为空(甚至 phone 的框架也不行):

如果我用 RecyclerView 替换我的视图,预览会按预期工作:

我不知道我应该在这里做什么:如果 RecyclerView 正确显示并且我在子类的构造函数中所做的就是调用 super() 和 return 如果 isInEditMode 是是的,那有什么可以解释我的视图未显示?

如何才能让我的自定义视图显示在 Android Studio 的布局编辑器中?

截至今天,使用 Android Studio 2.1.2 打开同一个项目确实会在预览窗格中成功显示 UI。我只能猜测这是 IDE 中的一个错误,自从我问了这个问题后就已经修复了。

要解决此问题,请使用较低的

API levels like 26

然后重建项目。