Android - 自定义Dialog内部透明,外部有遮罩

图例:

Android - 自定义Dialog内部透明,外部有遮罩_第1张图片

代码

1、自定义dialog:引入样式和代码指定样式

package com.gxjl.pe.gxjlpesdk.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.gxjl.pe.gxjlpesdk.R;

/**
 * 相机、相册选择 弹框
 * Created by xiaoshuai on 2018/8/20.
 */

public abstract class CameraPhotoDialog extends Dialog implements View.OnClickListener{
    private Context context;

    public CameraPhotoDialog(@NonNull Context context) {
        super(context, R.style.dialogTransparent);//内容样式在这里引入

        this.context = context;
    }

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

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

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

        setContentView(R.layout.dialog_camrea_photo);

		//tv_title = findViewById(R.id.tv_title);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.9); // 宽度设置为屏幕宽度的80%
        //lp.dimAmount=0.0f;//外围遮罩透明度0.0f-1.0f
        dialogWindow.setAttributes(lp);
        dialogWindow.setGravity(Gravity.BOTTOM);//内围区域底部显示

    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.tv_cancel) {
            this.dismiss();
        }
    }

    protected abstract void confirm();
}

2、dialog_camrea_photo.xml 布局




    

        

            

            

            
        

        

            
        
    

3、style.xml 样式布局:指定dialog内容样式




    

 

你可能感兴趣的:(【,Android,工具封装,】)