当您想要对齐时,如何最好地使用约束layout,但不确定儿童小部件的大小

发布于 2025-01-24 06:31:43 字数 2652 浏览 0 评论 0原文

我正在尝试创建此布局:

”带有两个TextView和两个EditText儿童的约束Layout”

我的标准是:

  • 我希望标签相对于彼此右键,但在父母内(少量)。
  • 我希望EditText项目填充
  • 在基线处对齐的剩余空间文本,

因为知道EditText小部件比TextView小部件高,并且“密码”比“登录”更宽,我能够提出此布局:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/login"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="8dp"
            android:autofillHints="login"
            android:hint="@string/user_name_hint"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/textView"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="48dp"
            android:text="@string/login"
            app:layout_constraintBaseline_toBaselineOf="@+id/login"
            app:layout_constraintStart_toStartOf="parent" />

        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="16dp"
            android:ems="10"
            android:hint="@string/password_hint"
            android:inputType="textPassword"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/login"
            app:layout_constraintTop_toBottomOf="@+id/login"
            android:autofillHints="password" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:text="@string/password"
            app:layout_constraintBaseline_toBaselineOf="@+id/password"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

但是我意识到这取决于无效的假设。实现这种布局的“正确”方法是什么?

(我意识到,使用Gridlayout这将是几乎很难的,但是我正在尝试学习使用ConstraintLayout。

I'm trying to create this layout:

ConstraintLayout with two TextView and two EditText children

My criteria are:

  • I want the labels right-justified relative to each other, but otherwise flush left (with margin) within the parent.
  • I want the EditText items to fill the remaining space
  • Text aligned at the baseline

Knowing that the EditText widgets are taller than the TextView widgets, and that "Password" is wider than "Login", I was able to come up with this layout:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/login"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="8dp"
            android:autofillHints="login"
            android:hint="@string/user_name_hint"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/textView"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="48dp"
            android:text="@string/login"
            app:layout_constraintBaseline_toBaselineOf="@+id/login"
            app:layout_constraintStart_toStartOf="parent" />

        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="16dp"
            android:ems="10"
            android:hint="@string/password_hint"
            android:inputType="textPassword"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/login"
            app:layout_constraintTop_toBottomOf="@+id/login"
            android:autofillHints="password" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:text="@string/password"
            app:layout_constraintBaseline_toBaselineOf="@+id/password"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

But I realize that this depends on invalid assumptions. What would be the "right" way to achieve this layout?

(I realize that this would have been near-trivial using a GridLayout, but I'm trying to learn to use ConstraintLayout.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只想待在家 2025-01-31 06:31:43

尝试以下布局。我在两个 textviews的末端创建了一个障碍,并将其末端限制在障碍物上。开始受到父级的限制。偏差设置为1.0,以向右移动。这应该为您提供正确的文字。 (您可能需要以后的 ConstraintLayout 才能使用;我使用了2.1.3版)

deDittexts 的布局很简单。

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/loginLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="@string/login"
        app:layout_constraintBaseline_toBaselineOf="@+id/login"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/passwordLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="@string/password"
        app:layout_constraintBaseline_toBaselineOf="@+id/password"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="end"
        app:constraint_referenced_ids="loginLabel,passwordLabel" />

    <EditText
        android:id="@+id/login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="8dp"
        android:autofillHints="login"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/barrier"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:autofillHints="password"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/barrier"
        app:layout_constraintTop_toBottomOf="@+id/login" />

</androidx.constraintlayout.widget.ConstraintLayout>

可能有一种更简单的方法来完成所有这些操作,但是目前我并没有发生它。

Try the following layout. I created a barrier toward the end of the two TextViews and constrained their ends to the barrier. The starts are constrained to the parent start. The biases are set to 1.0 to move both to the right. This should give you the right-justified text. (You may need a later version of ConstraintLayout for this to work; I used version 2.1.3)

The layout for the EditTexts is straightforward.

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/loginLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="@string/login"
        app:layout_constraintBaseline_toBaselineOf="@+id/login"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/passwordLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="@string/password"
        app:layout_constraintBaseline_toBaselineOf="@+id/password"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="end"
        app:constraint_referenced_ids="loginLabel,passwordLabel" />

    <EditText
        android:id="@+id/login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="8dp"
        android:autofillHints="login"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/barrier"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:autofillHints="password"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/barrier"
        app:layout_constraintTop_toBottomOf="@+id/login" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

There may be an easier way to do all this, but it is not occurring to me at this time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文