【问题标题】:Cannot place TextInputEditText on Top of TextInput Layout. AndroidStudio无法将 TextInputEditText 放置在文本输入布局的顶部。安卓工作室
【发布时间】:2020-08-03 05:27:46
【问题描述】:

所以我遇到了一个问题,我试图在 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(app:module)

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 轴偏移。 Click Here For New Screenshot

【问题讨论】:

  • 至少使用最新的稳定版本:implementation 'com.google.android.material:material:1.1.0'
  • TextInputEditText 中使用android:layout_height="match_parent",在TextInputLayout 中使用android:layout_height="wrap_content"
  • @GabrieleMariotti 它确实有效,但仍然与顶部有一点偏移。这是截图link
  • 删除TextInputLayout中的android:padding="5dp"
  • 试过@GabrieleMariotti Y轴偏移仍然存在。内容完全可见,但偏移量仍然存在

标签: android android-studio android-layout material-components-android android-textinputlayout


【解决方案1】:

应用这些更改:

  • 使用Material Components最新稳定版implementation 'com.google.android.material:material:1.1.0'
  • TextInputLayout 中使用android:layout_height="wrap_content"
  • 删除android:padding="5dp" 中的TextInputLayout
  • TextInputEditText 中使用android:layout_height="match_parent"
  • 删除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>

【讨论】:

  • 非常感谢。从 1.0.0 更新到 1.1.0 工作。祝你有美好的一天,快乐的编码:)
【解决方案2】:

尝试将 TextInputEditText 的 android:layout_width 和 android:layout_height 设置为“match_parent”,如果您想完美适配,也可以尝试从 TextInputLayout 中移除 android:padding="5dp"。还要从第二个 TextInputLayout 中删除 android:hint="Password"。这就是为什么您会收到双重提示的密码。

【讨论】:

  • @roshan_topno 我确实删除了密码,它确实修复了双重提示,并且不知何故我修复了 X 轴偏移。唯一的问题是 Y 轴偏移。
猜你喜欢
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
  • 2018-11-12
  • 2015-12-16
相关资源
最近更新 更多