首页 > 解决方案 > 无法将 TextInputEditText 放置在 TextInput 布局的顶部。安卓工作室

问题描述

所以我遇到了一个问题,我试图在 com.google.material 库的 TextInputLayout 下将我的 EditText 转换为 TextInputEditText。

我无法将我的 TextInputEdit 文本放在 TextInputLayout 的顶部 这是一个屏幕截图。 单击此处获取屏幕截图

正如你所看到的,它从顶部和左侧都有点偏离。

登录.xml

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

    android:layout_width="210dp"
    android:layout_height="53dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:background="@drawable/roundedittext"
    android:ems="10"
    android:hint="  Email"
    android:importantForAutofill="no"
    android:inputType="textEmailAddress"
    android:padding="5dp"
    app:errorEnabled="true"
    tools:layout_editor_absoluteX="101dp"
    tools:layout_editor_absoluteY="366dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/signupemailfeild"
        android:layout_width="210dp"
        android:layout_height="47dp" />

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



<com.google.android.material.textfield.TextInputLayout
    android:layout_width="210dp"
    android:layout_height="47dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/roundedittext"
    android:ems="10"
    android:hint="  Password"
    android:importantForAutofill="no"
    android:inputType="textPassword"
    android:padding="5dp"
    app:errorEnabled="true"
    app:passwordToggleEnabled="true"
    tools:layout_editor_absoluteX="101dp"
    tools:layout_editor_absoluteY="444dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/signuppasswordfeild"
        android:layout_width="match_parent"
        android:layout_height="47dp"
        android:hint="Password"
        android:inputType="textPassword"
        tools:layout_editor_absoluteX="match_parent"
        tools:layout_editor_absoluteY="match_parent" />

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

build.gradle(应用程序:模块)

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    implementation 'com.google.firebase:firebase-auth:19.3.1'
    implementation 'com.google.firebase:firebase-database:19.3.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.navigation:navigation-fragment:2.3.0'
    implementation 'androidx.navigation:navigation-ui:2.3.0'
    implementation 'com.github.castorflex.smoothprogressbar:library:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.mediarouter:mediarouter:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.firebase:firebase-analytics:17.2.2'
}

更新:- 修复了 X 轴偏移 现在问题仅在于 Y 轴偏移。 单击此处获取新屏幕截图

标签: androidandroid-studioandroid-layoutmaterial-components-androidandroid-textinputlayout

解决方案


应用这些更改:

  • 使用最新稳定版的 Material Componentsimplementation 'com.google.android.material:material:1.1.0'
  • android:layout_height="wrap_content"TextInputLayout
  • android:padding="5dp" 中删除TextInputLayout
  • android:layout_height="match_parent"TextInputEditText
  • android:hint=" Password"中删除TextInputEditText
  • 更改app:passwordToggleEnabled="true"app:endIconMode="password_toggle"

只需使用:

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="  Password"
        android:importantForAutofill="no"
        app:errorEnabled="true"
        app:endIconMode="password_toggle"
        ...>

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/signuppasswordfeild"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="textPassword"
            />

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

在此处输入图像描述


推荐阅读