在ConstraintLayout中右对齐视图而不剪切

时间:2018-04-11 18:33:36

标签: android android-layout android-constraintlayout constraint-layout-chains

我正在创建一个对话框,其中两个按钮相对于父ConstraintLayout正确对齐。

一切都很好,直到按钮的文字变得很长。当其中一个或两个按钮的文本很长时,按钮会超出父级的边界,从而导致文本被剪切,如下图所示。我想处理文本较长的情况。

即。期望的行为是

  • 按钮应该在文本很长时包装文本
  • 按钮应保持在父级的范围内并服从父级填充
  • 按钮应与父母保持对齐

当按钮文字很短时,布局按预期工作:

enter image description here

当按钮文字很长时:

  • 取消按钮文本很长时,将截断取消文本。发生这种情况是因为按钮本身被推过了父母的边界。 enter image description here
  • 当ok按钮的文本很长时,取消超出父边界的文本,因为按钮被推到父母的边界之外。 enter image description here

布局代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/dialog_padding"
    android:paddingLeft="@dimen/dialog_padding"
    android:paddingRight="@dimen/dialog_padding"
    android:paddingTop="@dimen/dialog_padding">

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dialog_text_margin"
        tools:text="Dialog title" />

    <TextView
        android:id="@+id/dialog_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dialog_text_margin"
        app:layout_constraintTop_toBottomOf="@id/dialog_title"
        tools:text="Dialog text content" />

    <Button
        android:id="@+id/cancel_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toStartOf="@id/ok_btn"
        app:layout_constraintTop_toBottomOf="@id/dialog_content"
        tools:text="Dismiss" />

    <Button
        android:id="@+id/ok_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/dialog_content"
        tools:text="Accept" />
</android.support.constraint.ConstraintLayout>

我尝试无用的事情

  • app:layout_constraintStart_toStartOf="parent"添加到取消按钮会导致按钮不再对齐,因此解决方案不正确
  • 将关闭按钮的结尾限制在接受按钮的开头,导致按钮不再对齐
  • 使用layout_width="0dp"按钮并使用app:layout_constrainedWidth="true"无效

1 个答案:

答案 0 :(得分:6)

以下是我认为你想要完成的两个屏幕截图。

首先,有一些简短的文字:

enter image description here

现在有一些长篇文章:

enter image description here

我对布局采取了一些自由,并引入了按钮约束宽度的33%的指南。你没有指定按钮可以水平扩展多少,所以我做了这个假设。

此布局的XML如下。我将按钮的宽度设置为0dpmatch_constraints,以便调整其大小。这些按钮也被放置在packed链中,将两个按钮组合在一起。水平偏差现在设置为0.5,但是如果它开始向你爬去,那么增加它会将组向右移动。

ConstraintLayout documentation对这些功能以及如何使用它们有一些很好的描述。

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.33" />

<TextView
    android:id="@+id/dialog_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/dialog_text_margin"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Dialog title" />

<Button
    android:id="@+id/cancel_btn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toStartOf="@+id/ok_btn"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintStart_toEndOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/dialog_title"
    tools:text="Dismiss" />

<Button
    android:id="@+id/ok_btn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/cancel_btn"
    app:layout_constraintTop_toTopOf="@+id/cancel_btn"
    tools:text="Accept" />