【问题标题】:Setting maximum height on RecyclerView在 RecyclerView 上设置最大高度
【发布时间】:2016-09-29 21:54:54
【问题描述】:

我有一个包含线性布局的对话框片段,其中涉及 RecyclerView 上方的 titleText,在最底部,recyclerView 下方有一个按钮。

由于 recyclerView 会根据适配器设置的项目数展开或折叠,因此按钮有时会被截断并且不再出现在屏幕上,因为 recyclerView 只会覆盖整个屏幕。

我的问题是,有没有一种方法可以设置 recyclerView 的最大高度,而无需隐藏下方的按钮。我也不想只给视图一个随机高度,以防 recyclerView 不包含任何项目,它只是一个空白部分。

如果您遇到过这个问题,请告诉我,以及您是如何解决这个问题的。谢谢!

【问题讨论】:

  • 检查它对我有用link

标签: android android-layout android-recyclerview


【解决方案1】:

更新 您可以使用布局权重轻松实现此目的。这是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Title"
            android:textSize="21sp"/>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="30dp">
        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Submit"/>
</FrameLayout>

Title 和 RecyclerView 将根据内容包装内容,并且按钮将始终占据底部。

【讨论】:

  • 这个解决方案本质上和设置一个layoutHeight参数是一样的,因为它仍然会将80%的视图分配给recyclerView,即使它是空的并且应该被压缩?
  • 感谢您的更新!这是一个很酷的解决方案。然而,基本上这个按钮只会覆盖在 recyclerView 项目的顶部。
  • 只需在您的回收站视图中添加一个填充底部。
  • 我永远不会喜欢这个解决方案,因为它不能跨不同的屏幕进行扩展。而且它有不必要的视图嵌套。毫无疑问,它(在某种程度上)是有效的,但可以通过更简单的布局结构轻松实现所需的行为。
  • 在你的布局中没有任何使用权重
【解决方案2】:

我建议使用RelativeLayout,因为它可以处理像您这样的案例的视图定位,这样您就可以真正专注于主要设计。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Some title" />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/title"
        android:layout_above="@+id/button"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"/>
</RelativeLayout>

上面的 XML 代码是您需要的框架代码。您可以添加边距和尺寸来控制间距。但无论如何(除非您提供负边距)您的视图永远不会相互重叠。

使用 RelativeLayout 的主要技巧是能够使用 XML 标签,例如 android:layout_below 或 android:layout_above 或 android:layout_start 或 android:layout_end 以您的方式完美对齐您的视图 想要。

【讨论】:

    猜你喜欢
    • 2017-07-30
    • 2017-05-22
    • 2012-02-16
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2021-07-27
    相关资源
    最近更新 更多