使用ConstraintLayout作为DialogFragment的根布局时的奇怪行为

Orb*_*bit 9 android android-dialog android-dialogfragment android-constraintlayout

我正在构建一个使用a的自定义对话框DialogFragment.我注意到非常奇怪的行为,各种ViewGroup用作对话框布局的根.我认为这是由于系统窗口之间的一些奇怪的交互以及它如何显示对话框.在这个特定的实例中,我使用a ConstraintLayout作为布局的根视图.

显示时,对话框将延伸到屏幕边缘,Layout Inspector显示测量宽度超过16,000,000.甚至更奇怪的是ConstraintLayout定义填充,仍然可以在屏幕上看到.

下面是对话框的类:

public class AgreementDialog extends DialogFragment {

    // Bindings..

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.dialog_agreement, container);
        ButterKnife.bind(this, view);

        return view;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是布局,dialog_agreement:

<android.support.constraint.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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:padding="@dimen/margin_large"
    android:layout_margin="@dimen/margin_xlarge"
    >

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />


    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin_standard"
        android:text="this is some text. "
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/checkbox"
        />

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin_large"
        android:text="123456789"
        app:layout_constraintBottom_toTopOf="@+id/negative"
        app:layout_constraintTop_toBottomOf="@id/description"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />

    <Button
        android:id="@+id/positive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/confirm"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="24dp"/>

    <Button
        android:id="@+id/negative"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:text="@string/cancel"
        app:layout_constraintBaseline_toBaselineOf="@id/positive"
        app:layout_constraintEnd_toStartOf="@id/positive"
        />


</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 为什么测得的宽度如此之大?
  2. 鉴于测量的宽度超过16,000,000,人们可以预期末端填充也会在屏幕外.它为什么可见?
  3. 如何解决这些问题,以便能够显示包含其内容的正常对话框?

编辑:我注意到删除填充似乎导致宽度达到那个大数字.保持填充会导致对话框保持屏幕边缘的正常边距,但会剪切内容.

qtm*_*fld 2

我使用您的布局创建了一个示例应用程序dialog_agreement.xml,它工作正常:

在此输入图像描述 在此输入图像描述

1.为什么测量的宽度这么大?

布局dialog_agreement.xml指的是维度资源

  • @dimen/margin_standard
  • @dimen/margin_large
  • @dimen/margin_xlarge

如果它们的值不适中,对话就会很奇怪。

2. 考虑到测量的宽度超过 16,000,000,人们会期望末端填充也会超出屏幕。为什么它是可见的?

根据指南,每个子视图都会对其大小做出一个愿望。但父视图最初会考虑它自己的填充。因此,对话框将在屏幕尺寸内,并且还保留其填充。

视图的大小用宽度和高度来表示。视图实际上拥有两对宽度和高度值。

第一对称为测量宽度测量高度。这些尺寸定义了视图在其父视图中的大小。...

第二对简称为宽度和高度,或者有时称为绘图宽度绘图高度。这些尺寸定义了绘图时和布局后屏幕上视图的实际大小。这些值可以但不一定与测量的宽度和高度不同。...

为了测量其尺寸,视图会考虑其填充。...

3. 如何解决这些问题,以便可以显示包含其内容的正常对话框?

在 中设置适中的值res/values/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="margin_standard">16dp</dimen>
    <dimen name="margin_large">16dp</dimen>
    <dimen name="margin_xlarge">16dp</dimen>
</resources>
Run Code Online (Sandbox Code Playgroud)

示例应用程序AgreementDialog如上面的屏幕截图所示。