内容简介:翻译自:https://stackoverflow.com/questions/49824478/weird-behavior-when-using-constraintlayout-as-the-root-layout-of-a-dialogfragmen
我正在使用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>
问题:
>为什么测得的宽度如此之大?
>鉴于测得的宽度超过16,000,000,人们可以预期末端填充也会偏离屏幕.它为什么可见?
>如何纠正这些问题,以便显示包装内容的正常对话框?
编辑:我注意到删除填充似乎导致宽度达到那个大数字.保持填充会导致对话框保持屏幕边缘的正常边距,但会剪切内容.
我用你的布局dialog_agreement.xml创建了一个示例应用程序,它运行正常:
1.为什么测得的宽度如此之大?
布局dialog_agreement.xml引用 dimension resources :
> @ dimen / margin_standard
> @ dimen / margin_large
> @ dimen / margin_xlarge
如果它们的值不适中,则会对话框进行处理.
2.如果测量的宽度超过16,000,000,则可以预期末端填充也将在屏幕外.它为什么可见?
根据 the guide ,每个儿童视图都希望其大小.但父视图最初考虑了自己的填充.因此,对话框将在屏幕大小内,并保留其填充.
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,如上面的屏幕截图所示.
翻译自:https://stackoverflow.com/questions/49824478/weird-behavior-when-using-constraintlayout-as-the-root-layout-of-a-dialogfragmen
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。