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

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

我正在使用 DialogFragment 构建自定义对话框。我注意到将各种 ViewGroup 用作对话框布局的根时会出现非常奇怪的行为。我认为这是由于系统窗口和它显示对话框的方式之间存在一些奇怪的交互。在此特定实例中,我使用 ConstraintLayout 作为布局的 Root View 。

显示时,对话框延伸到屏幕边缘,布局检查器显示测量宽度超过 16,000,000。更奇怪的是 ConstraintLayout 定义了 padding,它仍然可以在屏幕上看到。

下面是对话框的类:

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

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

最佳答案

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

enter image description here enter image description here

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

布局dialog_agreement.xml引用dimension resources :

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

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

2。考虑到超过 16,000,000 的测量宽度,人们会期望结束填充也离开屏幕。为什么可见?

根据 the guide ,每个 subview 都希望它的大小。但是父 View 最初考虑了它自己的填充。因此,对话框将在屏幕大小内,并保留其填充。

The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.

The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent. ...

The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. ...

To measure its dimensions, a view takes into account its padding. ...

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

关于android - 使用 ConstraintLayout 作为 DialogFragment 的根布局时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49824478/

相关文章:

android - AlertDialog 在某些设备上破坏了布局

java - Android - 使用带有 adRquest 的 admob 加载广告时遇到问题

android - DialogFragment 中的可点击超链接

android - 模拟谷歌播放应用程序中看到的水平图像列表

java - 自定义 DialogFragment 窗口

android - 无法使自定义 DialogFragment 在 Fragment 上透明

Android 使用 ArrayList 中的值创建 AlertDialog?

android - 强制用户在对话框中选择选项

android - Android LVL 可以使用 ANDROID_ID 的哪些替代品?

java - 如何在Android中使用HttpPost发送图像