Android里把Dialog设置为全屏的方法

                                         Android里把Dialog设置为全屏的方法

 有的时候我们需要把Dialog设置为全屏,于是我们想到了如下的办法:

//设置成全屏
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mDetailDialog.addContentView(detailView, p);

 实际情况是dialog根本不会全屏,我的实现步骤如下:

1、定义Dialog类

package com.nes.seilivetv.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.nes.seilivetv.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class EpgDetailInforDialog extends Dialog {
    private Context mContext;

    public EpgDetailInforDialog(@NonNull Context context) {
        super(context);
        View inflate;

        mContext = context;
        inflate = LayoutInflater.from(context).inflate(R.layout.epg_detail_info, null);
        setContentView(inflate);
    }

    public EpgDetailInforDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);

        View inflate;

        mContext = context;
        inflate = LayoutInflater.from(context).inflate(R.layout.epg_detail_info, null);
        setContentView(inflate);
    }

    protected EpgDetailInforDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void setDialogLocation() {
        Window win = this.getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.gravity = Gravity.END;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        win.setWindowAnimations(R.style.dialogWindowAnim);
        win.setAttributes(lp);
    }
}

2、创建全屏Dialog

EpgDetailInforDialog epgDetailInforDialog = new EpgDetailInforDialog(getContext(), R.style.dialog_sen5_full);
        epgDetailInforDialog.setDialogLocation();
        epgDetailInforDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                Log.d("wujiang", "call epgDetailInforDialog dismiss");
            }
        });

        epgDetailInforDialog.show();

注意: 一定要设置Dialog的theme,不然还真的不能全屏,它会是一个很大的Dialog。

3、在style.xml里增加如下的style

    

4、设置动画

我这里使用的是dialogWindowAnim,定义如下:

    

side_panel_in和side_panel_out子anim文件夹下。

//side_panel_in



    
    

 

//side_panel_out




    
    

                                                                                  THE         END

你可能感兴趣的:(android)