在顶部和底部的2个视图之间放置一个recyclerview

时间:2017-03-13 14:39:54

标签: android android-layout android-recyclerview

我正在努力实现这种布局

<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中有多少项目,我怎么能在{2}按钮之间准确放置RecyclerView

3 个答案:

答案 0 :(得分:2)

您需要告知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>

答案 1 :(得分:0)

您始终可以在相对布局中指定回收站视图的位置。您可以将它放在视图的顶部,底部或两者上。

首先,你需要在按钮上添加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

Click here查看有关如何在RelativeLayout

中定位视图的完整列表

答案 2 :(得分:0)

在指定LinearLayout属性时,必须使用layout_weight作为父级:

<?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>
相关问题