android 快速开发-Dialog的简单封装

先看效果
dialog.gif

使用方法:

AndroidStudio引入(https://jitpack.io/)

step1:Add it in your root build.gradle at the end of repositories:
allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
step2:Add the dependency
dependencies {
             compile 'com.github.ithedan:BaseDialog:1.0'
    }
Activity中使用:
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bottom = (Button) findViewById(R.id.bottom_top);
        Button top = (Button) findViewById(R.id.top_bottom);
        Button left = (Button) findViewById(R.id.left_right);
        Button right = (Button) findViewById(R.id.right_left);
        Button center = (Button) findViewById(R.id.center);
        Button center1 = (Button) findViewById(R.id.center1);
        bottom.setOnClickListener(this);
        top.setOnClickListener(this);
        left.setOnClickListener(this);
        right.setOnClickListener(this);
        center.setOnClickListener(this);
        center1.setOnClickListener(this);
    }


  private void showDialog(int grary, int animationStyle) {
        BaseDialog.Builder builder = new BaseDialog.Builder(this);
        final BaseDialog dialog = builder.setViewId(R.layout.photo_choose_dialog)
                .setPaddingdp(10, 0, 10, 0)//设置dialogpadding
                .setGravity(grary)//设置显示位置
                .setAnimation(animationStyle)//设置动画
                .setWidthHeightpx(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)//设置dialog的宽高
                .isOnTouchCanceled(true)//设置触摸dialog外围是否关闭dialog
                .addViewOnClickListener(R.id.but_choose_one, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,"相册", Toast.LENGTH_SHORT).show();
                    }
                })//设置监听事件
                .builder();
        dialog.show();

        Button button = dialog.getView(R.id.but_choose_three);//根据id获取dialog中的恐惧
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.close();
            }
        });//关闭dialog
    }


  @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bottom_top:
                showDialog(Gravity.BOTTOM, R.style.Bottom_Top_aniamtion);
                break;
            case R.id.top_bottom:
                showDialog(Gravity.TOP, R.style.Top_Bottom_aniamtion);
                break;
            case R.id.left_right:
                showDialog(Gravity.CENTER, R.style.Left_Right_aniamtion);
                break;
            case R.id.right_left:
                showDialog(Gravity.CENTER, R.style.Right_Left_aniamtion);
                break;
            case R.id.center:
                showDialog(Gravity.CENTER, R.style.Alpah_aniamtion);
                break;
            case R.id.center1:
                showDialog(Gravity.CENTER, R.style.Scale_aniamtion);
                break;
        }
    }
在BaseDialog:1.0中默认定义了以上六种动画效果,如果达不到项目要求可以根据自己的需求在style中自己定义,调用builder.setAnimation(int styleAnimation) 即可,dialog中默认的styles是:

    
如果不符合要求可以调用builder.setStyle(int themeResId)设置自己的style;
源码地址
dialog封装主要是为了项目中有多个dialog,不需要每个自定义dialog都要重写,有一个BaseDialog就够用了,如有什么问题,敬请提出,十分感谢!希望越来越好,谢谢!如果喜欢,还请点击start,喜欢支持一下了,谢谢O(∩_∩)O~

你可能感兴趣的:(android 快速开发-Dialog的简单封装)