添加TextInputLayout致Android Studio布局预览失效,APK可正常运行
Material3项目中添加TextInputLayout导致布局预览失效问题
在Android Studio 2022.1.1 patch 1上开发Material3应用,添加MaterialButton可正常显示,但加入TextInputLayout后布局预览失效,且无任何报错提示。已尝试卸载重装AS、清理缓存、使用Material3模板、检查Gradle依赖等方法,问题仍未解决。
正常运行的布局代码
<com.google.android.material.button.MaterialButton android:id="@+id/mybtn" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:text="@string/open"/>
引发问题的布局代码
<com.google.android.material.textfield.TextInputLayout android:id="@+id/textField" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:hint="ok"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" /> </com.google.android.material.textfield.TextInputLayout>
项目级build.gradle配置
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.4.1' apply false id 'com.android.library' version '7.4.1' apply false id 'org.jetbrains.kotlin.android' version '1.8.0' apply false }
App模块build.gradle依赖
dependencies { implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.appcompat:appcompat:1.6.0' implementation 'com.google.android.material:material:1.8.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
settings.gradle配置
pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } } rootProject.name = "MyMood" include ':app'
应用主题配置
- 浅色主题:
<style name="Theme.MyMood" parent="Theme.Material3.Light.NoActionBar"> - 深色主题:
<style name="Theme.MyMood" parent="Theme.Material3.Dark.NoActionBar">
内容的提问来源于stack exchange,提问作者Bxx
相关产品推荐
相关产品推荐

