我正在努力实现这种布局
<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
?
答案 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>