Jac*_*ang 2 android constraints android-layout android-constraintlayout
我有一个TextView向左对齐并垂直居中。两组ImageView和TextView均右对齐,有两种布局配置,一组右图,一组左图。
hello world 文本可能很长。我希望它扩展以填充整个宽度而不覆盖左侧文本。
我添加app:layout_constraintStart_toEndOf="@id/text1"了约束右侧组(图像+文本)不与左侧文本重叠。但是,它的行为并不像我预期的那样。
如何仅使用 ConstraintLayout 使其不重叠?
Text1如果正确的文本太长,则会被掩盖,这是意料之中的。请注意,第二行的图像也不见了。

代码:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Text1" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/row1_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toStartOf="@id/row1_image"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toEndOf="@id/text1"
app:layout_constraintTop_toTopOf="parent"
tools:text="Hello World" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/row1_image"
android:layout_width="22dp"
android:layout_height="22dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/row1_text"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_confirm" />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/horizontal_barrier"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="row1_text,row1_image" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/row2_image"
android:layout_width="22dp"
android:layout_height="22dp"
app:layout_constraintEnd_toStartOf="@id/row2_text"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toEndOf="@id/text1"
app:layout_constraintTop_toTopOf="@id/horizontal_barrier"
tools:src="@drawable/ic_confirm" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/row2_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/row2_image"
app:layout_constraintTop_toTopOf="@id/horizontal_barrier"
tools:text="Hello World" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
小智 9
请检查 Constraintlayout 版本。使用app:layout_constrainedWidth="true"
与android:layout_width="wrap_content"
| 归档时间: |
|
| 查看次数: |
917 次 |
| 最近记录: |