填充布局直到下一个布局(在底部)

时间:2018-05-05 00:39:53

标签: android android-layout android-constraintlayout android-relativelayout

所以,我有一个标题,然后是一个CardView,它应该填满屏幕直到页脚。问题是,它填满了所有屏幕,并且页脚在屏幕下方,正如您在图像中看到的那样。

我想保持页脚(按钮+两个图像)始终位于底部,并且cardview应始终填充屏幕的其余部分。在cardview中有一个滚动视图,可以在cardview中读取文本。

无论如何,这是 XML代码:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_10">

    <View
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:background="@color/colorPrimary" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:layout_marginBottom="@dimen/spacing_mlarge"
        android:orientation="vertical"
        android:padding="@dimen/spacing_mlarge">

    <TextView
        android:id="@+id/qstView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_marginBottom="20dp"
        android:text="Insert question text here"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textColor="@color/white" />

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

        <android.support.v7.widget.CardView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="@dimen/spacing_middle"
            android:layout_marginEnd="@dimen/spacing_middle"
            android:layout_marginLeft="@dimen/spacing_middle"
            android:layout_marginRight="@dimen/spacing_middle"
            android:layout_marginStart="@dimen/spacing_middle"
            android:layout_marginTop="@dimen/spacing_middle"
            android:visibility="visible"
            app:cardCornerRadius="6dp"
            app:cardElevation="5dp">

            <ScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

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

                    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/checkbox_full_layout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_margin="5dp"
                        android:orientation="vertical">

                    </LinearLayout>

                </LinearLayout>
            </ScrollView>

        </android.support.v7.widget.CardView>
    </LinearLayout>
    <Button
        android:id="@+id/add_field_button"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:onClick="onAddAnswer"
        android:layout_gravity="bottom"
        android:layout_marginLeft="-4dp"
        android:layout_marginRight="-4dp"
        android:background="@drawable/btn_rounded_primary"
        android:text="Suivant"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:textStyle="bold" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="@dimen/spacing_mlarge"
        android:layout_gravity="center_horizontal"
        android:gravity="center">
        <ImageView
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="@dimen/spacing_mlarge"
            android:layout_marginTop="@dimen/spacing_large"
            android:src="@drawable/ineedhelp"/>
        <View
            android:layout_width="@dimen/spacing_large"
            android:layout_height="0dp" />
        <ImageView
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="@dimen/spacing_mlarge"
            android:layout_marginTop="@dimen/spacing_large"
            android:src="@drawable/mbtouch_dark"/>
    </LinearLayout>
        <View
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/spacing_middle"
            android:layout_alignParentBottom="true"/>
    </LinearLayout>
</RelativeLayout>

这就是我现在所拥有的:

Relative Layout - I have at the moment

这就是我想要的(这里我强制尺寸,但由于并非所有设备都相同,因此不会以最佳方式使用空间。)

Relative Layout - I want

提前致谢!

1 个答案:

答案 0 :(得分:2)

尝试按照以下方式排列您的观点(对于CardView和底部布局,其他内容将保持原样)..

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

     <!--  Since, weight is 1 it will occupy whole space subtracting height of bottom  -->
     <CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        .... />

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

        <!--  Put your bottom widgets over here  -->

    </LinearLayout>

</LinearLayout>