我正在创建一个对话框,其中两个按钮相对于父ConstraintLayout正确对齐。
一切都很好,直到按钮的文字变得很长。当其中一个或两个按钮的文本很长时,按钮会超出父级的边界,从而导致文本被剪切,如下图所示。我想处理文本较长的情况。
即。期望的行为是
当按钮文字很短时,布局按预期工作:
当按钮文字很长时:
布局代码
<?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"
无效答案 0 :(得分:6)
以下是我认为你想要完成的两个屏幕截图。
首先,有一些简短的文字:
现在有一些长篇文章:
我对布局采取了一些自由,并引入了按钮约束宽度的33%的指南。你没有指定按钮可以水平扩展多少,所以我做了这个假设。
此布局的XML如下。我将按钮的宽度设置为0dp
或match_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" />