可变高度滚动视图

Variable Height ScrollView

在android中,我有以下布局:

顶部有控件,可将数据加载到滚动视图中,用户可以 select 底部还有另一行带有保存按钮的控件。我的问题是我想要滚动视图来填充显示,而不是将底线推出视图。如果我在滚动视图上设置固定高度,根据设备的不同,它要么太小要么太高。我怎样才能使它的高度可变以匹配显示器,同时始终保持底线可见?

(编辑:这是我目前的尝试,它没有达到上述目的):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".Find">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/select_book_and_chapter"
            android:layout_width="393dp"
            android:layout_height="77dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/border"
            android:orientation="horizontal"
            android:padding="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <android.support.v7.widget.AppCompatSpinner
                android:id="@+id/books"
                android:layout_width="280dp"
                android:layout_height="55dp"
                android:layout_margin="10dp"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:background="@drawable/noborder"
                android:entries="@array/books"
                android:gravity="center"
                android:spinnerMode="dropdown"
                android:theme="@style/large_spinner"
                app:layout_constraintStart_toStartOf="parent"
                tools:layout_editor_absoluteY="320dp" />

            <android.support.v7.widget.AppCompatSpinner
                android:id="@+id/chapters"
                android:layout_width="81dp"
                android:layout_height="54dp"
                android:layout_margin="10dp"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:background="@drawable/noborder"
                android:entries="@array/chapter_one"
                android:gravity="center"
                android:spinnerMode="dropdown"
                android:theme="@style/large_spinner"
                app:layout_constraintStart_toStartOf="parent"
                tools:layout_editor_absoluteY="320dp" />


        </LinearLayout>

        <ScrollView
            android:id="@+id/scroll_verses"
            android:layout_width="387dp"
            android:layout_height="460dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:clipToPadding="false"
            android:fillViewport="true"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout2">

            <LinearLayout
                android:id="@+id/select_verse"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" />


        </ScrollView>


        <LinearLayout
            android:id="@+id/controls"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal"
            android:visibility="gone">

            <android.support.v7.widget.AppCompatSpinner
                android:id="@+id/folders"
                style="@style/Widget.AppCompat.Spinner.Underlined"
                android:layout_width="299dp"
                android:layout_height="36dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:background="@drawable/border"
                android:spinnerMode="dropdown"
                android:visibility="visible" />

            <EditText
                android:id="@+id/create_new_name"
                android:layout_width="265dp"
                android:layout_height="36dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:ems="10"
                android:background="@drawable/noborder"
                android:inputType="textPersonName"
                android:text="New Folder Nam"
                android:visibility="gone"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.578"
                app:layout_constraintStart_toEndOf="@+id/create_new_text"
                app:layout_constraintTop_toBottomOf="@+id/folders" />

            <Button
                android:id="@+id/cancel_new_folder"
                android:layout_width="35dp"
                android:layout_height="37dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:text="@string/cancel"
                android:visibility="gone" />

            <Button
                android:id="@+id/save_scripture"
                android:layout_width="wrap_content"
                android:layout_height="37dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:onClick="saveToDB"
                android:text="Save"
                android:visibility="visible" />

        </LinearLayout>


    </LinearLayout>


</FrameLayout>

您的布局应如下所示。此外,您应该尽量避免使用静态高度,而是使用 wrap_content 代替。我没有更改它,因为这可能是您的要求。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

<RelativeLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/select_book_and_chapter"
        android:layout_width="393dp"
        android:layout_height="77dp"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="horizontal"
        android:padding="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/books"
            android:layout_width="280dp"
            android:layout_height="55dp"
            android:layout_margin="10dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:gravity="center"
            android:spinnerMode="dropdown"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="320dp" />

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/chapters"
            android:layout_width="81dp"
            android:layout_height="54dp"
            android:layout_margin="10dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:gravity="center"
            android:spinnerMode="dropdown"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="320dp" />


    </LinearLayout>

    <ScrollView
        android:id="@+id/scroll_verses"
        android:layout_width="387dp"
        android:layout_height="match_parent"
        android:layout_below="@+id/select_book_and_chapter"
        android:layout_above="@+id/controls"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:clipToPadding="false"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/select_verse"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />


    </ScrollView>


    <LinearLayout
        android:id="@+id/controls"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:visibility="gone">

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/folders"
            style="@style/Widget.AppCompat.Spinner.Underlined"
            android:layout_width="299dp"
            android:layout_height="36dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:spinnerMode="dropdown"
            android:visibility="visible" />

        <EditText
            android:id="@+id/create_new_name"
            android:layout_width="265dp"
            android:layout_height="36dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="New Folder Nam"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.578"
            app:layout_constraintStart_toEndOf="@+id/create_new_text"
            app:layout_constraintTop_toBottomOf="@+id/folders" />

        <Button
            android:id="@+id/cancel_new_folder"
            android:layout_width="35dp"
            android:layout_height="37dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:visibility="gone" />

        <Button
            android:id="@+id/save_scripture"
            android:layout_width="wrap_content"
            android:layout_height="37dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:onClick="saveToDB"
            android:text="Save"
            android:visibility="visible" />

    </LinearLayout>


</RelativeLayout>
</FrameLayout>

您可以通过在滚动视图中设置 weight="1" 来实现此目的

让我给你样品

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/select_book_and_chapter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        ..... />

    <ScrollView
        android:id="@+id/scroll_verses"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" //Solution
        ...../>

    <LinearLayout
        android:id="@+id/controls"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        ..../>
</LinearLayout>

你可以使用RelativeLayout作为rootView,然后设置layout_alignParentTop为topView,设置layout_alignParentBottom为bottomView,最后设置上面的ScrollView bottomView 和 topView 的底部 layout_belowand layout_above。例如,

<RelativeLayout 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">
<LinearLayout
    android:layout_alignParentTop="true"
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:orientation="vertical">

</LinearLayout>
<ScrollView
    android:layout_below="@+id/top"
    android:layout_above="@+id/bottom"
    android:id="@+id/scroll_verses"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/select_verse"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>
<LinearLayout
    android:layout_alignParentBottom="true"
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:orientation="vertical">
</LinearLayout>

试试这个

 <?xml version="1.0" encoding="utf-8"?>
   <FrameLayout 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">

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

    <LinearLayout
        android:id="@+id/select_book_and_chapter"
        android:layout_width="393dp"
        android:layout_height="77dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/border"
        android:orientation="horizontal"
        android:padding="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/books"
            android:layout_width="280dp"
            android:layout_height="55dp"
            android:layout_margin="10dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:background="@drawable/noborder"
            android:entries="@array/books"
            android:gravity="center"
            android:spinnerMode="dropdown"
            android:theme="@style/large_spinner"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="320dp" />

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/chapters"
            android:layout_width="81dp"
            android:layout_height="54dp"
            android:layout_margin="10dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:background="@drawable/noborder"
            android:entries="@array/chapter_one"
            android:gravity="center"
            android:spinnerMode="dropdown"
            android:theme="@style/large_spinner"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="320dp" />


    </LinearLayout>

    <ScrollView
        android:id="@+id/scroll_verses"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:clipToPadding="false"
        android:fillViewport="true"
        android:layout_marginBottom="50dp"
        android:layout_below="@id/select_book_and_chapter">

        <LinearLayout
            android:id="@+id/select_verse"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />


    </ScrollView>


    <LinearLayout
        android:id="@+id/controls"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:visibility="gone"
        android:layout_alignParentBottom="true">

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/folders"
            style="@style/Widget.AppCompat.Spinner.Underlined"
            android:layout_width="299dp"
            android:layout_height="36dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:background="@drawable/border"
            android:spinnerMode="dropdown"
            android:visibility="visible" />

        <EditText
            android:id="@+id/create_new_name"
            android:layout_width="265dp"
            android:layout_height="36dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:ems="10"
            android:background="@drawable/noborder"
            android:inputType="textPersonName"
            android:text="New Folder Nam"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.578"
            app:layout_constraintStart_toEndOf="@+id/create_new_text"
            app:layout_constraintTop_toBottomOf="@+id/folders" />

        <Button
            android:id="@+id/cancel_new_folder"
            android:layout_width="35dp"
            android:layout_height="37dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:text="@string/cancel"
            android:visibility="gone" />

        <Button
            android:id="@+id/save_scripture"
            android:layout_width="wrap_content"
            android:layout_height="37dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:onClick="saveToDB"
            android:text="Save"
            android:visibility="visible" />

    </LinearLayout>


</RelativeLayout>


 </FrameLayout>