【问题标题】:Chain with a barrier in ConstraintLayout在 ConstraintLayout 中带有障碍的链
【发布时间】:2019-09-06 00:27:04
【问题描述】:

我想使用无嵌套的 ConstraintLayout 来实现下面的布局。

布局需要一个屏障,因为不能保证哪个 textview 会更高。它还需要一条链,因为一切都需要居中。问题是我找不到让链与障碍一起工作的方法。

这是我目前的布局,但不会居中:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/topLeftText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        tools:text="Test test test test test test test test test test"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/topRightText"/>

    <TextView
        android:id="@+id/topRightText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        tools:text="Test test test test test test test test test test test test test test test test test test test test"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@+id/topLeftText"
        app:layout_constraintEnd_toEndOf="parent"/>

    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="topLeftText,topRightText"
        app:barrierDirection="bottom"/>

    <TextView
        android:id="@+id/bottomText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        tools:text="Test test test test test test test test test test test test test test test test test test test test"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/barrier"/>

</android.support.constraint.ConstraintLayout>

如果有人知道如何实现这一点,将不胜感激!

【问题讨论】:

  • 您是否尝试将左上角文本视图的顶部约束到右上角文本视图的顶部。然后将右上角和底部文本视图约束到屏障
  • 您的问题解决了吗,如果解决了怎么办?我目前面临类似的问题,否则我能想到的唯一解决方案是使用嵌套布局。这有点与使用 ConstraintLayout 相矛盾,保持层次结构平坦。
  • 您找到解决方法了吗?
  • 您面临的问题是您无法将所有内容垂直居中吗?如果是这种情况,也许您可​​以尝试使用水平引导线而不是障碍?
  • @TreyWurm 我使用了嵌套布局。由于这个问题仍然没有答案,我认为如果不更改 ConstraintLayout,这可能是不可能实现的。

标签: android android-constraintlayout


【解决方案1】:

我知道它很旧,但我认为要在没有嵌套的情况下这样做,您必须将顶部组件更改为类似的东西。

android:layout_height="wrap_content" android:layout_gravity="center"

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

【讨论】:

  • 问题是关于在ConstraintLayout中居中TextViews。您的答案是将 ConstraintLayout 置于其父级的中心。那就是嵌套,因为你必须添加这样的父级。
猜你喜欢
  • 2020-03-26
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多