Android、dialog_fragment。 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)

Android, dialog_fragment. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)

我正在使用对话框片段,一切正常,直到我尝试删除标题栏。有一个代码:

对话框XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#f0f0f0">

<GridView
    android:id="@+id/gv_registration_avatars"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_margin="@dimen/avatar_layout_margin"
    android:columnWidth="100dp"
    android:drawSelectorOnTop="true"
    android:gravity="center"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:focusable="true"
    android:clickable="true"
    />
<Button
    android:id="@+id/dismiss"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/avatar_layout_margin"
    android:layout_marginRight="@dimen/avatar_layout_margin"
    android:layout_below="@id/gv_registration_avatars"
    android:text="Ok"
    android:layout_gravity="right"/>

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar_avatars"
    android:layout_centerInParent="true"
    android:visibility="gone"/>

DialogFragment class 是:

public class AvatarPickDialogFragment extends DialogFragment {

private GridView mGridView;
private ProgressBar mProgressBar;
private GridViewAvatarAdapter mGridAdapter;
private ArrayList<String> mGridData;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.avatar_pick_dialog_fragment, container, false);
    mGridView = (GridView) rootView.findViewById(R.id.gv_registration_avatars);
    mProgressBar = (ProgressBar) rootView.findViewById(R.id.progressBar_avatars);
    mGridData = new ArrayList<>();
    mGridAdapter = new GridViewAvatarAdapter(getActivity(), R.layout.grid_item_avatar_layout, mGridData);
    mGridView.setAdapter(mGridAdapter);
    mProgressBar.setVisibility(View.VISIBLE);
    String item;
    for (int i = 0; i < 10; i++) {
        item = new String();
        item= new String("string");

        mGridData.add(item);

    }
    mProgressBar.setVisibility(View.GONE);
    mGridAdapter.setGridData(mGridData);

    Button dismiss = (Button) rootView.findViewById(R.id.dismiss);
    dismiss.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
    return rootView;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // The only reason you might override this method when using onCreateView() is
    // to modify any dialog characteristics. For example, the dialog includes a
    // title by default, but your custom layout might not need it. So here you can
    // remove the dialog title, but you must call the superclass to get the Dialog.
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    return dialog;
}
}

当我使用 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) 时,标题栏消失了,但是我的对话框 window 宽度变得很短,我不知道为什么,因为有标题栏没事。有什么建议吗?

您可以尝试在片段的 onStart() 方法中设置片段宽度

@Override
public void onStart()
{
  super.onStart();

  if (getDialog() == null)
    return;

    getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

}