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

9
我正在使用DialogFragment构建自定义对话框。我注意到,使用各种ViewGroups作为对话框布局的根时会出现非常奇怪的行为。我认为这是由于系统窗口与显示对话框的方式之间的某种奇怪交互所致。在这个特定的实例中,我使用ConstraintLayout作为布局的根视图。
当显示对话框时,它会延伸到屏幕的边缘,并且布局检查器显示测量宽度超过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;
    }
}

这里是布局文件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>

问题:

  1. 为什么测量的宽度如此之大?
  2. 给定测量的宽度超过16,000,000,人们会期望结束填充也会超出屏幕。为什么它是可见的?
  3. 如何解决这些问题,以便可以显示正常包裹其内容的对话框?

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


3
你能添加一些屏幕截图吗?将预期与实际结果进行比较将是一个很好的开始。 - Sagar
1
一些额外的信息会很有帮助。标题提到了“CoordinatorLayout”,但是在您的代码中并没有出现这个视图组。它在哪里呢?一个展示这种行为的小型演示项目也会很有帮助,否则可能会很难复制。您在哪个设备/ API 级别/ 模拟器上看到了这种行为? - Cheticamp
@Cheticamp 标题已经修复。 - Orbit
你是否正在使用最新版本的ConstraintLayout(1.1.0)? 1.0版本存在一些测量问题,这些问题已经得到修复。 - Rapunzel Van Winkle
2个回答

2

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

enter image description here enter image description here

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>

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

我希望以上回答能够令您满意地解决问题。 - qtmfld

-4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接