我想創建一個AlertDialog
作爲DialogFragment
,它帶有標題,確定按鈕,取消按鈕和ExpandableListView
。問題是,ExpandableListView
需要儘可能多的空間,並按下按鈕和標題的對話框。我想要的是頂部的標題,底部的按鈕以及ExpandableListView
將所有剩餘空間全部放在屏幕上,以便DialogFragment
在展開時不會增加/減少尺寸,而是保留它滾動。控制AlertDialog中的視圖大小
這裏是描述情況的圖片,左邊是已初始化的DialogFragment
,第二張是展開ExpandableListView
的其中一個部分之後的圖片。沒有想到醜陋。
我想實現如下:
- 保持固定在
FragmentDialog
的大小,優選地對整個窗口(fill_parent
/match_parent
)。 - 保持按鈕固定在底部,標題固定在頂部,
ExpandableListView
固定(但仍可滾動)在中心。
我已經嘗試了很多種不同的東西,但這裏是我目前的需要。
定製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(recipe_filter_dialog.xml)的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()));
}
}
爲視圖提供了一個固定的佈局高度和寬度 – 3xplore