Android RecyclerView以action button作为最后一个元素,但当有许多项目时,它会浮动或粘在底部
我想要实现的是一个屏幕,其中我有一个显示列表的RecyclerView。。它可能有很多或很少的项目,当它有很多项目时,按钮必须在屏幕的末端浮动和粘滞 如果项目很少,屏幕上有空白,则该按钮必须作为RecyclerView列表中的最后一个元素出现Android RecyclerView以action button作为最后一个元素,但当有许多项目时,它会浮动或粘在底部,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,我想要实现的是一个屏幕,其中我有一个显示列表的RecyclerView。。它可能有很多或很少的项目,当它有很多项目时,按钮必须在屏幕的末端浮动和粘滞 如果项目很少,屏幕上有空白,则该按钮必须作为RecyclerView列表中的最后一个元素出现 按钮是一个带背景的矩形凸起按钮。有没有什么建议可以实现这样的效果。一个选项是让按钮一直在列表视图上方浮动,然后手动将其定位,使其看起来位于列表的底部。如果你控制了这个位置,你可以决定它什么时候应该粘在底部 请参阅有关如何在运行时获取视图高度的帖子 view
按钮是一个带背景的矩形凸起按钮。有没有什么建议可以实现这样的效果。一个选项是让按钮一直在列表视图上方浮动,然后手动将其定位,使其看起来位于列表的底部。如果你控制了这个位置,你可以决定它什么时候应该粘在底部 请参阅有关如何在运行时获取视图高度的帖子
view.measure(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
int height=view.getMeasuredHeight();
有关如何获得屏幕高度,请参阅文章
int screenHeight = getWindowManager().getDefaultDisplay().getHeight()
然后在layout.xml中
`android:layout_alignParentBottom="true"` on the element
然后使用信息定位按钮
现在奇迹发生了
int offset = screenHeight - ButtonHeight - listHeight;
if(offset < 0) offset = 0;
btn.setTop(offset);
int offset=屏幕高度-按钮高度-列表高度;
如果(偏移量<0)偏移量=0;
btn.setTop(偏移量);
这样做吗
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_gravity="bottom|center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
什么时候没有什么东西
你可以这样做
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weight_sum ="10">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"/>
<Button
android:text="button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
这和上面的答案是一样的。但不同之处在于,按钮将始终与底部对齐,重量为1。这是一种奇怪的设计。如果你用和GMail和其他许多应用程序一样的方式做一个浮动按钮呢?这将符合谷歌的指导方针。如果我有选择的余地,我就不会在这里了:D。。这是业务需求