Android 与ConstraintLayout一起使用时,按钮文本不换行

Android 与ConstraintLayout一起使用时,按钮文本不换行,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,我有两个按钮彼此相连,在应用程序中有两种可能的状态,要么只有左侧按钮可见,在这种情况下它应该水平居中,要么两者都可见,在这种情况下它们都应该水平居中。这一切都是可行的,但是按钮中有多个单词,在小屏幕上,按钮都被剪裁而不是包装。要固定此设置,两个按钮上的宽度均为0dp,但在这种情况下,按钮将尽可能宽,因此在较大屏幕上或只有一个按钮可见时,它看起来不正确。我的问题是,在使用ConstraintLayout时,如何将单词包装在按钮中?如何约束按钮,使其正确包裹,并且不会变得比需要的更宽?使用Linea

我有两个按钮彼此相连,在应用程序中有两种可能的状态,要么只有左侧按钮可见,在这种情况下它应该水平居中,要么两者都可见,在这种情况下它们都应该水平居中。这一切都是可行的,但是按钮中有多个单词,在小屏幕上,按钮都被剪裁而不是包装。要固定此设置,两个按钮上的宽度均为0dp,但在这种情况下,按钮将尽可能宽,因此在较大屏幕上或只有一个按钮可见时,它看起来不正确。我的问题是,在使用ConstraintLayout时,如何将单词包装在按钮中?如何约束按钮,使其正确包裹,并且不会变得比需要的更宽?使用LinearLayout,这一切都是现成的,但我想使用ConstraintLayout。我尝试在两个按钮上设置app:layout_constrainedWidth=true,但这不起作用,它将只包装第一个按钮,如果第二个按钮的文本足够长,它将包装它

更新:我将示例中的按钮文本更新为更长

XML:

在按钮中使用app:layout\u constraintWidth\u default=wrap,并将宽度设置为0dp,而不是像这样的wrap\u内容:

<?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>
问题中提供的较长文本的屏幕截图,两个按钮环绕:

按钮2消失的屏幕截图:

在按钮中使用app:layout\u constraintWidth\u default=wrap,并将宽度设置为0dp,而不是像这样的wrap\u内容:

<?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>
问题中提供的较长文本的屏幕截图,两个按钮环绕:

按钮2消失的屏幕截图:


尽管SaadAAkash的答案是正确的,但至少在androidx constraint layout 1.1.3中,layout_constraintWidth_default=wrap已被弃用,正确的方法如下:

用布局宽度=换行内容替换布局宽度=0dp 用layout\u constrainedWidth\u default=wrap替换layout\u constrainedWidth=true
它应该会产生相同的结果。

尽管SaadAAkash的答案是正确的,但至少在androidx constraint layout 1.1.3中,layout_constraintWidth_default=wrap被弃用,正确的方法如下:

用布局宽度=换行内容替换布局宽度=0dp 用layout\u constrainedWidth\u default=wrap替换layout\u constrainedWidth=true
它应该会产生相同的结果。

很难说是什么错了,因为我试图复制此代码,它的工作原理与您需要的一样,只是约束条件几乎没有变化。我对文本进行了匿名化,可能button1的文本有点短,如果您将其延长一点,它会左右剪切,至少在我的三星galaxy s4上是这样。所以有必要让文本进入第二行等等?我查了一些答案。您可以通过编程方式设置文本,包括\n使宽度正确。是的,例如,如果布局宽度为0dp,则文字将在需要时自动换行,并对两个按钮进行相同的换行,使它们具有相同的宽度,但这种方法的问题是,按钮将占用所有可用空间,而不是只按需要的宽度。我希望按钮能按需要扩展,但不能超出需要。我不确定是否以编程方式设置\n,因为如果不需要,它不应该换行。如果您可以指定所需的宽度,然后将其应用于按钮文本,它将起作用。因为唯一被改变的是文本,并且只有当getWidth更大时它才能适应。我知道,这不是一个优雅的解决方案。但是如果你尝试一下:很难说是什么错了,因为我试图复制这段代码,它的工作原理和你需要的一样,只是约束条件没有什么变化。我匿名化了文本,可能button1的文本有点短,如果你再长一点,它会左右剪切,至少在我的三星galaxy s4上是这样。所以有必要让文本进入第二行等等?我查了一些答案。您可以通过编程方式设置文本,包括\n使宽度正确。是的,例如,如果布局宽度为0dp,则文字将在需要时自动换行,并对两个按钮进行相同的换行,使它们具有相同的宽度,但这种方法的问题是,按钮将占用所有可用空间,而不是只按需要的宽度。我希望按钮能按需要扩展,但不能超出需要。我不确定是否以编程方式设置\n,因为如果不需要,它不应该换行。如果您可以指定所需的宽度,然后将其应用于按钮文本,它将起作用。因为唯一被改变的是文本,并且只有当getWidth更大时它才能适应。我知道,这不是一个优雅的解决方案。但如果你尝试一下:这几乎奏效了,所以我会批准它。我还必须添加一个水平偏差约束,并将按钮移动到
当两个按钮都显示时,通过将按钮1的偏移量设置为1.0,将按钮2的偏移量设置为0.0,使它们在更大的屏幕或横向视图中居中,但是当只有第一个按钮显示时,偏移量必须为0.5,否则将填充整个宽度。我也不认为按钮需要重量。谢谢。我需要添加app:layout\u constraintWidth\u default=wrap。我无法想象为什么这个框架不能自动处理这个问题。谢谢你的回答!这几乎奏效了,所以我会批准它。我还必须添加一个水平偏差约束,当按钮1的偏差为1.0,按钮2的偏差为0.0时,我必须将按钮向中间移动,以便它们在较大的屏幕或横向中居中,但是当只有第一个按钮显示时,偏差必须为0.5,否则它将填充整个宽度。我也不认为按钮需要重量。谢谢。我需要添加app:layout\u constraintWidth\u default=wrap。我无法想象为什么这个框架不能自动处理这个问题。谢谢你的回答!
if (button2.visibility == View.GONE) {
    ...
     constraintSet.connect(R.id.button2, ConstraintSet.START, R.id.button1, ConstraintSet.END, 0);
}