如何使BottomSheetDialogFragment覆盖全屏?

ano*_*ous 5 android fragment bottom-sheet

我一直BottomSheetDialogFragment在显示一些数据,但是当我开始显示时,fragment它会出现在屏幕的50%左右。所以,我的问题是如何在显示时使其全屏显示。

BottomSheetDialogFragment 码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.bot_frag, container, false);
    TextView tv = v.findViewById(R.id.textVi);
    back=v.findViewById(R.id.back_of_bot);
    back.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            }
    );
    return v;
}
Run Code Online (Sandbox Code Playgroud)

Nil*_*iya 2

您可以使用对话框片段,请参考:

public class DialogFragments extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_dialogfragment_layout, null);
        getDialog().setTitle("Title");
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        getDialog().getWindow().setGravity(Gravity.BOTTOM);
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, (int) (metrics.heightPixels * 0.30));// here i have fragment height 30% of window's height you can set it as per your requirement
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimationUpDown;

}
Run Code Online (Sandbox Code Playgroud)

当你想打开时,打开 Bottomsheet 对话框,如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.bot_frag, container, false);
    TextView tv = v.findViewById(R.id.textVi);
    back=v.findViewById(R.id.back_of_bot);
    back.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   FragmentManager fm = getFragmentManager();
                   DialogFragments dialogFragment = new DialogFragments(this);
                   dialogFragment.show(fm, "Bottomsheet Fragment");
                }
            }
    );
    return v;
}
Run Code Online (Sandbox Code Playgroud)