2

我想调整我的 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>
4

1 回答 1

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;
}
于 2013-08-02T02:33:34.043 回答