android 简单快速 自定义dialog(简单好用,不需要设置样式)

1.MyDialog代码:

public class MyDialog{

    private Activity activity;
    private ViewGroup contentView;
    private View view;
    public MyDialog(Activity activity) {
        this.activity=activity;
        contentView = activity.findViewById(android.R.id.content);
        view=LayoutInflater.from(activity).inflate(R.layout.dialog_layout,null);
        view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        view.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 显示dialog(包含动画)
     */
    public void show(){
        Animation animation= AnimationUtils.loadAnimation(activity,R.anim.dialog_in_anim);
        view.setAnimation(animation);
        contentView.addView(view);
    }

    /**
     * 移除dialog(包含动画)
     */
    public void dismiss(){
        Animation animation= AnimationUtils.loadAnimation(activity,R.anim.dialog_out_anim);
        view.setAnimation(animation);
        contentView.removeView(view);
    }
}

2.res/anim 下新建 dialog_in_anim.xml 和 dialog_out_anim.xml

dialog_in_anim.xml 底部显示



    

dialog_out_anim.xml 底部隐藏



    

3.界面布局:dialog_layout.xml ,半透明颜色值:#80000000




    

        
        
        

        

            

            
        
    

4.使用:

new MyDialog(activity).show();

你可能感兴趣的:(android,自定义dialog)