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

标签 android android-studio android-layout mdc-components

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

我已经尝试了很多东西,但没有一个能解决我的问题:

  • 更新到 Android Studio 3.5 Canary 或 Dev
  • targetSdkVersioncompileSdkVersions 更改为 28
  • 重新安装 Android Studio
  • 使缓存失效并重启
  • 使用不同的Theme.MaterialComponents 变体

这个 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 样式具有作为父级的 Widget.MaterialComponents .TextInputLayout.OutlinedBox)

感谢您的回答!!

最佳答案

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

关于android - 布局预览器中未呈现 "Material Design Components",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55886572/

相关文章:

java - 在android中更改Imageview的大小

Android Studio 和 OpenCV 'Compilation Error' , 'Cannot resolve symbol styleable'

android - 如何以编程方式在 Android 中查找平板电脑或手机?

android - 有没有办法在Android上创建一个三角形按钮?

安卓操作系统和应用程序

java - Twitter 分享带有文本的图像,toast "Image Could Not Be Loaded"

android - 如何在多点触控上缩放整个 Activity ?

android-studio - Flutter 升级到 Flutter 1.11.0 (Mac OS) 后的 Flutter 问题

java - 如何通过ID从SQLITE数据库中删除数据

android - drawableRight 靠近文本时宽度匹配_parent