GuideLine不在DialogFragment中工作

时间:2017-07-07 08:32:45

标签: android android-dialogfragment android-constraintlayout

我想创建一个这样的dialogFragment:

enter image description here

其实我手机上有这个:

enter image description here

button1被button2覆盖。它看起来像guideLine不起作用。 如果textView足够长,它运行良好。但如果textView很短,问题就会发生。我该如何解决这个问题。

我的对话框xml是:

    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="32dp">

        <TextView
            android:id="@+id/dialog_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Dialog Message"
            android:textAppearance="?android:attr/textAppearanceMedium"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <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.5" />

        <Button
            android:id="@+id/dialog_button_positive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="32dp"
            android:layout_marginRight="32dp"
            android:layout_marginTop="16dp"
            android:gravity="center"
            android:text="Positive"
            android:textAppearance="?android:textAppearanceButton"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/guideline"
            app:layout_constraintTop_toBottomOf="@id/dialog_message" />

       <Button
            android:id="@+id/dialog_button_negative"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="32dp"
            android:layout_marginRight="32dp"
            android:layout_marginTop="16dp"
            android:gravity="center"
            android:text="Negative"
            android:textAppearance="?android:textAppearanceButton"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@id/guideline"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/dialog_message" />

    </android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

指南中存在对话框或任何非全屏屏幕的错误。我有一个在移动设备上全屏显示的活动,但是在平板电脑上却显示了一个对话框,而依赖于Guideline的视图就消失了。

yao liu之前所述,在对话框片段上使用getDialog().getWindow().setLayout()或仅在getWindow().setLayout(...)上使用onStart()即可显示该视图。

答案 1 :(得分:-1)

您不需要该指南,您可以使用Chains代替。 试试这个

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="32dp">

    <TextView
        android:id="@+id/dialog_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dialog Message"
        android:textAppearance="?android:attr/textAppearanceMedium"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/dialog_button_positive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:gravity="center"
        android:text="Positive"
        app:layout_constraintHorizontal_chainStyle="packed"
        android:textAppearance="?android:textAppearanceButton"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/dialog_button_negative"
        app:layout_constraintTop_toBottomOf="@id/dialog_message" />

    <Button
        android:id="@+id/dialog_button_negative"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginStart="16dp"
        android:gravity="center"
        android:text="Negative"
        android:textAppearance="?android:textAppearanceButton"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/dialog_button_positive"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/dialog_message" />

</android.support.constraint.ConstraintLayout>

如果您不熟悉链,请参阅small tutorial