自定义DialogFragment窗口

时间:2013-07-24 04:09:15

标签: java android android-dialogfragment

我想调整DiaglogFragment窗口的宽度和高度,但下面的代码没有给出对话框窗口的调整大小版本,而是修改了主活动视图。我也尝试修改xml但仍然失败。

我不确定如何在这个问题上使用getDialog(),仍然会遇到一些错误。 dialog_fragment.xml是我想要修改的布局,应该完全自定义宽度和高度。

public static class TestDialogFragment extends DialogFragment {
    DialogFragment dialogFragment;
public DialogFragment newInstace() {
     dialogFragment = new TestDialogFragment();
    Log.d("Dialog Create", "sdf1");
    return dialogFragment;
}



@SuppressLint("NewApi")
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
                                      R.style.PopupTheme);
    builder.setTitle("TestDialogFragment");
    builder.setView(getContentView());
    Dialog dialog = builder.create();
    Log.d("Dialog Create", "sdf2");
    return dialog;

}

private View getContentView() {
    LayoutInflater inflater = getActivity().getLayoutInflater();

    LayoutParams params = getActivity().getWindow().getAttributes();

    params.height = 200; //fixed height
    params.width = 200; //fixed width

    getActivity().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); 

    //getDialog().getWindow().setLayout(200, 200);

    Log.d("Dialog Create", "sdf3");
    return inflater.inflate(R.layout.dialog_fragment, null);

}
} 

dialog_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
android:layout_height="match_parent"
    android:minWidth="250dp"
    android:minHeight="250dp"
     >


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

这对我有用,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
android:layout_height="match_parent"
    android:minWidth="250dp"
    android:minHeight="250dp"
     >

我需要做的就是从onCreateView()

中删除此代码
private View getContentView() {
   View rowView = inflater.inflate(R.layout.dialog_fragment, null);   

    return rootView;
}