自定义全屏Dialog

需求:自定义dialog使其沾满整个屏幕,透明显示(活动弹窗)。

先上代码

布局文件

 




    

        

        

java代码

package com.cfiigroup.zhehui.hepaidai.ui.dialog;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;


public class PromptDialog extends DialogFragment implements View.OnClickListener {

    private Button mBtnUse;
    private ImageView mIvDismss;
    private RelativeLayout mRlActivity;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().setCanceledOnTouchOutside(false);

        setCancelable(true);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_dialog_bg)));
        View v = inflater.inflate(R.layout.dialog_customview, null);

        initView(v);
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    private void initView(View v) {
        mBtnUse = (Button) v.findViewById(R.id.btn_use);
        mIvDismss = (ImageView) v.findViewById(R.id.iv_dismss);
        mRlActivity = (RelativeLayout) v.findViewById(R.id.rl_activity);
        mBtnUse.setOnClickListener(this);
        mIvDismss.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_use:
                UiManager.callWebActivity(getActivity(), UiManager.webflag_tiyanjin);
                dismiss();
                break;
            case R.id.iv_dismss:
                dismiss();
                break;
        }
    }
    }

分析:定义全屏dailog window去除title 设置主题使其全屏

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

getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

设置背景透明度

 getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_dialog_bg)));

你可能感兴趣的:(自定义全屏Dialog)