如何使BottomSheetDialogFragment覆盖整个屏幕?

时间:2017-12-01 07:13:50

标签: 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;
}

2 个答案:

答案 0 :(得分:0)

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

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;

}

当您想要打开时,请按照以下方式打开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;
}

答案 1 :(得分:0)

您必须将此款式应用于BottomSheetDialogFragment

android.R.style.Theme_Material_Light_NoActionBar_Fullscreen