Adding a TextInputLayout breaks the Android Studio layout preview, but the apk builds as expected and the app runs without issues

huangapple go评论61阅读模式
英文:

Adding a TextInputLayout breaks the Android Studio layout preview, but the apk builds as expected and the app runs without issues

问题

我正在尝试使用Android Studio创建一个Material3应用程序。成功添加MaterialButton后,但在添加TextInputLayout时,预览失败,甚至没有显示错误。

以下是没有问题的代码片段:

    <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"/>

以下是引发问题的XML代码片段:

    <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>

我的项目是全新创建的,使用了最新版本的Android Studio(2022.1.1 patch 1)。

这是问题的GIF

我尝试过:

  • 完全卸载Android Studio、Android SDK和所有gradle、缓存和配置文件,然后重新安装所有内容
  • 使缓存无效并重新启动
  • 使用Android Studio Material 3 Activity项目模板
  • 检查gradle文件中的问题(缺少依赖项、依赖项版本等)

我的 build.gradle(项目):

// 顶层构建文件,您可以在其中添加所有子项目/模块的通用配置选项。
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
}

我的 build.gradle(模块 app)依赖项:

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'

我正在使用Material3样式:

  • 亮色主题: <style name="Theme.MyMood" parent="Theme.Material3.Light.NoActionBar">
  • 暗色主题: <style name="Theme.MyMood" parent="Theme.Material3.Dark.NoActionBar">
英文:

I am trying to make a Material3 application with Android Studio.
I was able to successfully add a MaterialButton but when adding a TextInputLayout, the preview fails without even showing an error.

This is the snippet that works without issues:

    <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"/>

This is the xml snippet that causes the issue:

    <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>

My project is brand new and created using the latest Android Studio version (2022.1.1 patch 1)

This is a GIF of the issue

I tried:

  • Completely uninstalling Android Studio, the Android SDK and all gradle, caches and configuration files and then reinstalled everything
  • Invalidating caches and restarting
  • Using the Android Studio Material 3 Activity project template
  • Checking for issues in the gradle files (missing dependencies, dependencies versions...)

My build.gradle (project)

// 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
}

My build.gradle (Module app) dependencies:

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'
}

My settings.gradle:

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "MyMood"
include ':app'

I am using the Material3 styles:

  • Light: <style name="Theme.MyMood" parent="Theme.Material3.Light.NoActionBar">
  • Dark: <style name="Theme.MyMood" parent="Theme.Material3.Dark.NoActionBar">

答案1

得分: 3

I had the same issue and was losing my mind over it as well! However I made it work now with Theme.Material3.Light.NoActionBar

  • Update Android Studio to Electric Eel | 2022.1.1 Patch 2
  • Update Material Design to implementation 'com.google.android.material:material:1.9.0-alpha02'
  • Update Plugins to id 'com.android.application' version '7.4.2' apply false and id 'com.android.library' version '7.4.2' apply false

Afterwards clean the project and invalidate ALL caches under: File -> Invalidate Caches (MacOS)
After Android Studio restarted Rebuild Project and then it should work.

英文:

I had the same issue and waslosing my mind over it as well! However I made it work now with Theme.Material3.Light.NoActionBar

  • Update Android Studio to Electric Eel | 2022.1.1 Patch 2
  • Update Material Design to implementation 'com.google.android.material:material:1.9.0-alpha02'
  • Update Plugins to id 'com.android.application' version '7.4.2' apply false and id 'com.android.library' version '7.4.2' apply false

Afterwards clean the project and invalidate ALL caches under: File -> Invalidate Caches (MacOS)
After Android Studio restarted Rebuild Project and then it should work.

答案2

得分: 2

我有相同的问题,Electric Eel + Material3主题 + TextInputLayout破坏了布局预览。

在升级到Electric Eel之前,这个工作正常,目前我认为降级Android Studio是唯一的选项。

英文:

I have the same problem, Electric Eel + Material3 Theme + TextInputLayout brakes layout preview.

This used to work fine before upgrading to Electric Eel, and at the moment, I think downgrading Android Studio is the only option.

答案3

得分: 0

好的,以下是翻译好的部分:

"well i reproduced your code in the newest version of Android Studios ( Android Studio Electric Eel | 2022.1.1 Patch 1 ) and the xml showed just fine."

我在最新版本的Android Studios(Android Studio Electric Eel | 2022.1.1 Patch 1)中重现了您的代码,XML 显示正常。

"I did have issues reproducing the xml myself back in the previous version ( Dolphin ) but i guess its fixed now. Try updating android studio to the newest version."

在以前的版本(Dolphin)中,我自己重现 XML 时出现了问题,但我想现在已经修复了。尝试将 Android Studio 更新到最新版本。

"If that doesn't work try changing an option in your " design screen " called "Theme for Preview "."

如果那不起作用,请尝试在“设计界面”中更改一个选项,名为“预览主题”。

英文:

well i reproduced your code in the newest version of Android Studios ( Android Studio Electric Eel | 2022.1.1 Patch 1 ) and the xml showed just fine.

I did have issues reproducing the xml myself back in the previous version ( Dolphin ) but i guess its fixed now.
Try updating android studio to the newest version.

If that doesn't work try changing an option in your " design screen " called "Theme for Preview "

huangapple
  • 本文由 发表于 2023年2月9日 02:18:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定