Android TextInputLayout 的设计器渲染问题:java.lang.IllegalArgumentException:权重超出 [0, 1000] 范围

Android designer render problem with TextInputLayout: java.lang.IllegalArgumentException: weight is out of range of [0, 1000]

今天打开 Android Studio 时,我感到很惊讶 - 我看不到布局的预览:

我认为这只是一个简单的修复:

尽管其中 none 已经解决了问题。

我也遇到过其他人遇到的渲染问题并尝试了各种解决方案,但我仍然遇到问题。

奇怪的是,每当我从布局中删除 TextInputLayout 小部件时,渲染问题就会消失:

(以下是导致问题的布局之一。)

<?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"
    android:id="@+id/fragmentNewCanvas_rootLayout"
    tools:context=".fragments.newcanvas.NewCanvasFragment">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/fragmentNewCanvas_projectTitleTextInputLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:hint="@string/fragmentNewCanvas_project_name_str"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/fragmentNewCanvas_projectTitleTextInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/fragmentNewCanvas_spanCountTextInputLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:hint="@string/fragmentNewCanvas_span_count_str"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fragmentNewCanvas_projectTitleTextInputLayout">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/fragmentNewCanvas_spanCountTextInputEditText"
            android:inputType="number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/fragmentNewCanvas_doneButton"
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="24dp"
        android:text="@string/str_button_done"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是演示问题的 gif:

不包含 TextInputLayout 的布局渲染良好,但包含的布局会导致问题。

堆栈跟踪:

java.lang.IllegalArgumentException: weight is out of range of [0, 1000] (too low)
    at com.android.internal.util.Preconditions.checkArgumentInRange(Preconditions.java:527)
    at android.graphics.Typeface.create_Original(Typeface.java:979)
    at android.graphics.Typeface_Delegate.create(Typeface_Delegate.java:123)
    at android.graphics.Typeface.create(Typeface.java:979)
    at com.google.android.material.internal.CollapsingTextHelper.maybeCloneWithAdjustment(CollapsingTextHelper.java:532)
    at com.google.android.material.internal.CollapsingTextHelper.setCollapsedTypefaceInternal(CollapsingTextHelper.java:475)
    at com.google.android.material.internal.CollapsingTextHelper.setTypefaces(CollapsingTextHelper.java:459)
    at com.google.android.material.textfield.TextInputLayout.setEditText(TextInputLayout.java:1467)
    at com.google.android.material.textfield.TextInputLayout.addView(TextInputLayout.java:888)
    at android.view.ViewGroup.addView(ViewGroup.java:5048)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1131)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1130)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:505)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:361)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:436)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:727)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:883)
    at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.run(RenderExecutor.kt:187)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)

如果有人有解决方案,将不胜感激。

找到临时解决方法:

将主题从 Material3 更改为 MaterialComponents

将等待错误修复。

改变

实施'com.google.android.material:material:x.x.x-alpha'

实施'com.google.android.material:material:1.5.0'

运行 今天使用 TextInputLayout 遇到了同样的问题,升级到 com.google.android.material:material:1.6.0-alpha03 为我解决了这个问题。