制作软键盘滚动视图而不是调整其大小

时间:2018-03-14 01:12:04

标签: android android-layout android-actionbar android-softkeyboard android-scrollview

我正在尝试为我的Android应用模拟类似iOS的键盘。简单地说,我的活动内容应滚动键盘后面而不调整间距(这是android:windowSoftInputMode="stateHidden|adjustResize"的作用)。

我可以通过使我的父布局成为ScrollView(如Make soft keyboard behave like IOS in Android中所述)来实现近距离滚动行为,但这样做滚动我的自定义操作栏(包含{ {1}})。要么我没有正确实现自定义操作栏(所以它总是总是在屏幕顶部,忽略滚动),或者有一些技巧让软键盘只滚动调整大小(和滚动)一个特定的元素,为自己腾出空间。

希望这张简单的图片说明了我想要实现的目标。在右侧的屏幕中,操作栏保持不变,主内容间距保持不变,视图可滚动。

My desired keyboard scroll behavior

这是我在活动中添加的自定义操作栏布局。

android.support.v7.widget.Toolbar

当前视图实验布局如下。我尝试过很多东西,但都没有。下面的一个是我最新的测试,也没有实现我想要达到的目标。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/actionbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@color/primaryRed"
    android:elevation="4dp"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="-7dp"
            android:contentDescription="@string/back"
            android:scaleType="centerInside"
            android:src="@drawable/ic_up_unpadded" />

        <TextView
            android:id="@+id/hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginStart="20dp"
            android:text="@string/back"
            android:textColor="@android:color/white"
            android:textSize="20sp" />

    </RelativeLayout>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/theGlassFiles"
        android:textAllCaps="true"
        android:textColor="@android:color/white"
        android:textSize="22sp" />

</android.support.v7.widget.Toolbar>

修改

围绕父/子布局进行交换,我实现了真正接近我所瞄准的目标。操作栏始终位于顶部,键盘仅调整滚动视图的大小。 然而,当我滚动时,我无法在第一个<?xml version="1.0" encoding="utf-8"?> <ScrollView 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:fillViewport="true" android:fitsSystemWindows="true" tools:context="JoinActivity"> <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center"> <include layout="@layout/actionbar" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/message" android:layout_width="362dp" android:layout_height="wrap_content" android:layout_marginTop="76dp" android:gravity="center" android:lineSpacingExtra="8sp" android:text="@string/joinMessage" android:textColor="@color/primaryBlue" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.489" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <LinearLayout android:id="@+id/form" android:layout_width="362dp" android:layout_height="wrap_content" android:focusableInTouchMode="true" android:orientation="vertical" android:paddingBottom="20dp" android:paddingTop="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.489" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/message" app:layout_constraintVertical_bias="0.176"> <EditText android:id="@+id/firstName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="@string/firstNameHint" android:imeOptions="actionDone" android:inputType="textPersonName" /> <EditText android:id="@+id/middleName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:ems="10" android:hint="@string/middleNameHint" android:imeOptions="actionDone" android:inputType="textPersonName" /> <EditText android:id="@+id/lastName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:ems="10" android:hint="@string/lastNameHint" android:imeOptions="actionDone" android:inputType="textPersonName" /> <EditText android:id="@+id/town" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:ems="10" android:hint="@string/townHint" android:imeOptions="actionDone" android:inputType="textPersonName" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:ems="10" android:hint="@string/passwordHint" android:imeOptions="actionDone" android:inputType="textPersonName" /> <EditText android:id="@+id/confirmPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:ems="10" android:hint="@string/confirmPasswordHint" android:imeOptions="actionDone" android:inputType="textPersonName" /> <Button android:id="@+id/joinButton" android:layout_width="match_parent" android:layout_height="49dp" android:layout_marginTop="25dp" android:background="@color/primaryRed" android:text="@string/join" android:textAllCaps="false" android:textColor="@android:color/white" android:textSize="22sp" /> </LinearLayout> </android.support.constraint.ConstraintLayout> </ScrollView> 元素上方滚动。使用图片,我只能向上滚动到第一个输入,并且无法继续向上滚动并看到绿色图标。使用我的实际XML布局,我无法滚动查看EditText组件。这是更新的布局:

TextView

0 个答案:

没有答案