[英]Place a recyclerview between 2 views on top and bottom
我正在尝试实现这种布局
<RelativeLayout>
<Button> <!-- Always attached to the top of the parent -->
<RecyclerView>
<Button> <!-- Always attached to the bottom of the parent -->
<RelativeLayout>
这是代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewTemperatureLog"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
我的问题是,尽管视图放置正确,但是每当我滚动到RecyclerView
的最后一项时,它都会部分隐藏在底部Button
后面。 无论RecyclerView
中有多少个项目,如何将RecyclerView
恰好放在两个按钮之间?
您需要告诉RecyclerView在按钮的上方和下方。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/topButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewTemperatureLog"
android:layout_above="@+id/bottomButton"
android:layout_below="@+id/topButton"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<Button
android:id="@+id/bottomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
您始终可以在相对布局内指定回收者视图的位置。 您可以将其放在视图的顶部,底部或两个视图上。
首先,您需要在按钮上添加ID,然后您可以指定回收站视图的位置
android:layout_below="@+id/button_top" // place the view below the specified view id
android:layout_above="@+id/button_bottom" // place the view above the specified view id
单击此处查看有关如何在RelativeLayout
放置视图的完整列表
指定layout_weight
属性时,必须使用LinearLayout
作为父项:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewTemperatureLog"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.