Android ConstraintLayout中TextView文字被Button遮挡如何提前自动换行
问题原因
你当前使用的wifi_text未设置右侧约束,且宽度设置为wrap_content,ConstraintLayout无法感知到右侧Button的存在,会让TextView优先铺满自身宽度到父布局右边缘才触发换行,所以文字会被右侧按钮遮挡。
解决方法
仅需要调整wifi_text的两个属性即可实现你要的效果:
- 将
android:layout_width属性值从wrap_content改为0dp(在ConstraintLayout中0dp代表匹配约束范围,会自动按照左右约束计算可用宽度) - 给
wifi_text新增右侧约束app:layout_constraintEnd_toStartOf="@id/wifi_settings_button",把TextView的右边界限制在Button的左边界位置,还可以按需添加android:layout_marginEnd属性预留文字和按钮之间的间距,避免太拥挤。
修改后的wifi_text代码示例:
<TextView android:id="@+id/wifi_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginLeft="32dp" android:layout_marginTop="32dp" android:layout_marginEnd="16dp" android:text="Wi-Fi: some longer text should be on two lines and it is not on two lines" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/wifi_settings_button" app:layout_constraintTop_toBottomOf="@id/status_text" />
修改后TextView的可用宽度就是「父布局左边界到Button左边界」的范围,文字超出这个范围就会自动换行,不会被按钮遮挡。
内容的提问来源于stack exchange,提问作者user2351471
相关产品推荐
相关产品推荐

