当我添加 <com.google.android.material.textfield.TextInputEditText>

问题描述 投票:0回答:3

在我开发的任何 Android 应用程序中,我个人更喜欢

com.google.android.material.textfield.TextInputEditText
视图,而不是普通的
EditText
,就像在我看来,它看起来 much 在任何可能的方面都更好。

问题是,使用

com.google.android.material.textfield.TextInputEditText
时,Android Studio 中的预览会变成空白。除非通过编译并在设备上运行实际应用程序才能预览,否则很难设计 UI。

这是我的

fragment_world.xml
文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".WorldFragment">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textField"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Hello, world!" />

    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

如您所见,这里没有发生任何异常情况。在我将

com.google.android.material.textfield.TextInputEditText
视图添加到布局中之前,预览显示得很好。现在,预览变为空白。如果我切换到另一个选项卡,然后再切换回来,我可以看到布局中每个视图的轮廓,但它很快就会恢复到之前的空白状态。

我试图创建一个新的、干净的项目,除了这里的布局外什么都没有:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textField"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Hello, world!" />

</com.google.android.material.textfield.TextInputLayout>

那没有用,同样的事情发生了。

android android-studio material-ui material-design
3个回答
0
投票

确保您的应用主题应该是

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

目前,应该是

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

如果设置了所有依赖项,我认为这应该有所帮助。

快乐编码:)


0
投票

其实是Android Studio本身的问题。在 Canary 6 版本中尝试相同的设置后,它运行得非常好。


0
投票

有同样的问题,问题从

开始
implementation 'com.google.android.material:material:1.8.0'

现在,我将版本更改为

implementation 'com.google.android.material:material:1.7.0'

它有效,正如我在评论中看到的,问题出在 Android Studio 中,所以只需等待修复

© www.soinside.com 2019 - 2024. All rights reserved.