如何制作自定义alertdialog全屏

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

我希望像新的活动屏幕一样定制AlertDialog全屏。

尝试了这些答案,仍然无法正常工作

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;

他们都没有为我工作。有关如何完成它的任何建议?

android android-alertdialog android-dialog
2个回答
2
投票

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

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)

0
投票

您可以使用以下方法。 (它在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")
© www.soinside.com 2019 - 2024. All rights reserved.