【问题标题】:The "Material Design Components" aren't being rendered in the layout previewer“材料设计组件”未在布局预览器中呈现
【发布时间】:2019-04-28 02:42:09
【问题描述】:

在我所有的 Android 应用程序中,我为 AndroidX 实现了 Material Design Components (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

感谢您的回答!!

【问题讨论】:

  • 你能举一个不在预览窗口中呈现的布局示例吗?
  • 是的,当然!
  • 尝试使缓存无效并重新启动
  • @JawadAhmed 我试过了,但没有用。它暂时解决了问题,但几分钟后又冻结了
  • 我感觉您的安装可能有问题。你试过这个问题的所有答案吗? stackoverflow.com/questions/26594881/…

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我换了主题

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

    收件人:

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

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

    【讨论】:

    • 感谢您的回答!我实际上尝试过,它适用于某些组件,但即使您更改了某些属性,其他组件也会变成灰色。所以就我而言,这是一个部分解决方案。
    • 我还发现使用 Theme.MaterialComponents.Light.Bridge 主题解决了这个问题,但有些组件需要重新设置样式。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2019-12-22
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-25
        • 2019-11-01
        • 1970-01-01
        相关资源
        最近更新 更多