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

Android实现底部半透明弹出框PopUpWindow效果

程序员文章站 2022-07-02 12:01:02
android底部半透明弹出框popupwindow,供大家参考,具体内容如下 layout布局:

android底部半透明弹出框popupwindow,供大家参考,具体内容如下

layout布局:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#66fafafa"
 android:orientation="vertical">
 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="122dp"
  android:id="@+id/ll_popupwindow"
  android:background="#ffffff"
  android:layout_alignparentbottom="true"
  android:orientation="vertical"
  >
 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_margintop="26dp"
  android:orientation="horizontal">

  <textview
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:id="@+id/popwindow_facebook"
   android:drawabletop="@mipmap/gif_more_facebook"
   android:drawablepadding="12dp"
   android:gravity="center"
   android:text="facebook"
   android:textcolor="#4d4d4d"
   android:textsize="12sp" />

  <textview
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:id="@+id/popwindow_whatsapp"
   android:drawabletop="@mipmap/gif_more_whatsapp"
   android:drawablepadding="12dp"
   android:gravity="center"
   android:text="whatsapp"
   android:visibility="gone"
   android:textcolor="#4d4d4d"
   android:textsize="12sp" />

  <textview
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:id="@+id/popwindow_report"
   android:drawabletop="@mipmap/gif_more_report"
   android:drawablepadding="12dp"
   android:gravity="center"
   android:text="report"
   android:textcolor="#4d4d4d"
   android:textsize="12sp" />

 </linearlayout>

 </linearlayout>

 </relativelayout>

布局示意:

Android实现底部半透明弹出框PopUpWindow效果

代码部分:   

/*
 * 在当前页面调用initpopupwindow方法,底部弹出popupwindow
 * 重点在popupwindow的layout最外层布局设置android:background="#66fafafa" 半透明
 * */
 private void initpopupwindow(view root, final string uuid, final string title){
  log.d("click","init popopop");
  //inflate得到布局 ,底部弹出框的view
  final view popview = layoutinflater.from(mcontext).inflate(
    r.layout.layout_bottom_popwindow, null);
  view rootview = root; // 当前页面的根布局
  //创建popupwindow对象 宽高占满页面
  final popupwindow popupwindow = new popupwindow(popview,
    windowmanager.layoutparams.match_parent, windowmanager.layoutparams.match_parent);
  popupwindow.settouchable(true);
  // 设置弹出动画
  popupwindow.setanimationstyle(r.style.anim_edit_text_popup);
  // 显示在根布局的底部
  popupwindow.showatlocation(rootview, gravity.bottom | gravity.left, 0,
    0);
  //点击底部弹出框之外的部分让popupwindow 消失
  popview.setontouchlistener(new view.ontouchlistener() {
   @override
   public boolean ontouch(view v, motionevent event) {
    int height = popview.findviewbyid(r.id.ll_popupwindow).gettop();
    int y=(int) event.gety();
    if(event.getaction()==motionevent.action_up){
     if(y<height){
      popupwindow.dismiss();
     }
    }
    return true;
   }
  });
  //弹出框中控件的点击事件
  textview share_facebook= (textview) popview.findviewbyid(r.id.popwindow_facebook);
  share_facebook.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    share_facebook(uuid,title);
    popupwindow.dismiss();
   }
  });

  final textview share_whatsapp= (textview) popview.findviewbyid(r.id.popwindow_whatsapp);
  boolean whatsappfound = checkutils.isappinstalled(mcontext, "com.whatsapp");
  if (whatsappfound) {
   share_whatsapp.setvisibility(view.visible);
   share_whatsapp.setonclicklistener(new view.onclicklistener() {
    @override
    public void onclick(view v) {
     share_whatsapp(uuid,title);
    }
   });
  }

  textview report= (textview) popview.findviewbyid(r.id.popwindow_report);
  report.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    intent intent = new intent(mcontext, reportactivity.class);

    intent.putextra("fromch", true);

    intent.putextra("tid", uuid);
    mcontext.startactivity(intent);
    popupwindow.dismiss();<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

 <translate
  android:duration="100"
  android:fromydelta="0.0"
  android:toydelta="100%" />

</set>
   }  }); }

动画部分

进入时从最下方弹出到最上方

消失时从最上方向下移动直到隐藏

<style name="anim_edit_text_popup">
  <item name="android:windowenteranimation">@anim/popup_in</item>
  <item name="android:windowexitanimation">@anim/popup_out</item>
</style>

popup_in:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >

 <translate
  android:duration="100"
  android:fromydelta="100.0%"
  android:toydelta="0.0" />

</set>

pop_out:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

 <translate
  android:duration="100"
  android:fromydelta="0.0"
  android:toydelta="100%" />

</set>

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