首页 > 解决方案 > ConstraintLayout 2.0 渲染问题

问题描述

更新到 ConstraintLayout 2.0 后,我意识到即使是最简单的布局也无法在 Android Studio 中正确预览,无论是在 OS X 还是 Ubuntu 上。

首先,我的设置是这样的:我正在使用"androidx.constraintlayout:constraintlayout:2.0.0",我的两台电脑上的 Android Studio 版本都是 4.0.1(最新的稳定版本),Android Gradle 插件的版本是 4.0.1,Gradle 的版本是6.1.1.

这是一个例子:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我希望它能够绘制与屏幕顶部对齐的 TextView。

我最终看到的是:

在此处输入图像描述

我尝试关闭实验性渲染引擎,因为 AS 警告我它可能存在一些问题,但无济于事。

有什么明显的我可能会丢失吗?由于 2.0.0 发布了 2-3 天(并且候选发布版可供广大受众使用的时间更长),我认为此时所有明显的故障都应该得到解决。

更新。AS 指出存在以下渲染问题:

java.lang.NoClassDefFoundError: Could not initialize class androidx.constraintlayout.widget.ConstraintLayout$LayoutParams$Table
    at androidx.constraintlayout.widget.ConstraintLayout$LayoutParams.<init>(ConstraintLayout.java:2603)
    at androidx.constraintlayout.widget.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:1823)
    at androidx.constraintlayout.widget.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:481)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1125)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:501)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:348)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:404)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:678)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$8(RenderTask.java:809)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

更新。2我已经提交了一个错误报告,让我们看看这是否确实是 Android Studio / 约束布局的错误(我认为不是这种情况)。

标签: androidandroid-studioandroid-constraintlayoutandroid-studio-4.0

解决方案


好的,就我而言,这绝对是项目缓存的问题。一旦我清理了它(Build -> Clean Project),布局预览就开始工作得很好。


推荐阅读