如何设置DialogFragment的宽度和高度? [英] How to set DialogFragment's width and height?

查看:3389
本文介绍了如何设置DialogFragment的宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指定我在一个XML布局文件DialogFragment的布局(姑且称之为 layout_mydialogfragment.xml ),其 layout_width layout_height 属性特别是(以 100dp 每个比方说)。然后我吹在我的DialogFragment的 onCreateView(...)方法,此布局如下:

I specify the layout of my DialogFragment in an xml layout file (let's call it layout_mydialogfragment.xml), and its layout_width and layout_height attributes particularly (to be 100dp each let's say). I then inflate this layout in my DialogFragment's onCreateView(...) method as follows:

View view = inflater.inflate(R.layout.layout_mydialogfragment, container, false);

不幸的是,我发现我的对话框(DialogFragment)出现的时候,它不尊重 layout_width layout_height 在XML布局文件中指定的(和我对话的萎缩或扩展可变取决于内容)。任何人都知道我是否或如何才能让我的对话框尊重 layout_width layout_height 在XML布局文件中指定?目前,我有在我的DialogFragment的 onResume()方法再次指定我的对话框的宽度和高度如下...

Unfortunately, I find that when my dialog (DialogFragment) appears, it does not respect the layout_width and layout_height specified in its xml layout file (and my dialog shrinks or expands variably depending on content). Anyone know whether or how I can get my dialog to respect the layout_width and layout_height specified in its xml layout file? At the moment I'm having to specify the width and height of my dialog again in my DialogFragment's onResume() method as follows...

getDialog().getWindow().setLayout(width, height);

......就这样,不希望,一定要记住,使两地今后的任何更改对话框的宽度和高度。

... And thus, undesirably, have to remember to make any future changes to the dialog's width and height in two places.

推荐答案

如果您是直接从资源价值转换:

If you convert directly from resources values:

int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);        
getDialog().getWindow().setLayout(width, height);

然后在您的对话框布局指定match_parent:

Then specify match_parent in your layout for the dialog:

android:layout_width="match_parent"
android:layout_height="match_parent"

您只需要担心的一个地方(把它放在 DialogFragment#onResume )。它并不完美,但至少它适用于有一个RelativeLayout的为你的对话框的布局文件的根目录。

You only have to worry about one place (place it in your DialogFragment#onResume). Its not perfect, but at least it works for having a RelativeLayout as the root of your dialog's layout file.

这篇关于如何设置DialogFragment的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆