Android 傻瓜式用上高仿iOS对话框Dialog

如题,当你觉得系统给的对话框太丑了,想自己写时发现完全不知道怎么写,问度娘,又看得一头雾水,那今天就教你傻瓜式的用别人写好的吧,怎么实现的我不管,只要能用就行了

先上图,你觉得OK就用,不Ok就pass:


两个按钮
一个按钮

第一步:

新建文件:
如图所示在drawable上新建7个文件

新建7个resource文件,文件名,这个很重要,分别叫:
第一个:background_shape.xml
第一个代码:



    
    
    


第二个:left_button_shape.xml
第二个代码:




    
    


第三个:left_button_shape_depend_resource.xml  
第三个代码:




    
    
    


第四个:right_button_shape.xml
第四个代码:





    
    


第五个: right_button_shape_depend_resource.xml
第五个代码:




    
    
    


第六个:single_button_shape.xml
第六个代码:




    
    


第七个:single_button_shape_depend_resource.xml
第七个代码:




    

    


接下来----------------------------------------------------------------------------------------------
还要创建最后一个xml文件,在values下面创建,如图:


创建一个value resource

名称还是要注意,叫:custom_style.xml
其代码如下 :



    

别问我可不可以起其他名,如果你是大佬随意,是小白还是老老实实,可以避免很对问题,听话不挨打

第二步:

新建一个Java class,如图:


新建一个类名称为:MyDialog

新建一个类,类名为:MyDialog,将一下代码复制:


import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;

import android.os.Bundle;

import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * 使用说明:要想改对话框圆角的大小去background_shape.xml里面改,
 * 同时还要到left_button_shape_depend_resource.xml和right_button_shape_depend_resource.xml
 * 里面去改,要想改背景颜色也是一样的
 *
 * 本类提供方法setTitleSize()改标题字号和setMessageSize()改内容字号
 * setLeft() setRight() setTop() setBottom()四个方法改内容上下左右边距
 *     setTitleHeight()  setBottomHeight()分别设置标题和底部按钮的高度
 *     其他的想改字体颜色的到代码里面去看就行了 改标题颜色定位到131行代码
 *     内容142 按钮158和171,自行修改,方法就懒得提供了
 * 本类一共加载了四次资源,一次是主题,一次是背景,两次是按钮,左一个,右一个
 */
public class MyDialog  extends Dialog{

    private String title = "提示";
    private String message = "这是一个弹窗";
    private Context context;
    private boolean isThereNavigateButton = false;
    private boolean isTherePositiveButton = false;
    private float titleSize = 22;
    private float messageSize = 18;
    private int left = 75;
    private int right = 75;
    private int top = 0;
    private int bottom = 80;
    private int titleHeight = 150;
    private int bottomHeight = 150;

    private TextView titleTextView;
    private TextView messageTextView;
    private Button navigateButton;
    private Button positiveButton;
    private LinearLayout linearLayout;
    private ImageView imageView1;
    private ImageView imageView2;
    private LinearLayout linearLayout1;


    public MyDialog(Context context) {
        super(context, R.style.CustomDialog);//加载资源
        this.context = context;
        float scale = context.getResources().getDisplayMetrics().density;
        left = (int) (25*scale);//设置dp值
        right = (int) (25*scale);//设置dp值
        createAllView();
        setLayout();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(linearLayout);

        //设置按钮有无
        if (!isTherePositiveButton){
            positiveButton.setVisibility(View.GONE);
            imageView2.setVisibility(View.GONE);
            navigateButton.setBackgroundResource(R.drawable.single_button_shape);
        }
        if (!isThereNavigateButton){
            navigateButton.setVisibility(View.GONE);
            imageView2.setVisibility(View.GONE);
            positiveButton.setBackgroundResource(R.drawable.single_button_shape);
        }
        if (!isThereNavigateButton && !isTherePositiveButton){
            imageView1.setVisibility(View.GONE);
            linearLayout1.setVisibility(View.GONE);
        }

    }

    public MyDialog setNavigateButton(String content, final View.OnClickListener listener) {
        navigateButton.setText(content);
        navigateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dismiss();
            }
        });
        isThereNavigateButton = true;
        return this;
    }


    public MyDialog setPositiveButton(String content, final View.OnClickListener listener) {
        positiveButton.setText(content);
        positiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dismiss();
            }
        });
        isTherePositiveButton = true;
        return this;
    }

    //show方法
    public void show(){
        super.show();
    }

    //创建控件并设置控件的参数
    private void createAllView(){

        linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setBackgroundResource(R.drawable.background_shape);//加载资源

        titleTextView = new TextView(context);
        titleTextView.setGravity(Gravity.CENTER);
        titleTextView.setText("警告");
        titleTextView.setPadding(0,50,0,0);
        titleTextView.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        titleTextView.setHeight(titleHeight);//设置标题的宽度
        titleTextView.setTextColor(Color.BLACK);
        titleTextView.setTextSize(titleSize);//设置标题字体大小
        titleTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//设置字体样式

        messageTextView = new TextView(context);
        messageTextView.setGravity(Gravity.CENTER);
        messageTextView.setTypeface(Typeface.SANS_SERIF);
        messageTextView.setText("这是内容");
        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        messageTextView.setLayoutParams(params3);
        messageTextView.setTextColor(Color.BLACK);
        messageTextView.setTextSize(messageSize);//设置内容的字体大小
        messageTextView.setPadding(left, top, right, bottom);//设置内容与四周的间距


        imageView1 = new ImageView(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 4);
        imageView1.setLayoutParams(params);
        imageView1.setBackgroundColor(0xffb1b2b2);//设置背景颜色

        linearLayout1 = new LinearLayout(context);
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, bottomHeight);
        linearLayout1.setLayoutParams(params1);
        linearLayout1.setOrientation(LinearLayout.HORIZONTAL);

        positiveButton = new Button(context);
        positiveButton.setTextColor(0xff3478f6);//设置字体颜色
        positiveButton.setText("确定");
        LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
        positiveButton.setLayoutParams(params4);
        positiveButton.setTextSize(20);//设置确定的字体大小
        positiveButton.setBackgroundResource(R.drawable.left_button_shape);//加载资源

        imageView2 = new ImageView(context);
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(4, LinearLayout.LayoutParams.MATCH_PARENT);
        imageView2.setLayoutParams(params2);
        imageView2.setBackgroundColor(0xffb1b2b2);

        navigateButton = new Button(context);
        navigateButton.setTextColor(0xff3478f6);
        navigateButton.setText("取消");
        LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
        navigateButton.setLayoutParams(params5);
        navigateButton.setTextSize(20);//设置取消的字体大小
        navigateButton.setBackgroundResource(R.drawable.right_button_shape);//加载资源

    }


    //添加布局
    private void setLayout(){

        linearLayout.addView(titleTextView);
        linearLayout.addView(messageTextView);
        linearLayout.addView(imageView1);
        linearLayout.addView(linearLayout1);

        linearLayout1.addView(positiveButton);
        linearLayout1.addView(imageView2);
        linearLayout1.addView(navigateButton);

    }


    public String getTitle() {
        return title;
    }

    //设置标题
    public MyDialog setTitle(String title) {
        this.title = title;
        if (titleTextView == null){
            System.out.println("null");
        }else titleTextView.setText(title);
        return this;
    }

    public String getMessage() {
        return message;
    }

    //设置内容
    public MyDialog setMessage(String message) {
        this.message = message;
        messageTextView.setText(message);
        return this;
    }

    //设置标题大小
    public void setTitleSize(float titleSize) {
        this.titleSize = titleSize;
        titleTextView.setTextSize(titleSize);
    }

    //设置内容大小
    public void setMessageSize(float messageSize) {
        this.messageSize = messageSize;
        messageTextView.setTextSize(messageSize);
    }

    //设置内容左间距
    public void setLeft(int left) {
        this.left = left;
        messageTextView.setPadding(left, top, right, bottom);//设置内容与四周的间距
    }

    //设置内容右间距
    public void setRight(int right) {
        this.right = right;
        messageTextView.setPadding(left, top, right, bottom);//设置内容与四周的间距
    }

    //设置内容上间距
    public void setTop(int top) {
        this.top = top;
        messageTextView.setPadding(left, top, right, bottom);//设置内容与四周的间距
    }

    //设置内容下间距
    public void setBottom(int bottom) {
        this.bottom = bottom;
        messageTextView.setPadding(left, top, right, bottom);//设置内容与四周的间距
    }

    //设置底部按钮的高度
    public void setBottomHeight(int bottomHeight) {
        this.bottomHeight = bottomHeight;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, bottomHeight);
        linearLayout1.setLayoutParams(params);
    }


    //设置顶部标题的高度
    public void setTitleHeight(int titleHeight) {
        this.titleHeight = titleHeight;
        titleTextView.setHeight(titleHeight);
    }


}

第三步:

使用:
使用方法和Android自带的AlertDialog是一样的,只是不需要create,直接设置完标题和内容,确定和取消,就show就行了,下面是示例:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        new MyDialog(MainActivity.this)
                .setTitle("警告")
                .setMessage("你确定要删除该文件吗?")
                .setPositiveButton("是", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        
                    }
                })
                .setPositiveButton("否", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        
                    }
                })
                .show();
    }

你可能感兴趣的:(Android 傻瓜式用上高仿iOS对话框Dialog)