首页 > 解决方案 > KOTLIN 在键盘显示时调整片段滚动视图

问题描述

我在滚动视图中有一个表单,需要垂直滚动以在显示键盘时显示表单的其余部分。

我已将此添加到 manifest.xml

android:windowSoftInputMode="stateVisible|adjustResize">

片段.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@color/colorBlack"
    tools:context=".ui.main.ConnectFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:isScrollContainer="false"
        android:scrollbars="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="vertical">

          //My form
        </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

片段.kt

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        navController = Navigation.findNavController(view)

我需要在 onViewCreated 中添加一些东西吗?

标签: androidkotlin

解决方案


我唯一确定的是你的表单应该在scrollView之外。

它里面的所有东西都会在滚动时做出反应,所以当键盘打开时不会向上移动


推荐阅读