"Material Design Components" 未在布局预览器中呈现

The "Material Design Components" aren't being rendered in the layout previewer

在我所有的 android 应用程序中,我为 AndroidX 实现了 Material 设计组件 (MDC) 依赖项,并且一切正常很好 直到我将 Android Studio 更新到 3.4 稳定版。 该应用程序在模拟器和真实手机上都能完美编译和运行,但 XML Layout Previewer of Android Studio 不会呈现任何MDC 和预览页面冻结。

我已经尝试了很多东西,none 解决了我的问题:

此 XML 布局预览器问题 仅在至少包含 1 个 MDC 的布局中出现 。如果没有这些组件,一切正常。

我的 styles.xml 文件如下所示:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorGroupPrimary">@color/colorPrimary</item>
        <item name="colorGroupPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorGroupAccent">@color/colorAccent</item>
        <item name="android:fontFamily">@font/montserrat</item>
        <item name="android:windowBackground">@drawable/splash_bg</item
</style>

即使我将 AppTheme 扩展为 Theme.MaterialComponents.Light.NoActionBar,它也不起作用。

我的 build.gradle 文件如下所示:

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 28
        maxSdkVersion 28
        ...
    }
    ...
}

dependencies {
    implementation 'com.google.android.material:material:1.1.0-alpha05'
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
    ...
}

不呈现的布局示例

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:background="@color/colorBackground"
    tools:context=".Activities.AddNoteActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="?attr/colorGroupPrimaryDark">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/addNote_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:paddingStart="6dp"
            android:paddingEnd="6dp"
            app:contentInsetStartWithNavigation="0dp"
            app:navigationIcon="@drawable/ic_close"
            app:title="@string/addNotes_toolbar"
            app:titleTextAppearance="@style/TextAppearance.CACH.Title"
            app:titleTextColor="@color/colorWhite" />

    </com.google.android.material.appbar.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:orientation="vertical"
        android:padding="10dp">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/addNote_titleInput"
            style="@style/Widget.CACH.TextInputLayout.Primary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:hint="@string/addNotes_title"
            android:paddingBottom="8dp"
            app:errorEnabled="true">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/addNote_titleText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/colorBlack"
                android:textSize="22sp" />

        </com.google.android.material.textfield.TextInputLayout>
    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

在示例中,Toolbar 和 AppBar 不会造成任何问题,但此时我添加了 TextInputLayoutTextInputEditText 预览器冻结。如果在另一个布局中我添加 MaterialButtonFloatingActionButtonMaterialCardView 同样的事情会发生。

(在示例中,所有 style 属性都扩展了特定于组件的 MaterialComponent 主题。示例:TextInputLayout style has as parent Widget.MaterialComponents.TextInputLayout.OutlinedBox )

感谢您的回答!!

我有同样的问题,只需将 material 库降级到 alpha-04。现在 alpha 7 是最新版本,但从 alpha 05 到 07 存在此问题。

自从我将AndroidStudio 3.3 更新到3.4 版本后,AppbarLayout 出现了渲染问题,导致Javalang 异常,如图所示。我仍在寻找解决方案

我遇到了同样的问题,我把主题从

换了
<style name="AppTheme" parent="Theme.MaterialComponents.Light">

收件人:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">

也许这是最终的解决方案,但这对我来说是一个很好的解决方法。

我找不到这个问题的完整解决方案,但我尝试并每次都完美运行的是下载 AndroidStudio 3.5 Beta。使用该版本的 IDE 解决了我在使用 MaterialComponents 时遇到的所有问题。