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

时间:2020-04-05 16:28:57

标签: android android-dialogfragment

我有一个自定义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;
}

}

该如何解决?这是我的xml文件。这是DialogFragment的布局。它不会全屏显示。也不会显示xml的所有部分。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/line"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:background="@drawable/line_defeat"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.057"
            tools:layout_editor_absoluteX="0dp" />

        <ImageView
            android:id="@+id/image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/icon_defeat"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/line"
            app:layout_constraintVertical_bias="0.036" />

        <TextView
            android:id="@+id/textView_1"
            android:layout_width="400dp"
            android:layout_height="80dp"
            android:gravity="center"
            android:text="@string/defeat"
            android:textColor="@color/dark"
            android:textSize="30sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/image"
            app:layout_constraintVertical_bias="0.0" />

        <TextView
            android:id="@+id/textView_2"
            android:layout_width="400dp"
            android:layout_height="80dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.454"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_1"
            app:layout_constraintVertical_bias="0.131" />


        <Button
            android:id="@+id/start_again"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="@drawable/button_win_defeat"
            android:text="@string/button_defeat_win"
            android:textColor="@color/colorWhite"
            android:textSize="17sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_2"
            app:layout_constraintVertical_bias="0.024" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

这是我对DialogFragment的布局。它不会全屏显示。也不是全部显示xml。

1 个答案:

答案 0 :(得分:1)

如果您确定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);
    binding = DefeatAlertBinding.bind(view);
    binding.startAgain.setOnClickListener((v -> {
        mListener.onDialogPositiveClick(DefeatDialog.this);
    }));
    return v;
}

 @Override
public void onStart() {
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null) {
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
    }
}