我有 2 个按钮彼此相邻(链接),并且在应用程序中有 2 种可能的状态,或者只有左按钮可见,在这种情况下它应该水平居中,或者两者都可见,在这种情况下它们都应该水平居中。这一切都有效,但是按钮中有多个单词,并且在小屏幕上按钮都被剪裁而不是环绕。要修复此设置,将两个按钮的宽度设置为 0dp,但在这种情况下,按钮会变得尽可能宽,因此在较大的屏幕上或只有一个按钮可见时看起来不正确。我的问题是如何在使用 ConstraintLayout 时将单词包装在按钮中?我如何约束按钮,使它们正确包裹并且不会变得比它们需要的更宽?使用 LinearLayout 这一切都是开箱即用的,但我想使用 ConstraintLayout。我尝试在两个按钮上设置 app:layout_constrainedWidth="true"但这不起作用,它将只包装第一个按钮,如果第二个按钮有足够长的文本,它将不存在。
更新:我将示例中的按钮文本更新为更长。
XML:
<Button
android:id="@+id/button1"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Long text for first button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/objectAboveButtons"
app:layout_goneMarginEnd="0dp"
app:layout_goneMarginRight="0dp" />
<Button
android:id="@+id/button2"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:text="Long text for second button"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button1"
app:layout_constraintTop_toBottomOf="@+id/objectAboveButtons" />
最佳答案
在按钮中使用 app:layout_constraintWidth_default="wrap"
并将宽度设置为 0dp
而不是 wrap_content
,如下所示:
<?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:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintWidth_default="wrap"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Long text for first button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_weight="1"/>
<Button
android:id="@+id/button2"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintWidth_default="wrap"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:text="Long text for second button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/vertical_guideline_50_pc"
app:layout_constraintHorizontal_weight="1"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vertical_guideline_50_pc"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
当按钮包含较大的文本(如问题中提供的文本)时,我使用 Guideline
在按钮之间平均分配宽度。以编程方式将 Button2
的 app:layout_constraintStart_toEndOf
从 Guideline
更改为 Button1
,当其可见性处于 GONE< 时
状态:
if (button2.visibility == View.GONE) {
...
constraintSet.connect(R.id.button2, ConstraintSet.START, R.id.button1, ConstraintSet.END, 0);
}
没有按钮 2 的屏幕截图:
关于android - 与 ConstraintLayout 一起使用时,按钮文本不换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57452636/