【问题标题】:Weird behavior when using ConstraintLayout as the root layout of a DialogFragment使用 ConstraintLayout 作为 DialogFragment 的根布局时的奇怪行为
【发布时间】:2018-04-20 21:46:06
【问题描述】:

我正在使用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. 如何解决这些问题,以便显示包含其内容的普通对话框?

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

【问题讨论】:

  • 你能添加一些屏幕截图吗?预期与实际将是一个很好的开始
  • 一些额外的信息会有所帮助。标题提到了CoordinatorLayout,但该视图组并未出现在您的代码中。它从何而来。显示此行为的小型演示项目也会有所帮助,否则复制可能会很困难。您在什么设备/API 级别/模拟器上看到此行为。
  • @Cheticamp 标题已修复。
  • 您使用的是最新版本的 ConstraintLayout (1.1.0) 吗? 1.0 版的一些测量问题已得到修复。

标签: android android-dialogfragment android-dialog android-constraintlayout


【解决方案1】:

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

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

布局dialog_agreement.xml指的是dimension resources

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

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

2。考虑到测量的宽度超过 16,000,000,人们会期望末端填充也会在屏幕外。为什么可以看到?

根据the guide,每个子视图都希望自己的大小。但是父视图最初会考虑自己的填充。因此,对话框将在屏幕大小范围内,并保留其填充。

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

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

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

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

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,如上面的屏幕截图所示。

【讨论】:

  • 希望以上内容能满意回答您的问题。
【解决方案2】:

尝试使用 MaterialDialog 作为自定义对话框,其中包含您的片段。这不是您问题的正确答案,但它会大大简化您的工作。

https://github.com/afollestad/material-dialogs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 2022-11-07
    • 2018-04-19
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多