【问题标题】:Auto adjust limits in ConstraintLayout在 ConstraintLayout 中自动调整限制
【发布时间】:2019-02-11 04:01:44
【问题描述】:

我有一个ConstraintLayout 的表格,如下所示:

“全名”和“密码”在其右边缘对齐,“全名”提供距ConstraintLayout 的边距:

到目前为止一切都很好,但我想知道如何正确处理需要以编程方式更改这些文本的情况。如果“全名”变大或变小,“密码”将保持对齐:

但是如果“密码”改变了它的大小,它就会离开屏幕:

我尝试使用Barrier 来获取“全名”和“密码”的左边缘,但我无法让它工作,我认为Barrier 只能与其他元素和不要将屏障的元素与边缘对齐(如果我错了,请纠正我)。

这个案例对我来说特别重要,以避免为“全名”和“密码”的翻译可能会改变它们的大小并导致文本超出屏幕的语言制作几种不同的布局。

那么,如果“全名”和“密码”的文本发生变化,并且如图所示,EditTexts 也扩展到其右边缘,我该如何避免它们离开屏幕?

【问题讨论】:

  • 查看here 以获取使用屏障的示例。您可以将障碍设置在最大标签的右侧,然后将 EditTexts 与障碍对齐。
  • 这个例子涵盖了右边缘...我的问题是左边缘
  • 应该还是可以的。我会试着举一个例子。
  • 看起来@Cheticamp 打败了我。这个答案是一个很好的解决方案。
  • 有效,但只能在代码上使用,Android Studio 会阻止循环引用

标签: android android-layout android-constraintlayout


【解决方案1】:

这是对ConstraintLayout 的有趣测试。以下布局适用于版本 1.1.2 和 1.1.3,但未在其他版本中测试。

关键是将全名和密码字段的结尾相互约束,并将它们的开头约束到父字段的开头。将水平偏移设置为1.0 以使两个字段右对齐。

我想知道循环引用(end end)是否会愉快地解决,并且确实如此。我现在想知道这个解决方案是否恰好可以工作,但可能会在未来的版本中中断。

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/fullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="50dp"
        android:text="Full Name"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toEndOf="@+id/password"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:text="Password is longer"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toEndOf="@id/fullName"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fullName" />

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="@id/fullName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/fullName"
        app:layout_constraintTop_toTopOf="@id/fullName" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="@id/password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/password"
        app:layout_constraintTop_toTopOf="@id/password" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是第二次使用循环引用。障碍放置在文本字段的右侧,并且这些相同字段的末端与障碍相关联。 1.0 的水平偏差右对齐文本。屏幕截图看起来和上面一样,所以我不会在这里复制它们。

我认为这种方法更可取,恕我直言,不太可能破坏。

activity_main2.xml

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/fullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="50dp"
        android:text="Full Name"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toEndOf="@+id/barrier"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:text="Password is longer"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fullName" />

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="@id/fullName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/barrier"
        app:layout_constraintTop_toTopOf="@id/fullName" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="@id/password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/barrier"
        app:layout_constraintTop_toTopOf="@id/password" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="right"
        app:constraint_referenced_ids="fullName,password" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 有趣,所以这与我所做的相反 + 循环引用。我试图在可视化编辑器中进行循环引用,但它阻止了我,使这项工作的唯一方法是在代码中强制引用,所以它可能是“谷歌不想要的”
  • @mFeinstein 添加了替代解决方案。
  • 我想我更喜欢这个......循环引用在哪里?它是否隐含在屏障中?
  • 是的,到障碍物。
  • 我会尽快测试它,看看 Android Studio Designer 是否允许我通过拖放来完成。如果是这样,我认为它在未来不会破裂
【解决方案2】:

TableLayoutConstraintLayout 在你的情况下更好,布局可能是这样的:

<TableLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:stretchColumns="1">
    <TableRow>
        <TextView android:gravity="end"
            android:text="Full Name"/>
        <EditText/>
    </TableRow>
    <TableRow>
        <TextView android:gravity="end"
            android:text="Password"/>
        <EditText/>
    </TableRow>
</TableLayout>

当您更改两个TextView 之一时,它们将再次自动对齐。

【讨论】:

  • 有趣,我将保持问题开放只是为了学习ConstrainLayout 的新技巧,以防万一
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
  • 1970-01-01
  • 2014-02-15
  • 2011-09-22
相关资源
最近更新 更多