Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 控制AlertDialog中的视图大小_Android_Android Dialogfragment - Fatal编程技术网

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">