我在一个xml布局文件中指定DialogFragment的布局(我们称它为layout_mydialogfragment.xml
),尤其是它的layout_width
和layout_height
属性(可以100dp
这么说)。然后,我在DialogFragment的onCreateView(...)
方法中将此布局充气,如下所示:
View view = inflater.inflate(R.layout.layout_mydialogfragment, container, false);
不幸的是,我发现对话框(DialogFragment)出现时,它不遵守XML布局文件中指定的layout_width
和layout_height
(并且对话框根据内容而变小或缩小)。任何人都知道我是否如何才能让我的对话,尊重layout_width
并layout_height
在其XML布局文件中指定?目前,我必须在DialogFragment的onResume()
方法中再次指定对话框的宽度和高度,如下所示...
getDialog().getWindow().setLayout(width, height);
...因此,不希望出现的情况是,必须记住在以后两个位置对对话框的宽度和高度进行任何更改。
如果直接从资源值转换:
int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);
getDialog().getWindow().setLayout(width, height);
然后在布局中为对话框指定match_parent:
android:layout_width="match_parent"
android:layout_height="match_parent"
您只需要担心一个地方(将其放置在中DialogFragment#onResume
)。它并不完美,但至少可以将RelativeLayout用作对话框布局文件的根。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句