Android:当输入时保持TextInputEditText可见

4
我正在尝试创建一个包含几个 TextInputEditText 元素的活动。这些元素位于页面的中央位置,当键盘出现时会被覆盖。因此,我尝试在 AppManifest.xml 中使用 windowSoftInputMode="adjustPan",以便在键入时屏幕滚动以保持文本框可见。

然而,所有尝试都似乎失败了。以下是我使用的布局层次结构,以及我已经尝试过但未成功的解决方案示例。

layout xml

<ScrollView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillViewport="true"
    android:isScrollContainer="false">

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/inputA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/inputAText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/inputB"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/inputBText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </android.support.design.widget.TextInputLayout>

   </android.support.constraint.ConstraintLayout>

</ScrollView>

AndroidManifest.xml片段

<activity
    android:name=".activities.MyActivity"
    android:windowSoftInputMode="adjustPan"
    android:screenOrientation="sensorPortrait"
    android:theme="@style/AppTheme.NoActionBar" />

尝试的解决方案:
  1. AppManifest.xml中的相关activity中添加windowSoftInputMode="adjustPan"(我还尝试了其他StackOverflow建议中的adjustResizeadjustPan|adjustNothing
  2. ConstraintLayout放置在ScrollView
  3. 在该ScrollView中添加android:isScrollContainer="false"
  4. 使用仅包含ConstraintLayout和底部被约束的TextInputEditText的布局文件重新创建布局文件
  5. TextInputEditText更改为EditText并删除TextInputLayout
  6. ConstraintLayout更改为RelativeLayout
对于应该是相当简单的实现,我感到困惑。如有更多建议,将不胜感激。如果需要,我很乐意提供更多代码示例。

1
你尝试过不使用ScrollView吗?只使用ConstraintLayout。在最坏的情况下,可以尝试使用RelativeLayout代替ConstraintLayout...可能会起作用,也可能不会。 - Suresh
你有没有在清单文件中尝试去掉android:windowSoftInputMode="adjustPan"这一行?我认为默认情况下它会使视图上移。你是用真机测试还是模拟器? - Suresh
我已经尝试从清单中删除android:windowSoftInputMode="adjustPan",但不幸的是这也没有起作用。 - Tom Barden
啊,抱歉,我正在多个真实设备上进行测试。 - Tom Barden
让我们在聊天中继续这个讨论 - Suresh
显示剩余4条评论
1个回答

2
发现问题了!在我的主题中,我声明了<item name="android:windowTranslucentStatus">true</item>。由于某种原因,这会破坏windowSoftInputMode="adjustPan",因此我将其删除。
为了保持状态栏透明,我使用<item name="android:statusBarColor">@color/transparent</item>
希望有一天,有人会发现这个有用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接