基本上,我有一个带有很长字符串的按钮,这会导致换行。该按钮的左右边距被多行按钮文本忽略,因为该按钮占据了父级的全部宽度。
[一个明显的解决方案是将布局宽度设置为0dp,但是这会强制使用静态宽度,这意味着对于较短的buttontext,该按钮不再被包装。
是否有一个好的解决方案,如何保留wrap_content的宽度,但仍不忽略边距?
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:text="@string/longstring"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在按钮xml中添加以下行,以便按钮遵守边距
app:layout_constrainedWidth="true"
最简单的解决方案是,必须在按钮的字符串中使用/ n,以使按钮文本不扩展例如“这是一个字符串,然后/ n将在下一行”
通过这种方式,我认为您的按钮将不会忽略边距并在其中显示文本
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:text="@string/longstring"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constrainedWidth="true" />
</androidx.constraintlayout.widget.ConstraintLayout>