如何在 ConstraintLayout 中的图像之间的两行中对齐 TextView
How to align TextView in two lines between images in ConstraintLayout
我在 constraintlayout 中做一些设计,这让我很痛苦。我在左边有一张图片,两个垂直方向的文本在此图片的末尾对齐,我在右边有其他约束,带有图片和文本。
如果我在垂直方向的两个文本中键入一个长文本,该文本将覆盖我在视图右侧的约束。我想做的是如果这个文本很长不能覆盖这个约束,必须扩展到其他行。
这是我的代码:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_slider"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_error" />
<TextView
android:id="@+id/txt_title_animation_native_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textColor="@color/app_primary_medium"
android:textSize="15sp"
app:layout_constraintStart_toEndOf="@+id/image_slider"
app:layout_constraintTop_toTopOf="@+id/image_slider"
tools:text="Title" />
<TextView
android:id="@+id/txt_subtitle_animation_native_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:maxLines="2"
android:textColor="@color/app_grey_medium"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="@+id/txt_title_animation_native_cart"
app:layout_constraintTop_toBottomOf="@+id/txt_title_animation_native_cart"
tools:text="Subtitle asfda sdfowei oiwue rpoweiru oiwue twoeiu t oiweu t" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint_undo_button"
android:layout_width="31dp"
android:layout_height="match_parent"
android:layout_marginEnd="24dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_undo"
android:layout_width="14dp"
android:layout_height="16dp"
android:src="@drawable/icon_loop"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/icon_loop"
tools:visibility="visible" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center"
android:text="Ree"
android:textColor="@color/app_primary_medium"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image_undo" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
首先,你不需要两个嵌套约束布局,一个就够了。
然后只需使用 endToStartoOf image_undo 设置文本并将宽度设置为 0dp 而不是 wrap_content
只需这样做:
<TextView
android:id="@+id/txt_subtitle_animation_native_cart"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:maxLines="2"
android:textColor="@color/app_grey_medium"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="@+id/txt_title_animation_native_cart"
app:layout_constraintTop_toBottomOf="@+id/txt_title_animation_native_cart"
app:layout_constraintEnd_toStartOf="@+id/constraint_undo_button"
tools:text="Subtitle asfda sdfowei oiwue rpoweiru oiwue twoeiu t oiweu t" />
我建议您只使用一个父视图并阅读指南视图、约束链和障碍。所以一个可能的解决方案可能是下一个:
代码:
<?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"
android:padding="16dp">
<!-- In the parent constraint group add your custom background -->
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_80_percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.85" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_slider"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_title_animation_native_cart"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="@id/guideline_80_percent"
app:layout_constraintStart_toEndOf="@id/image_slider"
app:layout_constraintTop_toTopOf="@id/image_slider"
tools:text="Title" />
<TextView
android:id="@+id/txt_subtitle_animation_native_cart"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:maxLines="2"
app:layout_constraintBottom_toBottomOf="@id/image_slider"
app:layout_constraintEnd_toEndOf="@id/txt_title_animation_native_cart"
app:layout_constraintStart_toStartOf="@id/txt_title_animation_native_cart"
app:layout_constraintTop_toBottomOf="@id/txt_title_animation_native_cart"
tools:text="Subtitle asfda sdfowei oiwue rpoweiru oiwue twoeiu t oiweu t adfadf adflkjadfkj adlkjadflk" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_undo"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toTopOf="@id/text_undo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/guideline_80_percent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/text_undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/image_undo"
app:layout_constraintStart_toStartOf="@id/image_undo"
app:layout_constraintTop_toBottomOf="@id/image_undo"
tools:text="Ree" />
</androidx.constraintlayout.widget.ConstraintLayout>
我在 constraintlayout 中做一些设计,这让我很痛苦。我在左边有一张图片,两个垂直方向的文本在此图片的末尾对齐,我在右边有其他约束,带有图片和文本。
如果我在垂直方向的两个文本中键入一个长文本,该文本将覆盖我在视图右侧的约束。我想做的是如果这个文本很长不能覆盖这个约束,必须扩展到其他行。
这是我的代码:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_slider"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_error" />
<TextView
android:id="@+id/txt_title_animation_native_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textColor="@color/app_primary_medium"
android:textSize="15sp"
app:layout_constraintStart_toEndOf="@+id/image_slider"
app:layout_constraintTop_toTopOf="@+id/image_slider"
tools:text="Title" />
<TextView
android:id="@+id/txt_subtitle_animation_native_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:maxLines="2"
android:textColor="@color/app_grey_medium"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="@+id/txt_title_animation_native_cart"
app:layout_constraintTop_toBottomOf="@+id/txt_title_animation_native_cart"
tools:text="Subtitle asfda sdfowei oiwue rpoweiru oiwue twoeiu t oiweu t" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint_undo_button"
android:layout_width="31dp"
android:layout_height="match_parent"
android:layout_marginEnd="24dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_undo"
android:layout_width="14dp"
android:layout_height="16dp"
android:src="@drawable/icon_loop"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/icon_loop"
tools:visibility="visible" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center"
android:text="Ree"
android:textColor="@color/app_primary_medium"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image_undo" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
首先,你不需要两个嵌套约束布局,一个就够了。 然后只需使用 endToStartoOf image_undo 设置文本并将宽度设置为 0dp 而不是 wrap_content
只需这样做:
<TextView
android:id="@+id/txt_subtitle_animation_native_cart"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:maxLines="2"
android:textColor="@color/app_grey_medium"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="@+id/txt_title_animation_native_cart"
app:layout_constraintTop_toBottomOf="@+id/txt_title_animation_native_cart"
app:layout_constraintEnd_toStartOf="@+id/constraint_undo_button"
tools:text="Subtitle asfda sdfowei oiwue rpoweiru oiwue twoeiu t oiweu t" />
我建议您只使用一个父视图并阅读指南视图、约束链和障碍。所以一个可能的解决方案可能是下一个:
代码:
<?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"
android:padding="16dp">
<!-- In the parent constraint group add your custom background -->
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_80_percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.85" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_slider"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_title_animation_native_cart"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="@id/guideline_80_percent"
app:layout_constraintStart_toEndOf="@id/image_slider"
app:layout_constraintTop_toTopOf="@id/image_slider"
tools:text="Title" />
<TextView
android:id="@+id/txt_subtitle_animation_native_cart"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:maxLines="2"
app:layout_constraintBottom_toBottomOf="@id/image_slider"
app:layout_constraintEnd_toEndOf="@id/txt_title_animation_native_cart"
app:layout_constraintStart_toStartOf="@id/txt_title_animation_native_cart"
app:layout_constraintTop_toBottomOf="@id/txt_title_animation_native_cart"
tools:text="Subtitle asfda sdfowei oiwue rpoweiru oiwue twoeiu t oiweu t adfadf adflkjadfkj adlkjadflk" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_undo"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toTopOf="@id/text_undo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/guideline_80_percent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/text_undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/image_undo"
app:layout_constraintStart_toStartOf="@id/image_undo"
app:layout_constraintTop_toBottomOf="@id/image_undo"
tools:text="Ree" />
</androidx.constraintlayout.widget.ConstraintLayout>