简体   繁体   English

android中如何使AlertDialog全屏

[英]How to make AlertDialog full screen in android

I know this question has been asked before but I really can't get it, I want my alert dialog to take full screen but instead it just keeps showing my app in the background, I have tried many things before posting here, so please if you know how can this be done share your answer.我知道以前有人问过这个问题,但我真的不明白,我希望我的警报对话框全屏显示,但它只是一直在后台显示我的应用程序,我在这里发布之前尝试了很多事情,所以请如果你知道如何做到这一点分享你的答案。

Loading.java加载中。java

        class LoadingDialog {
    
       private Activity activity;
       private AlertDialog dialog;
    
    
          LoadingDialog(MainActivity myActivity){
             activity = myActivity;
    
    
        }
    
        void hello(){
    
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            LayoutInflater inflater = activity.getLayoutInflater();
            builder.setView(inflater.inflate(R.layout.custom_dialog, null));
            builder.setCancelable(false);
    
            dialog = builder.create();
            dialog.show();
    
    
        }
    void DismissDialog(){
        dialog.dismiss();
    }
    void dismiss() { dialog.dismiss(); }

}

custom_dialog.xml custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"

    >

    <!--<view-->
    <!-- android:layout_width="match_parent"-->
    <!-- android:layout_height="match_parent" >-->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/white"
        android:orientation="vertical">
        <com.airbnb.lottie.LottieAnimationView
            android:id="@+id/animationView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="100dp"
            android:padding="40dp"

            app:lottie_autoPlay="true"
            app:lottie_loop="true"
            app:lottie_rawRes="@raw/loading1" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:fontFamily="Arial"
            android:textAlignment="center"
            android:layout_marginRight="30dp"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="20dp"

            android:text="Loading..."
            android:textColor="#196bbf"
            android:textSize="18sp"
            android:textStyle="bold"
            />
    </LinearLayout>
</RelativeLayout>

How I call it in Main:我如何在 Main 中调用它:

final LoadingDialog loadingDialog = new LoadingDialog(MainActivity.this);

please can you tell me what am I missing?请你能告诉我我错过了什么吗? please help me in this.请帮助我。

To make Alert Dialog fit screen width and height but Actionbar is visible use below code:要使警报对话框适合屏幕宽度和高度,但操作栏可见,请使用以下代码:

    void hello(){

    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    LayoutInflater inflater = activity.getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.custom_dialog_, null));
    builder.setCancelable(false);
    dialog = builder.show();

    dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);

}

And if you want to hide the Actionbar also, then first put below code in your styles.xml file如果你还想隐藏操作栏,那么首先将下面的代码放在你的 styles.xml 文件中

    <style name="myFullscreenAlertDialogStyle" parent="Theme.AppCompat.Light.NoActionBar">      //no actionbar, but status bar exists
    <item name="android:windowFullscreen">true</item>                       //remove status bar
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>        //smooth animation
    <item name="colorAccent">@color/colorAccent</item>                      //change button text color
    </style>

And use this style as below:并使用这种风格如下:

    void hello(){

    AlertDialog.Builder builder = new AlertDialog.Builder(activity,R.style.myFullscreenAlertDialogStyle);
    LayoutInflater inflater = activity.getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.custom_dialog_, null));
    builder.setCancelable(false);
    dialog = builder.show();

    //dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);

   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM