显示键盘时使部分布局始终可见

Making part of layouts always visible when keyboard is shown

我有一个 DialogFragment 布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
android:background="@drawable/rounded_rect_light">

<RelativeLayout
    android:id="@+id/create_note_top_panel"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#f2f3f5">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Cancel"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:gravity="center"
        android:layout_centerInParent="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Send"/>
</RelativeLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/quote_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_alignParentTop="true"
            android:src="@drawable/edu4_create_note_quotes"/>

        <TextView
            android:id="@+id/quote_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/quote_image"
            android:scrollbars="vertical"
            android:fadeScrollbars="false"
            android:text="The issue is that soft keyboard is very tricky and this\n quesakdf\njk\nlsd\nfj\nklsdjfklsdjfkl jsdklfj skldjkljfklsdjklfjsdklfjsdkljfkls"
            android:maxLines="5"/>

        <EditText
            android:layout_width="match_parent"
android:layout_height="150dp"
            android:layout_below="@id/quote_text"
            android:layout_margin="10dp"
            android:gravity="top|left"
            android:background="@drawable/rounded_rect_background_white"
            android:padding="5dp"/>
    </RelativeLayout>
</ScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="15dp"
    android:layout_marginLeft="10dp">

    <TextView
        android:id="@+id/visibleTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:text="Publish"/>

    <ToggleButton
        android:id="@+id/switchPublishToCourse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"/>
</LinearLayout>

我希望当键盘打开时,顶部面板 (android:id="@+id/create_note_top_panel") 将在顶部可见,对话框(它的背景)将在键盘上方,底部 TextView ToggleButton 将可见,布局的其余部分将可滚动。

找到答案:将android:layout_weight="0" to the parts that you want to stay visible andandroid:layout_weight="1"添加到可以调整大小的那个。 这是完整的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
android:background="@drawable/rounded_rect_light">

<RelativeLayout
    android:id="@+id/create_note_top_panel"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#f2f3f5">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Cancel"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:gravity="center"
        android:layout_centerInParent="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Send"/>
</RelativeLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_marginTop="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/quote_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_alignParentTop="true"
            android:src="@drawable/edu4_create_note_quotes"/>

        <TextView
            android:id="@+id/quote_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:fadeScrollbars="false"
            android:layout_toRightOf="@id/quote_image"
            android:text="The issue is that soft keyboard is very tricky and this\n quesakdf\njk\nlsd\nfj\nklsdjfklsdjfkl jsdklfj skldjkljfklsdjklfjsdklfjsdkljfkls\nend of tex"
            android:maxLines="5"/>

        <EditText
            android:id="@+id/create_note_text"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_below="@id/quote_text"
            android:layout_margin="10dp"
            android:gravity="top|left"
            android:background="@drawable/rounded_rect_background_white"
            android:padding="5dp"/>
    </RelativeLayout>
</ScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="0"
    android:gravity="center_vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp">

    <TextView
        android:id="@+id/visibleTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:text="Publish"/>

    <ToggleButton
        android:id="@+id/switchPublishToCourse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"/>
</LinearLayout>

并在 Fragment class:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}