可变高度ScrollView

时间:2018-08-06 04:24:15

标签: android layout android-studio-3.0

在android中,我的布局如下:

Android Layout

顶部有控件,这些控件将数据加载到用户可以选择的滚动视图中,然后将底部的另一行带有保存按钮的控件加载到滚动视图中。我的问题是我想滚动视图以填充显示,但不将底线从视图中推出。如果在滚动视图上设置了固定高度,则视设备而定,它可能太小或太高。如何获得它以使其高度可变以匹配显示,同时始终保持底线可见?

(编辑:这是我目前的尝试,但无法实现以上目的):

<?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>

4 个答案:

答案 0 :(得分:1)

这是您的布局外观。另外,您应尝试避免使用静态高度,而应执行wrap_content。我没有更改,因为也许这是您的要求。

Setup

答案 1 :(得分:1)

您可以通过在滚动视图中设置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>

答案 2 :(得分:0)

您可以使用RelativeLayout作为rootView,然后将layout_alignParentTop设置为topView,并将layout_alignParentBottom设置为bottomView。最后,将ScrollView设置为bottomView和topView的底部,包含layout_belowlayout_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>

答案 3 :(得分:0)

尝试这个

 <?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>