在AlertDialog

时间:2015-05-13 18:12:03

标签: android android-dialogfragment

我想创建一个AlertDialog作为DialogFragment,其中包含标题,确定按钮,取消按钮和ExpandableListView。问题是ExpandableListView占用了所需的空间,并将按钮和标题推出对话框。我想要的是顶部的标题,底部的按钮和ExpandableListView以占用剩余的空间,直至全屏,以便DialogFragment不会增加/减小尺寸扩展它时,而是保持它可滚动。

这是描述情况的图像,左边是初始化的DialogFragment,第二个是扩展ExpandableListView的其中一个部分。没关系丑陋。

ExpandableListView expands too much.

我想实现以下目标:

  1. 保持FragmentDialog的大小固定,最好是整个窗口(fill_parent / match_parent)。
  2. 将按钮固定在底部,标题固定在顶部,ExpandableListView固定(但仍可滚动)在中间。
  3. 我尝试了很多不同的东西,但这是我目前的选择。

    自定义DialogFragment

    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);
            }
        }
    }
    

    DialogFragment的xml文件(recipe_filter_dialog.xml)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <com.my.app.RecipeFilterExpandableListView
            android:id="@+id/recipe_filter_expandableListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        />
    
    </LinearLayout>
    

    自定义ExpandableListView

    public class RecipeFilterExpandableListView extends ExpandableListView {
    
        public RecipeFilterExpandableListView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            this.setOnGroupExpandListener(new RecipeFilterDialogOnGroupExpandListener(this));
    
            // This adapter just fills the ExpandableListView, nevermind it.
            this.setAdapter(new RecipeFilterExpandableListAdapter(context, ((MyActivity)context).getDbFilter()));
        }
    }
    

1 个答案:

答案 0 :(得分:1)

尝试设置builder.setTitle(R.string.recipe_filter_title);而不是builder.setMessage(R.string.recipe_filter_title);并确定Dialog所需的高度和宽度,并使用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">