我有一个非常简单的问题,出于某种原因,我无法执行。
我有一个RecyclerView,它在创建 Activity 时仅被填充一次。我想在它下面添加另一个 View 。
如果RecyclerView不能填满整个屏幕长度(通常在纵向模式下),则 View 应直接位于其下方。
如果RecyclerView超出了屏幕长度(通常在横向模式下),则 View 仍应位于其下方,但也应停靠在屏幕底部。
就是这样。
我试过将它们放置在RelativeLayout,LinearLayout或CoordinatorLayout中。使用match_parent,wrap_content(带有或不带有setAutoMeasureEnabled)。使用app:layout_anchor和app:layout_anchorGravity,但没有任何效果。
感谢任何帮助。
这是我尝试之一的示例:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/features_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchor="@id/features_list"
app:layout_anchorGravity="bottom" />
</android.support.design.widget.CoordinatorLayout>
最佳答案
试试这个:
注意:我使用的是ScrollView,因为您说过RecyclerView的高度可能超过了屏幕尺寸。
<?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">
<!-- Required for the content to not exceed the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:measureAllChildren="true">
<!-- Content -->
</ScrollView>
<!-- View that needs to be under the ScrollView or
clipped to the button of the screen based on the ScrollView height -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
</LinearLayout>