【问题标题】:View doesn't scroll all the way up when clicking EditText单击 EditText 时视图不会一直向上滚动
【发布时间】:2021-11-01 16:35:14
【问题描述】:

当我点击 3 个编辑文本中的 1 个时,我的键盘出现并且视图向上滚动一点。但是我的布局应该一直向上移动。我的xml文件:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView"
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:layout_constraintVertical_bias="0.01999998">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="126dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/padding_default"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                app:layout_constraintTop_toTopOf="parent"
                tools:layout_editor_absoluteX="16dp" />

            <EditText
                android:id="@+id/instructionsEditText"
                android:layout_width="match_parent"
                android:layout_height="600dp"
                android:layout_margin="@dimen/padding_default"
                android:gravity="top|start"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                app:layout_constraintTop_toBottomOf="@+id/amountPersonsEditText" />

            <EditText
                android:id="@+id/notesEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/padding_default"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/instructionsEditText"
                tools:layout_editor_absoluteX="16dp" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

    <android.widget.Button
        android:id="@+id/saveInstructionsButton"
        style="@style/buttonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/padding_default"
        android:gravity="center"
        android:imeOptions="actionDone"
        android:text="@string/instructions_button_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的机器人清单:

    <activity
        android:name=".ui.instructions.InstructionsActivity"
        android:windowSoftInputMode="adjustResize"
        android:screenOrientation="portrait" />

我也试过这个:

fun ScrollView.moveToTop() {
    this.isFocusableInTouchMode = true
    this.fullScroll(View.FOCUS_DOWN)
    this.smoothScrollTo(0, bottom)
}

但是按钮在滚动视图之外并且按钮底部应该在键盘的顶部。

开始:

实际:

预期:

【问题讨论】:

  • 我无法正确了解您,请您详细说明您的担忧

标签: android android-constraintlayout


【解决方案1】:

在滚动视图中添加以下代码:

    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/saveInstructionsButton"

底部按钮中的代码:

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"

示例代码:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView"
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/saveInstructionsButton">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/amountPersonsEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/padding_default"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                app:layout_constraintTop_toTopOf="parent"
                tools:layout_editor_absoluteX="16dp"
                android:focusableInTouchMode="true" />

            <EditText
                android:id="@+id/instructionsEditText"
                android:layout_width="match_parent"
                android:layout_height="600dp"
                android:layout_margin="@dimen/padding_default"
                android:gravity="top|start"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                android:focusableInTouchMode="true"
                app:layout_constraintTop_toBottomOf="@+id/amountPersonsEditText" />

            <EditText
                android:id="@+id/notesEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/padding_default"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/instructionsEditText"
                tools:layout_editor_absoluteX="16dp"
                android:focusableInTouchMode="true"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
    <android.widget.Button
        android:id="@+id/saveInstructionsButton"
        style="@style/buttonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:imeOptions="actionDone"
        android:text="@string/instructions_button_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

清单文件代码:

<activity
    android:name=".ui.instructions.InstructionsActivity"
    android:windowSoftInputMode="adjustResize"
    android:screenOrientation="portrait" />

上述代码的输出是:

【讨论】:

  • 奇怪的是它不起作用。我会尝试一个独立的项目,我希望它会起作用。
【解决方案2】:

你的 xml 没问题,尝试修改清单中的活动声明,如下所示,

<activity
    android:name=".ui.instructions.InstructionsActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="portrait" />

【讨论】:

    【解决方案3】:

    替换这一行:

    android:windowSoftInputMode="adjustResize"
    

    与:

    android:windowSoftInputMode="adjustPan"
    

    当您键入时,它会将您的编辑文本移动到键盘上方。

    【讨论】:

      【解决方案4】:

      您必须将 stateAlwaysVisibe 用于 windowSofInputMode,如下所示:

      <activity
          android:name=".ui.instructions.InstructionsActivity"
          android:windowSoftInputMode="stateAlwaysVisible"
          android:screenOrientation="portrait" />
      

      【讨论】:

        【解决方案5】:

        根据文档:https://developer.android.com/training/keyboard-input/visibility

        考虑在AndroidManifest.xml 中为该活动更改android:windowSoftInputMode

        我猜:android:windowSoftInputMode="adjustPan" 应该可以解决问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-01-20
          • 1970-01-01
          • 1970-01-01
          • 2012-10-27
          • 2019-08-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多