Android 控制AlertDialog中的视图大小
我想创建一个Android 控制AlertDialog中的视图大小,android,android-dialogfragment,Android,Android Dialogfragment,我想创建一个AlertDialog作为DialogFragment,带有标题、ok按钮、cancel按钮和ExpandableListView。问题是ExpandableListView占据了它喜欢的空间,并将按钮和标题从对话框中推出。我想要的是顶部的标题、底部的按钮和ExpandableListView占据所有剩余空间,直到全屏,这样对话框片段在展开时不会增加/减少大小,而是保持滚动 这是一个描述情况的图像,左边是初始化的对话框片段,第二个是展开可展开列表视图的一个部分后的图像。别怕丑陋 我
AlertDialog
作为DialogFragment
,带有标题、ok按钮、cancel按钮和ExpandableListView
。问题是ExpandableListView
占据了它喜欢的空间,并将按钮和标题从对话框中推出。我想要的是顶部的标题、底部的按钮和ExpandableListView
占据所有剩余空间,直到全屏,这样对话框片段
在展开时不会增加/减少大小,而是保持滚动
这是一个描述情况的图像,左边是初始化的对话框片段
,第二个是展开可展开列表视图
的一个部分后的图像。别怕丑陋
我希望实现以下目标:
片段对话框
的大小固定,最好是整个窗口(fill\u parent
/match\u parent
)ExpandableListView
固定在中间(但仍可滚动)public class RecipeFilterDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.recipe_filter_title);
builder.setPositiveButton(R.string.recipe_filter_button_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// TODO: Perform filtering, fill list and return.
}
});
builder.setNegativeButton(R.string.recipe_filter_button_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// TODO: Kill dialog and return.
}
});
builder.setView(getActivity().getLayoutInflater().inflate(R.layout.recipe_filter_dialog, null));
builder.setCancelable(true);
return builder.create();
}
@Override
public void onStart() {
super.onStart();
AlertDialog dialog = (AlertDialog) getDialog();
if (dialog != null)
{
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
//dialog.getWindow().setLayout(width, height);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
}
对话框片段的xml文件(recipe\u filter\u dialog.xml)
尝试设置
builder.setTitle(R.string.recipe\u filter\u title)代码>而不是builder.setMessage(R.string.recipe\u filter\u title)
并决定您想要的对话框的高度和宽度
并在onStart()
方法中进行设置
也可以尝试这样做:
对话框类中的构造函数:
public RecipeFilterDialogFragment () {
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.FullscreenStyle);
}
以及上面使用的此对话框的样式:
<style name="FullscreenStyle" parent="@android:style/Theme.Holo.Light">
<item name="android:windowIsFloating">false</item>
<item name="android:windowFullscreen">true</item>
</style>
假的
真的
还可以尝试将父样式用作对话框主题f.e:
<style name="FullscreenStyle" parent="@android:style/Theme.Holo.Light.Dialog">
为视图提供固定的布局高度和宽度样式似乎不会影响对话框的大小,但使用setTitle
确实改变了一些小事情(只是视觉上的)。您建议我如何在onStart()
中计算和设置对话框的高度?我真的找不到这样做的方法,我想对话框和其中的RecipeFilterExpandableListView
都需要这样做。哦,在xml文件中为RecipeFilterExpandableListView
设置android:layout\u height=“300dp”
确实提供了所需的行为,但没有真正的帮助,因为它是硬编码的。如果您真的想创建全屏对话框,只需根据设备屏幕大小计算宽度和高度。然后样式将影响对话框的大小。我希望这会有所帮助。
<style name="FullscreenStyle" parent="@android:style/Theme.Holo.Light">
<item name="android:windowIsFloating">false</item>
<item name="android:windowFullscreen">true</item>
</style>
<style name="FullscreenStyle" parent="@android:style/Theme.Holo.Light.Dialog">