当您想要对齐时,如何最好地使用约束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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下布局。我在两个 textviews的末端创建了一个障碍,并将其末端限制在障碍物上。开始受到父级的限制。偏差设置为
1.0
,以向右移动。这应该为您提供正确的文字。 (您可能需要以后的 ConstraintLayout 才能使用;我使用了2.1.3版)deDittexts 的布局很简单。
可能有一种更简单的方法来完成所有这些操作,但是目前我并没有发生它。
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.
There may be an easier way to do all this, but it is not occurring to me at this time.