【发布时间】: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
- 将
targetSdkVersion和compileSdkVersions更改为 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 不会造成任何问题,但此时我添加了 TextInputLayout 和 TextInputEditText 预览器冻结。如果在另一个布局中添加 MaterialButton 或 FloatingActionButton 或 MaterialCardView 会发生同样的事情。
(在示例中,所有 style 属性都扩展了特定于组件的 MaterialComponent 主题。示例:TextInputLayout 样式具有父级 Widget.MaterialComponents.TextInputLayout.OutlinedBox)
感谢您的回答!!
【问题讨论】:
-
你能举一个不在预览窗口中呈现的布局示例吗?
-
是的,当然!
-
尝试使缓存无效并重新启动
-
@JawadAhmed 我试过了,但没有用。它暂时解决了问题,但几分钟后又冻结了
-
我感觉您的安装可能有问题。你试过这个问题的所有答案吗? stackoverflow.com/questions/26594881/…
标签: android android-studio android-layout mdc-components