Android 将两个视图在相反的侧面对齐(一个在顶部,一个在底部),并且能够在顶部视图增长时推动底部视图?

Android 将两个视图在相反的侧面对齐(一个在顶部,一个在底部),并且能够在顶部视图增长时推动底部视图?,android,android-recyclerview,android-constraintlayout,android-nestedscrollview,nestedrecyclerview,Android,Android Recyclerview,Android Constraintlayout,Android Nestedscrollview,Nestedrecyclerview,我基本上在视图的顶部有一个EditText对齐,在视图的底部有一个RecyclerView,它也可以增长(最新的项目在底部) 使用约束布局很容易做到这一点,但我的问题是,当EditText增长时,它应该开始向下推列表。但列表最初是在父级的底部对齐的。(所有内容都应该是可滚动的) 我希望这张照片能让事情变得更清楚 这里的技巧是避免形成垂直链(这样顶部的EditText始终保持在原位),并且还可以利用RecycleService上的app:layout_uconstraintedheight属性,以

我基本上在视图的顶部有一个EditText对齐,在视图的底部有一个RecyclerView,它也可以增长(最新的项目在底部) 使用约束布局很容易做到这一点,但我的问题是,当EditText增长时,它应该开始向下推列表。但列表最初是在父级的底部对齐的。(所有内容都应该是可滚动的) 我希望这张照片能让事情变得更清楚


这里的技巧是避免形成垂直链(这样顶部的EditText始终保持在原位),并且还可以利用RecycleService上的
app:layout_uconstraintedheight
属性,以便在编辑文本增长时使其收缩

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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">

    <EditText
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/text"
        app:layout_constraintVertical_bias="1"/>

</androidx.constraintlayout.widget.ConstraintLayout>

android:layout\u height=“wrap\u content”
app:layout\u constrained height=“true”
的组合,以及顶部和底部约束,意味着RecyclerView始终只与其项目或编辑文本下方的可用空间一样高,以较小者为准


app:layout\u constraintVertical\u bias=“1”
属性确保当列表没有填满屏幕时,它位于底部。

我将添加到前面的答案中,如果您希望能够滚动布局,可以使用如下内容:

<androidx.core.widget.NestedScrollView 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:fillViewport="true">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/edit_text"
        app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

这将使所有内容按照您的意愿对齐,您可以向下滚动查看完整的
recyclerView
NestedScrollView
将确保一切顺利运行