【问题标题】:Align two views in opposites sides (one at the top and one at the bottom) and be able to push the bottom view when the top view grows?对齐相对侧的两个视图(一个在顶部,一个在底部)并且能够在顶视图增长时推动底视图?
【发布时间】:2020-10-12 16:37:37
【问题描述】:

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

【问题讨论】:

    标签: android android-recyclerview android-constraintlayout android-nestedscrollview nestedrecyclerview


    【解决方案1】:

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

    <?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_height="wrap_content"app:layout_constrainedHeight="true" 的组合以及顶部和底部约束意味着 RecyclerView 将始终仅与它的项目一样高 EditText 下方的可用空间,以较小者为准。

    app:layout_constraintVertical_bias="1" 属性确保当列表没有填满屏幕时,它位于底部。

    【讨论】:

      【解决方案2】:

      我会添加到上一个答案,如果你想能够滚动你的布局,你可以这样使用:

      <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>
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-09
        • 2021-06-28
        • 1970-01-01
        • 2015-10-04
        • 1970-01-01
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多