Android 在RecyclerView上设置最大高度
我有一个对话框片段,其中包含线性布局,在RecyclerView上方有一个titleText,在最底部,在RecyclerView下方有一个按钮 由于recyclerView根据适配器设置的项目数展开或折叠,因此按钮有时会被截断,不再显示在屏幕上,因为recyclerView只覆盖整个屏幕 我的问题是,有没有一种方法可以在不隐藏按钮的情况下设置recyclerView的最大高度。我也不想只是给视图一个随机高度,以防recyclerView不包含任何项目,它只是一个空白部分 请让我知道你是否遇到过这个问题,以及你是如何解决这个问题的。谢谢 更新 使用布局权重可以轻松实现这一点。下面是一个例子:Android 在RecyclerView上设置最大高度,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,我有一个对话框片段,其中包含线性布局,在RecyclerView上方有一个titleText,在最底部,在RecyclerView下方有一个按钮 由于recyclerView根据适配器设置的项目数展开或折叠,因此按钮有时会被截断,不再显示在屏幕上,因为recyclerView只覆盖整个屏幕 我的问题是,有没有一种方法可以在不隐藏按钮的情况下设置recyclerView的最大高度。我也不想只是给视图一个随机高度,以防recyclerView不包含任何项目,它只是一个空白部分 请让我知道你是否遇到过
<?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>
标题和回收视图将根据内容包装内容,按钮将始终占据底部位置。我建议使用
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_在下方或android:layout_在上方或android:layout_开始
或者android:layout_end,它将您的视图与您的方式完美地对齐
需要
此解决方案基本上与设置layoutHeight参数相同,因为即使视图为空且应压缩,它仍会将80%的视图分发给recyclerView?感谢更新!这是一个很酷的解决方案。不过,本质上这个按钮只会覆盖在recyclerView项目的顶部。只需在你的recycler视图中添加一个填充底部。我永远不会喜欢这个解决方案,因为它不能跨不同的屏幕进行扩展。而且它有不必要的视图嵌套。毫无疑问,它是有效的(在某种程度上),但所需的行为可以很容易地实现与更简单的布局结构。没有任何使用重量在您的布局思想检查这一点,它为我工作