【问题标题】:RecyclerView fill empty space inside LinearLayoutRecyclerView 填充 LinearLayout 内的空白空间
【发布时间】:2018-07-12 17:54:04
【问题描述】:

背景

我有一个页眉和页脚 (LinearLayout),它们之间有一个 RecyclerView

问题

我需要页脚固定在屏幕底部,RecyclerView 填充空白空间

我尝试过的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/constraintLayout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center_horizontal"
              android:orientation="vertical"
              tools:context="br.com.sigane.coletordifal.activity.EnderecamentoActivity">
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Header" />
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
                  android:id="@+id/produtos_list"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:scrollbars="vertical" />
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Footer" />
    </LinearLayout>
</LinearLayout>

也尝试使用ConstraintLayout,但没有成功

【问题讨论】:

    标签: android android-layout android-recyclerview


    【解决方案1】:

    试试这个

    android:layout_height="0dp"android:layout_weight="1" 添加到RecyclerView

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Header" />
    
        </LinearLayout>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/produtos_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scrollbars="vertical" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Footer" />
    
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      我认为您不需要单独的线性布局。我觉得一个就够了。只需像这样为您的回收站视图和页脚设置权重。

       <android.support.v7.widget.RecyclerView
              android:id="@+id/produtos_list"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="2"
              android:scrollbars="vertical" />
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="0dp"
                  android:layout_weight="1"
              android:text="Footer" />
      

      【讨论】:

      • 我在问题中输入的这段代码只是一个例子。我已经从标题中删除了线性布局
      【解决方案3】:

      我推荐使用约束布局,它是一个非常强大的 FrameLayout。如果您使用的是 android studio 3.2,右键单击设计视图中的根元素会显示一个菜单,其中包含“转换为约束布局”选项

      您不想像这样嵌套布局的原因是,每个嵌套元素都会导致额外的度量传递,嵌套布局比平面布局更混乱且更难膨胀。

      强烈建议花一个小时左右的时间阅读约束布局以及如何使用它。你不会后悔的,帮助我将工作效率提高了 2 倍。

      希望这有帮助。

      【讨论】:

      • ConstraintLayout 是类固醇上的 FrameLayout?哥们,你把谷歌 CoordinatorLayout 的标语和 Constraintlayout 混淆了
      • constraintlayout 在某种意义上与框架布局非常相似,我们必须像使用框架布局一样为其指定位置。
      • 嗯……不!老兄阅读了 CoordinatorLayout 的源代码。它实际上是 FrameLayout。 ConstraintLayout 更类似于RelativeLayout。事实上,它是RelativeLayout的替代品。
      • 你的论点正是你在为论点而争论的感觉,我没有说明任何关于扩展的任何事情,我只是在谈论行为,我完成了这次谈话,它对手头的问题没有帮助。请在讨论中保持礼貌。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多