首页 > 解决方案 > EditText 居中内容的问题,如果有重力,软键盘会覆盖它

问题描述

我在 LinearLayout 中遇到 EditText 的这个问题,如果我给android:gravity="center"android:gravity="center_horizontal"给 EditText,软键盘在键入时会覆盖 EditText。当我第一次触摸 edittext 时,它工作正常,但是当我开始输入 EditText 时,它在键盘后面。

如果我不提供任何重力,这个问题永远不会出现。但我希望内容居中。

这是代码

        <EditText
        android:id="@+id/phoneET"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="@dimen/margin_bet_phn_edittxt"
        android:layout_marginRight="12dp"
        android:textAlignment="center"
        android:gravity="center"
        android:imeOptions="actionNext"
        android:inputType="number"
        android:maxLength="10" />

我尝试了不同的 windowSoftInputMode 选项(android:windowSoftInputMode="adjustPan")在清单中,但没有得到所需的结果。

还通过移除android:textAlignment="center"或仅保留它并移除重力来尝试它。但不成功,请帮忙。

整体布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_mobile"
android:gravity="center|top"
android:orientation="vertical">

<include layout="@layout/content_header" />


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_top_phn_view"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:gravity="center"
        android:lineSpacingExtra="6sp"
        android:text="Enter phone number"
        android:textColor="#ffffff"
        android:textSize="@dimen/enter_phn_text_size" />

    <EditText
        android:id="@+id/phoneET"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="@dimen/margin_bet_phn_edittxt"
        android:layout_marginRight="12dp"
        android:textAlignment="center"
        android:gravity="center_horizontal"
        android:imeOptions="actionNext"
        android:inputType="number"
        android:maxLength="10" />
</LinearLayout>

标签: androidandroid-layoutandroid-edittext

解决方案


推荐阅读