Chr*_*Gil 5 android android-layout android-studio mdc-components
在我所有的Android应用程序中,我都实现了Material Design Components(MDC)依赖关系,AndroidX并且一切正常,直到将Android Studio更新到3.4稳定版本为止。该应用程序可以在模拟器和真实手机中编译并完美运行,但是Android Studio 的XML Layout Previewer 不会呈现任何MDC,并且Preview页面会冻结。
我已经尝试了很多事情,但没有一个可以解决我的问题:
targetSdkVersion和更改compileSdkVersions为28Theme.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>
Run Code Online (Sandbox Code Playgroud)
即使我将其扩展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'
...
}
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)
在示例中,工具栏和AppBar不会造成任何麻烦,但此刻我添加了TextInputLayout,TextInputEditText并且预览器冻结了。如果在另一个布局中,我添加MaterialButton或FloatingActionButton或MaterialCardView发生相同的事情。
(在示例中,所有style属性都扩展了特定于组件的MaterialComponent主题。示例:TextInputLayoutstyle作为parent Widget.MaterialComponents.TextInputLayout.OutlinedBox)
我找不到这个问题的完整解决方案,但我每次尝试并完美运行的是下载AndroidStudio 3.5 Beta。使用该版本的 IDE 解决了我在使用 MaterialComponents 时遇到的所有问题。
| 归档时间: |
|
| 查看次数: |
518 次 |
| 最近记录: |