布局预览器中未呈现“材料设计组件”

时间:2019-04-28 02:42:09

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

在我所有的Android应用程序中,我都为AndroidX实现了 Material Design Components (MDC)依赖关系,并且一切正常,直到我将Android Studio更新到3.4 < / strong>稳定版本。 该应用程序可以在模拟器和真实手机中完美编译并运行,但是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>

在该示例中,工具栏和AppBar不会造成任何麻烦,但此刻我添加了TextInputLayoutTextInputEditText预览器冻结了。如果在其他布局中我添加了MaterialButtonFloatingActionButtonMaterialCardView,则会发生相同的情况。

(在示例中,所有style属性都扩展了特定于组件的MaterialComponent主题。示例:TextInputLayout样式具有父级Widget.MaterialComponents.TextInputLayout.OutlinedBox

感谢您的回答!

4 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,只是将材料库降级为alpha-04。现在,alpha 7是最新版本,但是从alpha 05 til 07开始存在此问题。

答案 1 :(得分:0)

自从我将Android Studio 3.3更新为3.4版本以来,AppbarLayout出现了渲染问题,并导致Javalang异常(如上图)。我仍在寻找解决方案 enter image description here

答案 2 :(得分:0)

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

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

收件人:

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

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

答案 3 :(得分:0)

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