如何制作自定义警报对话框全屏

How to make a custom alertdialog fullscreen

我希望自定义 AlertDialog 全屏,就像新的 activity 屏幕一样。

尝试了这些答案,但仍然无效

public void newDialog(final Context c){

    final AlertDialog alertDialog;

    LayoutInflater layoutInflater = LayoutInflater.from(c);
    dialogueView = layoutInflater.inflate(R.layout.layout_dialogue, null);

    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(c);
    alertDialogBuilder.setView(dialogueView);

    alertDialog = alertDialogBuilder.create();

    dialogueView.findViewById((R.id.closeBtn)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialog.dismiss();
        }
    });

    alertDialog.show();
}

layout_dialogue.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/full_screen_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/frame">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/closeBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background_sky"
    android:clickable="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/ic_delete" />

</android.support.constraint.ConstraintLayout>

style.xml

<style name="full_screen_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">true</item>
</style>

我也试过

android.R.style.Theme_Black_NoTitleBar_Fullscreen  

ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;

None 他们中的一些人为我工作。关于如何完成它有什么建议吗?

你可以使用自定义样式让我给你举个例子

style.xml

<style name="DialogTheme" parent="android:Theme.Dialog">

    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>
    <!-- No backgrounds, titles or window float -->
    <item name="android:windowBackground">@color/colorPrimary</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

Activity.java

AlertDialog.Builder builder = new AlertDialog.Builder(this , R.style.DialogTheme)

您可以使用以下方法。 (它在 Kotlin 中定义,但您也可以为 Java 实现)

class FullscreenDialog: DialogFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialogStyle)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_no_internet, container, false)
        return view
    }

    override fun onStart() {
        super.onStart()

        if(dialog != null) {
            val width = ViewGroup.LayoutParams.MATCH_PARENT
            val height = ViewGroup.LayoutParams.MATCH_PARENT
            dialog.window?.setLayout(width, height)
        }
    }

}

将以下内容添加到您的 styles.xml

<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="colorPrimaryDark">@color/appPrimaryColor</item>
    <item name="colorPrimary">@color/appPrimaryColor</item>

    <!-- Set this to true if you want Full Screen without status bar -->
    <item name="android:windowFullscreen">true</item>

    <item name="android:windowIsFloating">false</item>

    <!-- This is important! Don't forget to set window background -->
    <item name="android:windowBackground">@null</item>

</style>   

要显示对话框,请使用以下内容:

val fullScreenDialog = FullscreenDialog()
fullScreenDialog.isCancelable = false
fullScreenDialog.show(supportFragmentManager, "MyTag")

我一直在使用的无边距全屏对话框

public class ShareImageEditorDialog extends DialogFragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setStyle(DialogFragment.STYLE_NORMAL,
                android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_share_image, container);

        return v;
    }
}

并在点击按钮时显示

new ShareImageEditorDialog().show(((FragmentActivity)activity).getSupportFragmentManager(), "MyTag");