android - ConstraintLayout 中多个水平障碍的问题

标签 android android-constraintlayout

我正在用头撞 ConstraintLayout 的障碍(抱歉双关语......)

这是我想要实现的目标:

  • 将标签和编辑文本放置在“第一行”中。将它们的中心垂直对齐。添加一个水平屏障,引用它们下面的两个 UI 元素。
  • 在第二行添加两个按钮,一个左对齐,一个右对齐。在这两个之下再设置一个障碍。
  • 让 RecyclerView 填充第二个障碍下方的所有空间。

这是我对布局文件的尝试:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_marginBottom="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/editText"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/top_barrier"
        app:layout_constraintVertical_bias=".5"
        />

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintStart_toEndOf="@id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/top_barrier"
        app:layout_constraintVertical_bias=".5"
        />

    <android.support.constraint.Barrier
        android:id="@+id/top_barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="textView,editText" />

    <!-- Try to cut from here..... -->

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintTop_toBottomOf="@+id/top_barrier"
        app:srcCompat="@android:drawable/btn_star" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/top_barrier"
        />

    <android.support.constraint.Barrier
        android:id="@+id/lowerBarrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="imageButton,button" />

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"

        android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="@+id/lowerBarrier"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />

    <!-- ..... to here. -->

</android.support.constraint.ConstraintLayout>

问题是,一旦我在第一个障碍下方添加一些内容,第一个障碍就会向上移动。它似乎移动到了较小 UI 元素的底部,这对我来说似乎有点荒谬。

注释之间没有元素的布局如下所示: Barrier seems to be working according to my expectations

当我在注释之间添加元素时,它停止工作: enter image description here

如果您能解释我做错了什么,我会非常高兴。

干杯

编辑:

有趣的是,我可以通过以相反的顺序定义第二行中的按钮来更进一步。 但是在这两个障碍物下面添加第二个障碍物再次破坏了布局......

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_marginBottom="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/editText"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/top_barrier"
        app:layout_constraintVertical_bias=".5"
        />

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintStart_toEndOf="@id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/top_barrier"
        app:layout_constraintVertical_bias=".5"
        />

    <android.support.constraint.Barrier
        android:id="@+id/top_barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="textView,editText" />


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/top_barrier"
        />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintTop_toBottomOf="@+id/top_barrier"
        app:srcCompat="@android:drawable/btn_star" />

</android.support.constraint.ConstraintLayout>

最佳答案

恕我直言,我没有发现您的代码有任何问题。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name:"
        app:layout_constraintBaseline_toBaselineOf="@+id/editText"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView"
        tools:text="John Smith" />

    <android.support.constraint.Barrier
        android:id="@+id/top_barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="textView,editText" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_barrier" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_barrier"
        app:srcCompat="@android:drawable/btn_star" />

    <android.support.constraint.Barrier
        android:id="@+id/lowerBarrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="button,imageButton" />

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/lowerBarrier" />

    <!-- ..... to here. -->

</android.support.constraint.ConstraintLayout>

预览:

enter image description here

我仅更改了 constraintVertical_biasconstraintBaseline_toBaselineOf

关于android - ConstraintLayout 中多个水平障碍的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52605393/

相关文章:

android - 如何保护 Assets 文件夹中的数据库 sqlite(通过加密)?

android - 使用 CastCompanionLibrary 简单显示图像的示例

android - 如果指纹不起作用,如何设置回退方法

Crosswalk 中的 Android 构建错误

java - 将新的 Key 添加到 HashMap 的最后一个索引而不是第一个索引

Android 不均匀设计

android - ConstraintLayout 中元素的叠加

android - 当我更改元素的 id 时,通过 ConstraintSet 以编程方式添加障碍不起作用?

android - 约束布局 - 将 Viewpager 放置在另一个 View 之上

Android 将宽度设置为 ConstraintLayout.LayoutParams.MATCH_PARENT 给出 -1