欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义底部弹出框ButtomDialog

程序员文章站 2022-11-19 19:08:38
本文实例为大家分享了android自定义底部弹出框的具体代码,供大家参考,具体内容如下 先看看效果和你要的是否一样 一 、先来配置自定义控件需要的资源 1.在res文件夹下创...

本文实例为大家分享了android自定义底部弹出框的具体代码,供大家参考,具体内容如下

先看看效果和你要的是否一样

Android自定义底部弹出框ButtomDialog

一 、先来配置自定义控件需要的资源

1.在res文件夹下创建一个anim文件夹并创建两个slide_in_bottom.xml、slide_out_bottom.xml文件,负责弹框进出动画。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   android:shareinterpolator="false">
 <!-- slide_in_bottom.xml -->
 <translate
   android:duration="@integer/dp_300"
   android:fromxdelta="0%"
   android:toxdelta="0%"
   android:fromydelta="100%"
   android:toydelta="0%"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   android:shareinterpolator="false">
 <!-- slide_out_bottom.xml -->
 <translate
   android:duration="@integer/dp_300"
   android:fromxdelta="0%"
   android:toxdelta="0%"
   android:fromydelta="0%"
   android:toydelta="100%"/>
</set>

2.在style.xml添加阴影和动画样式。

 <style name="theme.light.notitle.dialog" parent="@android:style/theme.dialog">
    <item name="android:windowbackground">@android:color/transparent</item>
    <item name="android:windownotitle">true</item>
    <item name="android:windowisfloating">true</item>
    <item name="android:windowframe">@null</item>
  </style>
 
  <style name="theme.light.notitle.noshadow.dialog" parent="theme.light.notitle.dialog">
    <item name="android:backgrounddimenabled">false</item>
  </style>
 
  <style name="animation.bottom.rising" parent="@android:style/animation">
    <item name="android:windowenteranimation">@anim/slide_in_bottom</item>
    <item name="android:windowexitanimation">@anim/slide_out_bottom</item>
</style>

3.在drawable文件夹下创建一个title_background.xml文件,负责给文本内容添加背景。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <corners android:radius="8dp"/>
  <solid android:color="#ffffffff"/>
</shape>

二、自定义控件的布局

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:padding="12dp"
       >
 
  <linearlayout
    android:background="@drawable/title_background"
    android:id="@+id/lay_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginbottom="8dp"
    android:orientation="vertical"/>
 
  <textview
    android:id="@+id/btn_cancel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/title_background"
    android:paddingbottom="8dip"
    android:paddingtop="8dip"
    android:text="取消"
    android:gravity="center"
    android:textcolor="#007aff"
    android:textsize="17sp"/>
 
</linearlayout>

三、自定义控件类

public class buttomdialog extends dialog {
 
  public buttomdialog(context context, int themeresid) {
    super(context, themeresid);
  }
 
  public static class params {
    private final list<bottommenu> menulist = new arraylist<>();
    private view.onclicklistener cancellistener;
    private charsequence menutitle;
    private string canceltext;
    private context context;
  }
 
  public static class builder {
    private boolean cancancel = true;
    private boolean shadow = true;
    private final params p;
 
    public builder(context context) {
      p = new params();
      p.context = context;
    }
 
    public builder setcancancel(boolean cancancel) {
      this.cancancel = cancancel;
      return this;
    }
 
    public builder setshadow(boolean shadow) {
      this.shadow = shadow;
      return this;
    }
 
    public builder settitle(charsequence title) {
      this.p.menutitle = title;
      return this;
    }
 
    public builder addmenu(string text, view.onclicklistener listener) {
      bottommenu bm = new bottommenu(text, listener);
      this.p.menulist.add(bm);
      return this;
    }
 
    public builder addmenu(int textid, view.onclicklistener listener) {
      return addmenu(p.context.getstring(textid), listener);
    }
 
    public builder setcancellistener(view.onclicklistener cancellistener) {
      p.cancellistener = cancellistener;
      return this;
    }
 
    public builder setcanceltext(int resid) {
      p.canceltext = p.context.getstring(resid);
      return this;
    }
 
    public builder setcanceltext(string text) {
      p.canceltext = text;
      return this;
    }
 
    public buttomdialog create() {
      final buttomdialog dialog = new buttomdialog(p.context, shadow ? r.style.theme_light_notitle_dialog : r.style.theme_light_notitle_noshadow_dialog);
      window window = dialog.getwindow();
      window.setwindowanimations(r.style.animation_bottom_rising);
      window.getdecorview().setpadding(0, 0, 0, 0);
      windowmanager.layoutparams lp = window.getattributes();
      lp.width = windowmanager.layoutparams.match_parent;
      lp.height = windowmanager.layoutparams.wrap_content;
      window.setattributes(lp);
      window.setgravity(gravity.bottom);
      view view = layoutinflater.from(p.context).inflate(r.layout.dialog_bottom_menu, null);
      textview btncancel = (textview) view.findviewbyid(r.id.btn_cancel);
      viewgroup laycontainer = (viewgroup) view.findviewbyid(r.id.lay_container);
      viewgroup.layoutparams lpitem = new viewgroup.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content);
      viewgroup.marginlayoutparams lpdivider = new viewgroup.marginlayoutparams(viewgroup.layoutparams.match_parent, 1);
      lpdivider.setmargins(50,0,50,0);
      int dip1 = (int) (1 * p.context.getresources().getdisplaymetrics().density + 0.5f);
      int spacing = dip1 * 12;
      boolean hastitle = !textutils.isempty(p.menutitle);
      if (hastitle) {
        //标题样式
        textview ttitle = new textview(p.context);
        ttitle.setlayoutparams(lpitem);
        ttitle.setgravity(gravity.center);
        ttitle.settextcolor(p.context.getresources().getcolor(r.color.coloraccent));
        ttitle.settext(p.menutitle);
        ttitle.setpadding(0, spacing, 0, spacing);
        //单独给标题设置背景样式
//        ttitle.setbackgroundresource(r.drawable.common_dialog_selection_selector_top);
        laycontainer.addview(ttitle);
        view viewdivider = new view(p.context);
        viewdivider.setlayoutparams(lpdivider);
        viewdivider.setbackgroundcolor(0xffced2d6);
        laycontainer.addview(viewdivider);
      }
      //每一条的样式
      for (int i = 0; i < p.menulist.size(); i++) {
        bottommenu bottommenu = p.menulist.get(i);
        textview bbm = new textview(p.context);
        bbm.setlayoutparams(lpitem);
        bbm.setpadding(0, spacing, 0, spacing);
        bbm.setgravity(gravity.center);
        bbm.settext(bottommenu.funname);
        bbm.settextcolor(0xff007aff);
        bbm.settextsize(16);
        bbm.setonclicklistener(bottommenu.listener);
        laycontainer.addview(bbm);
        if (i != p.menulist.size() - 1) {
          view viewdivider = new view(p.context);
          viewdivider.setlayoutparams(lpdivider);
          viewdivider.setbackgroundcolor(0xffced2d6);
          laycontainer.addview(viewdivider);
        }
      }
 
      if (!textutils.isempty(p.canceltext)) {
        btncancel.settext(p.canceltext);
      }
 
      if (p.cancellistener != null) {
        btncancel.setonclicklistener(p.cancellistener);
      } else {
        btncancel.setonclicklistener(new view.onclicklistener() {
          @override
          public void onclick(view v) {
            dialog.dismiss();
          }
        });
      }
 
      dialog.setcontentview(view);
      dialog.setcanceledontouchoutside(cancancel);
      dialog.setcancelable(cancancel);
      return dialog;
    }
 
  }
 
  private static class bottommenu {
    public string funname;
    public view.onclicklistener listener;
 
    public bottommenu(string funname, view.onclicklistener listener) {
      this.funname = funname;
      this.listener = listener;
    }
  }
}

四、使用

public class mainactivity extends appcompatactivity implements view.onclicklistener {
 
  private button mdialogcustom;
  private buttommenudialog dialog;
 
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    initview();
  }
 
  private void initview() {
    mdialogcustom = (button) findviewbyid(r.id.custom_dialog);
    mdialogcustom.setonclicklistener(this);
  }
 
  @override
  public void onclick(view v) {
    switch (v.getid()) {
      case r.id.custom_dialog:
        buttommenudialog.builder builder = new buttommenudialog.builder(this);
        //添加条目,可多个
        builder.addmenu("相机", new view.onclicklistener() {
          @override
          public void onclick(view view) {
            dialog.cancel();
            toast.maketext(mainactivity.this, "相机", toast.length_short).show();
          }
        }).addmenu("相册", new view.onclicklistener() {
          @override
          public void onclick(view view) {
            dialog.cancel();
            toast.maketext(mainactivity.this, "相册", toast.length_short).show();
          }
        });
        //下面这些设置都可不写
        builder.settitle("这是标题");//添加标题
        builder.setcancancel(false);//点击阴影时是否取消dialog,true为取消
        builder.setshadow(true);//是否设置阴影背景,true为有阴影
        builder.setcanceltext("取消");//设置最下面取消的文本内容
        //设置点击取消时的事件
        builder.setcancellistener(new view.onclicklistener() {
          @override
          public void onclick(view view) {
            dialog.cancel();
            toast.maketext(mainactivity.this, "取消", toast.length_short).show();
          }
        });
        dialog = builder.create();
        dialog.show();
        break;
      default:
        break;
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。