Constraintlayout Vertical Bias 在两个视图中不起作用 Android [英] Constraintlayout Vertical Bias not working in two views Android

本文介绍了Constraintlayout Vertical Bias 在两个视图中不起作用 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在使用带有垂直偏差的约束布局来填充 recyclerview 中的视图.我成功做到了.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app=http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width=match_parent"android:layout_height=match_parent"android:background="@color/white"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recylerview";android:layout_width=match_parent"android:layout_height="wrap_content";android:paddingStart="10dp";android:paddingEnd="10dp";android:paddingBottom="10dp";app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager";app:layout_constraintBottom_toBottomOf=父级"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf=父"app:layout_constraintVertical_bias="0";/></androidx.constraintlayout.widget.ConstraintLayout>

我的 recyclerview 项目以相反的顺序显示我,因为我想制作聊天应用程序.所以我添加了 reverseLayoutstackFromEnd true.所以它看起来像这样.

如果我的项目是单一的,我的 ui 通过上面的 xml 代码转换成这样

现在我想在底部添加编辑文本和按钮.它不起作用垂直偏差.有谁知道怎么修.对我可以使用什么的任何建议

预期产出

场景一

场景 2

实际输出

我试过这个代码

解决方案

这个想法是您的 RV 将自行处理其自己的项目大小和空间.您无需为此担心.

剩余空间,将被 ConstraintLayout 使用.

因此您需要固定 RV 和底部容器";一起.这将默认导致 CL 尝试拉取和平衡布局,因此您必须告诉底部容器在底部 偏置.

在伪 XML 中:

<RecyclerView宽度=0dp高度=0dp开始/结束 = 父TopToTop=父级BottomToTopOf=底部容器><底部容器>宽度=0dp高度=包裹内容TopToBottomOf=RecyclerViewBottomToBottomOf=父级开始/结束 = 父级></ConstraintLayout>

现在还需要做一件事,因为 RV 会占用所有空间,您需要确保 BottomContainer 有更多关于它想要放置的位置的信息.

所以你添加:

app:layout_constraintVertical_bias=1.0";

到底部容器.

这意味着在垂直方向上为底部"尽你所能.(0.0 则相反).

最终结果是:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width=match_parent"android:layout_height=match_parent"xmlns:app=http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recylerview";android:layout_width=0dp"android:layout_height=0dp"android:paddingStart="10dp";android:paddingEnd="10dp";android:paddingBottom="10dp"app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager";app:layout_constraintBottom_toBottomOf=父级"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf=父"工具:listitem =@android:layout/simple_list_item_2";/><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width=0dp"app:layout_constraintVertical_bias=1.0";android:layout_height="wrap_content";android:id="@+id/bottomContainer";app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toBottomOf=@id/recylerview"app:layout_constraintBottom_toBottomOf=父"><编辑文本android:id="@+id/editTextContainer";android:layout_width=0dp"android:layout_height="wrap_content";app:layout_constraintBottom_toBottomOf=父级"app:layout_constraintEnd_toStartOf=@+id/button";app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf=父"/><按钮android:id="@+id/button";android:layout_width="wrap_content";android:layout_height="wrap_content";android:text="按钮"app:layout_constraintBottom_toBottomOf=父级"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toEndOf="@+id/editTextContainer";app:layout_constraintTop_toTopOf=父"/></androidx.constraintlayout.widget.ConstraintLayout></androidx.constraintlayout.widget.ConstraintLayout>

看起来像这样:

更新

如果同时添加

 app:stackFromEnd="true";app:reverseLayout="true";

它的工作方式相同,除了项目0"是在底部,最后"是项目在顶部.添加另一个项目会将其添加到顶部,但 BottomContainer 对此没有参与(或影响):

Hey I am using constraint layout with vertical bias to fill view in recyclerview. I successfully did that.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recylerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:paddingBottom="10dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0" />

</androidx.constraintlayout.widget.ConstraintLayout>

My recyclerview items showing me in reverse order because I want to make chat application. So I added reverseLayout and stackFromEnd true. So it look like this.

If my item is single my ui convert into like this by above xml code

Now I want to add edittext and button in bottom. Its not working vertical bias. Does any one know how to fix. Any suggestion to what can i use

Expected Output

Scenerio 1

Scenerio 2

Actual Output

I tried this code

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart=10dp"
        android:paddingEnd="10dp"
        android:paddingBottom="10dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toTopOf="@+id/inputContainer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/inputContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/crecyclerView">

        <EditText
            android:id="@+id/editTextContainer"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/editTextContainer"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

UPDATE

After adding @MartinMarconcini answer it fixed my Scenario 2. I want to fix as Scenario 1 as well.

解决方案

The idea is that your RV will handle its own item size and space by itself. You don't need to worry about this.

The Remainder of the space, will be used by ConstraintLayout.

So you need to pin the RV and the "Bottom Container" together. This would by default cause the CL to try to pull and balance layouts, so you have to tell the bottom container to be biased at the bottom.

In pseudo-XML:

<ConstraintLayout>
   <RecyclerView 
      width=0dp
      height=0dp
      start/end = parent
      TopToTop=parent
      BottomToTopOf=BottomContainer
   > 
   <BottomContainer>
      width=0dp
      height=wrap_content 
      TopToBottomOf=RecyclerView
      BottomToBottomOf=parent
      start/end = parent>
</ConstraintLayout>

Now this needs one more thing, because the RV would take all the space, you want to ensure the BottomContainer has more information about where it wants to be placed.

So you add:

app:layout_constraintVertical_bias="1.0"

to the Bottom Container.

This means, vertically, as "bottom" as you can. (0.0 would be the opposite).

The end result is:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recylerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:paddingBottom="10dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@android:layout/simple_list_item_2" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        app:layout_constraintVertical_bias="1.0"
        android:layout_height="wrap_content"
        android:id="@+id/bottomContainer"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/recylerview"
        app:layout_constraintBottom_toBottomOf="parent">

        <EditText
            android:id="@+id/editTextContainer"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/editTextContainer"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Which looks like this:

UPDATE

If you add both

  app:stackFromEnd="true"
  app:reverseLayout="true"

It works the same way, except the item "0" is at the bottom, and the "last" item is at the top. Adding another item would add it at the top but the BottomContainer has no participation (or influence) over this:

这篇关于Constraintlayout Vertical Bias 在两个视图中不起作用 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆