假设我在一个名为my_dialog_fragment.xml的xml布局文件中指定了我的DialogFragment的布局,并且我将其根视图的layout_width和layout_height值指定为固定值(例如100dp)。然后我在我的DialogFragment的onCreateView(…)方法中膨胀这个布局,如下所示:
View view = inflater.inflate(R.layout.my_dialog_fragment, container, false);
可悲的是,我发现当我的DialogFragment出现时,它不尊重layout_width和layout_height值在其xml布局文件中指定,而是根据其内容收缩或展开。有人知道我是否或如何得到我的DialogFragment尊重layout_width和layout_height值指定在其xml布局文件?目前,我必须在我的DialogFragment的onResume()方法中再次指定对话框的宽度和高度,如下所示:
getDialog().getWindow().setLayout(width, height);
这样做的问题是,我必须记住将来在两个地方对宽度和高度进行任何更改。
早期的一个解决方案几乎起作用了。我尝试了一些稍微不同的方法,结果对我有用。
(一定要看他的解决方案)
这就是他的解决办法。点击这里
除了builder.getContext(). gettheme (). applystyle (R.style。Theme_Window_NoMinWidth,真正的);
我把它改成
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get layout inflater
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
// Set layout by setting view that is returned from inflating the XML layout
builder.setView(layoutInflater.inflate(R.layout.dialog_window_layout, null));
AlertDialog dialog = builder.create();
dialog.getContext().setTheme(R.style.Theme_Window_NoMinWidth);
最后一行是真正的不同之处。