在ConstraintLayout中自动调整限制

时间:2018-09-05 23:58:39

标签: android android-layout android-constraintlayout

我有一个ConstraintLayout形式,例如:

enter image description here

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

enter image description here

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

Full name gets bigger and it's still on screen

但是,如果“密码”更改其大小,它将退出屏幕:

Password gets bigger and gets out of screen

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

这种情况对我来说尤其重要,避免对几种语言进行不同的布局,以免“全名”和“密码”的翻译可能会改变其大小并使文本不显示在屏幕上。

因此,如何避免“全名”和“密码”的文字出现变化,并且EditText扩展到其右边缘(如图所示)?

2 个答案:

答案 0 :(得分:3)

这是ConstraintLayout的有趣测试。以下布局可在1.1.2和1.1.3版本下使用,但未在其他版本中进行测试。

关键是将全名和密码字段的结尾彼此限制,并将它们的开头约束为父对象的开头。将水平偏差设置为1.0,以使两个字段右对齐。

我想知道循环引用(结束<->结束)是否可以成功解析,并且确实可以。我现在想知道这种解决方案是否可以正常工作,但是在将来的版本中可能会失效。

enter image description here

enter image description here

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>

答案 1 :(得分:1)

E:\neo4jDatabases\database-6c03e895-692f-4f45-90e4-dc29e0f5bfeb\ installation-3.4.1\data\databases\graph2.db TableLayout更好,在您的情况下,布局可能是这样的:

ConstraintLayout

当您更改两个<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> 之一时,它们将再次自动对齐。

相关问题