将RecyclerView与LinearLayout对齐(Aligning a RecyclerView below a LinearLayout)

编程入门 行业动态 更新时间:2024-10-27 16:27:17
RecyclerView与LinearLayout对齐(Aligning a RecyclerView below a LinearLayout)

我有一个称为“页脚”的线性布局我想放在我的RecyclerView (API 21中引入的一种新类型列表)下面。

recyclerview可能希望具有"match_parent"高度以有效地使用内存(避免不必要的计算)。 我不应该首先使用recyclerview因为它将占用整个屏幕并且页脚不可见。 由于recyclerview在第二位, linearlayout不应该具有"match_parent"高度,因为那时将不会看到recyclerview ,并且我不需要页脚来伸展。

所以我有了linearlayout "footer" ,这是我的root relativelayout第一个高度,然后linearlayout将是"match_parent" ,并在“footer”上方指定:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <LinearLayout android:id="@+id/footer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/addTaskImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:src="@drawable/ic_edit" android:visibility="visible" /> <RelativeLayout android:id="@+id/addTaskRL" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone"> <EditText android:id="@+id/taskET" android:layout_width="match_parent" android:layout_height="150sp" android:background="@color/White"> </EditText> <RelativeLayout android:id="@+id/paletteRed" android:layout_width="30dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="50dp" android:layout_marginTop="10dp" android:background="@color/Red" /> <RelativeLayout android:id="@+id/paletteOrange" android:layout_width="30dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/paletteRed" android:background="@color/Orange" /> <RelativeLayout android:id="@+id/paletteGreen" android:layout_width="30dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/paletteOrange" android:background="@color/Green" /> <RelativeLayout android:id="@+id/paletteRoyalBlue" android:layout_width="30dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/paletteGreen" android:background="@color/RoyalBlue" /> <RelativeLayout android:id="@+id/palettePurple" android:layout_width="30dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/paletteRoyalBlue" android:background="@color/Purple" /> <Button android:id="@+id/submitBtn" android:layout_width="70dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/palettePurple" android:background="@color/SkyBlue" android:textColor="@color/White" android:text="@string/submit"> </Button> </RelativeLayout> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/tasksRV" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/footer"/> </RelativeLayout>

我知道这是一个布局问题,因为如果我删除了页脚,我的recyclerview会显示出来。 我对上面的布局有什么误解?

I have a linear layout called "footer" I want to place below my RecyclerView (a new type of list introduced in API 21).

The recyclerview will probably want to have a "match_parent" height to efficiently use memory (avoid unecessary calculation). I shouldn't have the recyclerview first because then it will take up the whole screen and the footer wont be visible. Since the recyclerview comes second the linearlayout shouldn't have a "match_parent" height, because then the recyclerview won't be seen, and I don't need the footer to stretch anyways.

So I have the linearlayout "footer", a height first in my root relativelayout, then the recyclerview will be "match_parent", and specified above the "footer":

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <LinearLayout android:id="@+id/footer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/addTaskImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:src="@drawable/ic_edit" android:visibility="visible" /> <RelativeLayout android:id="@+id/addTaskRL" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone"> <EditText android:id="@+id/taskET" android:layout_width="match_parent" android:layout_height="150sp" android:background="@color/White"> </EditText> <RelativeLayout android:id="@+id/paletteRed" android:layout_width="30dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="50dp" android:layout_marginTop="10dp" android:background="@color/Red" /> <RelativeLayout android:id="@+id/paletteOrange" android:layout_width="30dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/paletteRed" android:background="@color/Orange" /> <RelativeLayout android:id="@+id/paletteGreen" android:layout_width="30dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/paletteOrange" android:background="@color/Green" /> <RelativeLayout android:id="@+id/paletteRoyalBlue" android:layout_width="30dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/paletteGreen" android:background="@color/RoyalBlue" /> <RelativeLayout android:id="@+id/palettePurple" android:layout_width="30dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/paletteRoyalBlue" android:background="@color/Purple" /> <Button android:id="@+id/submitBtn" android:layout_width="70dp" android:layout_height="30dp" android:layout_below="@id/taskET" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/palettePurple" android:background="@color/SkyBlue" android:textColor="@color/White" android:text="@string/submit"> </Button> </RelativeLayout> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/tasksRV" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/footer"/> </RelativeLayout>

I know this is a layout issue only because if I remove the footer my recyclerview is displayed. What is my misunderstanding of layouts above?

最满意答案

你只是忘了添加这一行

android:layout_alignParentBottom="true"

到你的页脚布局,所以将它锚定到RelativeLayout的底部。

You just forgot to add this line

android:layout_alignParentBottom="true"

to your footer layout, so to anchor it to the bottom of the RelativeLayout.

更多推荐

本文发布于:2023-04-28 01:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329622.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RecyclerView   LinearLayout   Aligning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!