即使顶视图的高度比屏幕大/小,如何设置底视图始终低于顶视图?

问题描述 投票:0回答:1

[I have 3 layouts inside relative layout like this]1

我的目标是使布局 3 始终位于布局 2 (回收器视图)下方。

当列表项尺寸较小时,它工作得很好,但当它变大时,布局 3 就会被推出屏幕

即使布局 2 的高度增加或减少,我也想将布局 3 始终调整到布局 2 下方。如果它增长超过屏幕尺寸,只需将布局 3 放在底部,布局 2 放在布局 3 上方。

  1. 布局 1 为标题。
  2. 布局 2 是 Recycler 视图。
  3. 布局 3 是摘要。

我尝试将parentBottom true设置为布局3,当列表项充满屏幕时它可以工作,但如果列表项较少(例如,4、5个项目),它仍保留在底部。

请帮助我。

我的xml是

<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout
        android:id="@+id/bill_heading_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">

        <TextView
            style="@style/billing_heading"
            android:layout_weight=".3"
            android:text="@string/sn" />

        <TextView
            style="@style/billing_heading"
            android:layout_weight="1.8"
            android:text="@string/item_name" />

        <TextView
            android:id="@+id/rateTv"
            style="@style/billing_heading"
            android:layout_weight=".7"
            android:text="@string/rates" />

        <TextView
            style="@style/billing_heading"
            android:layout_weight="0.5"
            android:text="@string/qty" />

        <TextView
            style="@style/billing_heading"
            android:layout_weight=".9"
            android:text="@string/total_value" />

    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/billing_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/bill_heading_layout" />

    <LinearLayout
        android:layout_below="@id/billing_recycler"
        android:id="@+id/summary_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/billing_border"
        android:orientation="vertical">

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

            <TextView
                style="@style/billing_summary"
                android:layout_weight="2.8"
                android:gravity="end|center"
                android:paddingStart="0dp"
                android:paddingEnd="10dp"
                android:text="@string/sub_total" />

            <TextView
                android:id="@+id/subQtyTv"
                style="@style/billing_summary"
                android:layout_weight="0.5"
                android:text="@string/qty" />

            <TextView
                android:id="@+id/subTotalTv"
                style="@style/billing_summary"
                android:layout_weight=".9"
                app:autoSizeTextType="uniform"
                android:text="@string/total_value" />

        </LinearLayout>

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

            <TextView
                style="@style/billing_summary"
                android:layout_weight="2.8"
                android:gravity="end|center"
                android:paddingStart="0dp"
                android:paddingEnd="10dp"
                android:text="@string/discount" />

            <TextView
                style="@style/billing_summary"
                android:layout_weight="0.5" />

            <TextView
                android:id="@+id/discountTv"
                style="@style/billing_summary"
                android:layout_weight=".9"
                app:autoSizeTextType="uniform"
                android:text="@string/total_value" />
        </LinearLayout>

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

            <TextView
                style="@style/billing_summary"
                android:layout_weight="2.8"
                android:gravity="end|center"
                android:paddingStart="0dp"
                android:paddingEnd="10dp"
                android:text="@string/scheme" />

            <TextView
                style="@style/billing_summary"
                android:layout_weight="0.5" />

            <TextView
                android:id="@+id/schemeTv"
                style="@style/billing_summary"
                android:layout_weight=".9"
                android:text="@string/total_value"
                app:autoSizeTextType="uniform" />
        </LinearLayout>

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

            <TextView
                style="@style/billing_summary"
                android:layout_weight="2.8"
                android:gravity="end|center"
                android:paddingStart="0dp"
                android:paddingEnd="10dp"
                android:text="@string/grand_total" />

            <TextView
                style="@style/billing_summary"
                android:layout_weight="0.5" />

            <TextView
                android:id="@+id/grandTotalTv"
                style="@style/billing_summary"
                android:layout_weight=".9"
                android:text="@string/total_value"
                app:autoSizeTextType="uniform" />
        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

[Result 1]2

[Rusult 2: when parentBottom=ture]3

我尝试了第二种方法,使用约束布局作为父布局,如下所示

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:gravity="center"
    android:text="@string/app_name"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:spanCount="29" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/make_report"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/recyclerView" />

[And I got the result as this]4

最底部的按钮被推出。我尝试了

app:layout_constraintBottom_toBottomOf="parent"
按照https://stackoverflow.com/users/6067590/akanshi-srivastava建议的答案,它适用于具有更多跨度计数的列表项。当列表计数达到 4 或 5 时,列表项和按钮之间存在间隙,如下所示 [gap between list and button]5,在这个布局中我不希望有间隙。我希望,如果列表数量较少,希望按钮像这样附加在列表下方 [button just below list]6,当列表增长超过屏幕大小时,我希望按钮仅附加在底部并列表直到上方按钮并开始滚动。

xml android-layout android-recyclerview
1个回答
0
投票

您应该使用

ConstrainLayout
并且可以设置底部视图的

 app:layout_constraintBottom_toBottomOf="parent"

这样,无论您向屏幕添加什么以及它的宽度/高度是什么,该视图都将始终位于底部。 最重要的是,我看到你有很多嵌套的

RelativeLayout
LinearLayout
ConstraintLayout
将帮助您展平布局层次结构。您可以阅读官方文档来了解
ConstraintLayout
的好处以及如何使用它。

© www.soinside.com 2019 - 2024. All rights reserved.