【问题标题】:Align a set of views to the right of the largest view of those appearing in the right part of the screen将一组视图与出现在屏幕右侧的最大视图的右侧对齐
【发布时间】:2020-02-21 17:36:17
【问题描述】:

我可以使用ConstraintLayout 创建一个视图,根据最大的子视图对齐子视图吗?
示例:

考虑到打印的右侧Text 在其中一行中可能更大,我想让进度条与右侧最大的Text 对齐。
那可能吗?我可以用嵌套的LinearLayout 做到这一点,但想知道ConstraintLayout 是否解决了这个问题

【问题讨论】:

  • 是的,您可以通过使用约束布局或 Relativelaout 来实现这一点。在 Relativelayout 中,将文本对齐到 allignparenEnd,将进度条对齐到文本的开头
  • @Raza:但在这种情况下,每个进度条都将与其右侧的文本对齐,而我希望进度条彼此从上到下对齐与较大尺寸的文本对齐
  • 您是否使用要显示的列表项。或者这是包含这些项目的静态视图。?
  • @Raza:静态视图

标签: android android-linearlayout android-view android-constraintlayout


【解决方案1】:

创建一个Barrier,方向为start,并引用右侧TextViews的所有ID。将每个进度条的end 约束到BarrierBarrier 将与最长 TextView 的开头对齐。

如何实现这一点的示例 XML:

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

    <androidx.constraintlayout.widget.Barrier
            android:id="@+id/barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="start"
            app:constraint_referenced_ids="text1,text2"/>

    <TextView
            android:id="@+id/progress1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ProgressBar"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toStartOf="@id/barrier" />

    <TextView
            android:id="@+id/progress2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ProgressBar"
            app:layout_constraintTop_toBottomOf="@id/progress1"
            app:layout_constraintEnd_toStartOf="@id/barrier" />

    <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

    <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Longer Text"
            app:layout_constraintTop_toBottomOf="@id/text1"
            app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

结果:

为防止左侧TextView 与其他Views 重叠,您需要将其end 限制为进度条,将水平偏移设置为0,使其与其start 约束对齐并设置@当宽度设置为wrap_content 时,将强制执行 987654336@ 对其的约束。它应该是这样的:

<TextView
        android:id="@+id/foo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Foo"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/progress1"
        app:layout_constraintTop_toTopOf="parent" />

当文本到达进度条时,它将导致文本换行到下一行。如果您不希望文本换行,可以添加省略号或将 TextView 限制为最多 1 行。

或者,您可以将左侧的TextView's 宽度设置为0,这样它将占用进度条左侧的所有可用空间。

【讨论】:

  • 这似乎可行,但我现在遇到的问题是,如果左侧的 Foo 文本太长,它会与进度条重叠。我想我需要某种方法来阻止 textview 重叠
  • 如果文本太长,它确实会换行,但在预览中,换行文本的顶部会超出父级 ConstraintLayout 并看起来被切断
  • 你设置top约束了吗?
  • 我已设置顶部和底部与右侧进度条的顶部和底部对齐,使其居中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多