ConstraintLayout中如何将TextView放在ImageView右侧不重叠且占满剩余宽度
ConstraintLayout 布局重叠修复方案
以下为核心修改点,修改后即可实现ImageView固定左侧、TextView占满剩余宽度的效果:
- 移除ImageView中错误的反向约束:
app:layout_constraintEnd_toEndOf="@id/effective_warnings_card_text",该约束会将图片右边界和文字右边界绑定,导致逻辑冲突 - 调整TextView的宽度与约束配置:
- 将
android:layout_width="match_parent"改为android:layout_width="0dp",0dp是ConstraintLayout中专用于占满约束范围内空间的配置,等同于match_constraint - 给TextView新增约束
app:layout_constraintEnd_toEndOf="parent",将其右边界绑定到父布局右边界
- 将
修正后完整代码
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/margin_default" android:layout_marginTop="12dp" android:layout_marginEnd="@dimen/margin_default" android:layout_marginBottom="12dp"> <ImageView android:id="@+id/effective_warnings_card_icon" android:layout_width="48dp" android:layout_height="0dp" android:focusable="false" android:importantForAccessibility="no" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/account_unavailable_image" tools:src="@tools:sample/avatars" /> <TextView android:id="@+id/effective_warnings_card_text" style="@style/TextAppearance.TSBApp20.Body.Regular" android:layout_width="0dp" android:layout_height="wrap_content" android:background="#000000" android:text="aggfgffh" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@+id/effective_warnings_card_icon" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
内容的提问来源于stack exchange,提问作者Pritish
相关产品推荐
相关产品推荐

