如何在全屏上显示对话框片段?

问题描述 投票:0回答:1

我有一个自定义DialogFragment,它仅显示一半的高度。宽度很好。我在布局中设置height =“ match_parent”。我的根元素是RelativeLayout。我不知道如何解决高度问题。

这里是java类。

public class DefeatDialog extends DialogFragment {


public interface DialogListener {
    void onDialogPositiveClick(DialogFragment dialog);

}

public DefeatDialog() {
}

DialogListener mListener;

DefeatAlertBinding binding;

@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
    try {
        // Instantiate the NoticeDialogListener so we can send events to the host
        mListener = (DialogListener) context;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(context.toString()
                + " must implement NoticeDialogListener");
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    LayoutInflater inflater = getActivity().getLayoutInflater();

    View view = inflater.inflate(R.layout.defeat_alert, null, false);
    builder.setView(view);
    binding = DefeatAlertBinding.bind(view);
    binding.startAgain.setOnClickListener((v -> {
        mListener.onDialogPositiveClick(DefeatDialog.this);
    }));


    final AlertDialog dialog = builder.create();

    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    dialog.show();
    return dialog;
}

}

我该如何解决?

android android-dialogfragment
1个回答
0
投票

如果您确定XML的根目录是match_parent到match_parent,这应该可以解决您的问题,请用以下内容替换onCreateDialog:>

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    final Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    return dialog;
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.defeat_alert, container, false);
    builder.setView(view);
    binding = DefeatAlertBinding.bind(view);
    binding.startAgain.setOnClickListener((v -> {
        mListener.onDialogPositiveClick(DefeatDialog.this);
    }));
    return v;
}
© www.soinside.com 2019 - 2024. All rights reserved.