Android PopupWindow实现右侧、左侧和底部d出菜单

Android PopupWindow实现右侧、左侧和底部d出菜单,第1张

概述本教程为大家分享了AndroidPopupWindowd出菜单的具体代码,供大家参考,具体内容如下

本教程为大家分享了AndroID PopupWindowd出菜单的具体代码,供大家参考,具体内容如下

项目代码:http://xiazai.jb51.net/201611/yuanma/PopupLeftMenu(jb51.net).rar

项目SDK是5.1,建议将代码拷到自己的工程中去

代码如下:

MainActivity类:

package com.example.popupleftmenu;  import androID.app.Activity; import androID.content.Context; import androID.graphics.drawable.colorDrawable; import androID.os.Bundle; import androID.vIEw.Gravity; import androID.vIEw.MotionEvent; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.vIEw.VIEw.OntouchListener; import androID.vIEw.VIEwGroup.LayoutParams; import androID.vIEw.WindowManager; import androID.Widget.button; import androID.Widget.PopupWindow; import androID.Widget.Toast;  public class MainActivity extends Activity {   private Context context = null;  private PopupWindow popupWindow;  private int from = 0;    @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   context = this;   setContentVIEw(R.layout.activity_main);   button popleftBtn = (button)findVIEwByID(R.ID.pop_left_btn);   button popRightBtn = (button)findVIEwByID(R.ID.pop_right_btn);   button popBottomBtn = (button)findVIEwByID(R.ID.pop_bottom_btn);   popleftBtn.setonClickListener(popClick);   popRightBtn.setonClickListener(popClick);   popBottomBtn.setonClickListener(popClick);  }     OnClickListener popClick = new OnClickListener() {      @OverrIDe   public voID onClick(VIEw v) {    switch(v.getID()){     case R.ID.pop_left_btn:{      from = Location.left.ordinal();      break;     }     case R.ID.pop_right_btn:{      from = Location.RIGHT.ordinal();      break;     }     case R.ID.pop_bottom_btn:{      from = Location.BottOM.ordinal();      break;     }    }        //调用此方法,menu不会顶置    //popupWindow.showAsDropDown(v);    initPopupWindow();       }  };  /**   * 添加新笔记时d出的popWin关闭的事件,主要是为了将背景透明度改回来   *   */  class popupdismissListener implements PopupWindow.OndismissListener{    @OverrIDe   public voID ondismiss() {    backgroundAlpha(1f);   }     }      protected voID initPopupWindow(){   VIEw popupWindowVIEw = getLayoutInflater().inflate(R.layout.pop,null);   //内容,高度,宽度   if(Location.BottOM.ordinal() == from){    popupWindow = new PopupWindow(popupWindowVIEw,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,true);   }else{    popupWindow = new PopupWindow(popupWindowVIEw,true);   }   //动画效果   if(Location.left.ordinal() == from){    popupWindow.setAnimationStyle(R.style.AnimationleftFade);   }else if(Location.RIGHT.ordinal() == from){    popupWindow.setAnimationStyle(R.style.AnimationRightFade);   }else if(Location.BottOM.ordinal() == from){    popupWindow.setAnimationStyle(R.style.AnimationBottomFade);   }   //菜单背景色   colorDrawable DW = new colorDrawable(0xffffffff);   popupWindow.setBackgroundDrawable(DW);   //宽度   //popupWindow.setWIDth(LayoutParams.WRAP_CONTENT);   //高度   //popupWindow.setHeight(LayoutParams.FILL_PARENT);   //显示位置   if(Location.left.ordinal() == from){    popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main,null),Gravity.left,500);   }else if(Location.RIGHT.ordinal() == from){    popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main,Gravity.RIGHT,500);   }else if(Location.BottOM.ordinal() == from){    popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main,Gravity.BottOM|Gravity.CENTER_HORIZONTAL,0);   }   //设置背景半透明   backgroundAlpha(0.5f);   //关闭事件   popupWindow.setondismissListener(new popupdismissListener());      popupWindowVIEw.setontouchListener(new OntouchListener() {        @OverrIDe    public boolean ontouch(VIEw v,MotionEvent event) {     /*if( popupWindow!=null && popupWindow.isShowing()){      popupWindow.dismiss();      popupWindow=null;     }*/     // 这里如果返回true的话,touch事件将被拦截     // 拦截后 PopupWindow的ontouchEvent不被调用,这样点击外部区域无法dismiss     return false;    }   });      button open = (button)popupWindowVIEw.findVIEwByID(R.ID.open);   button save = (button)popupWindowVIEw.findVIEwByID(R.ID.save);   button close = (button)popupWindowVIEw.findVIEwByID(R.ID.close);         open.setonClickListener(new OnClickListener() {        @OverrIDe    public voID onClick(VIEw v) {     Toast.makeText(context,"Open",Toast.LENGTH_LONG).show();     popupWindow.dismiss();    }   });      save.setonClickListener(new OnClickListener() {        @OverrIDe    public voID onClick(VIEw v) {     Toast.makeText(context,Toast.LENGTH_LONG).show();     popupWindow.dismiss();    }   });      close.setonClickListener(new OnClickListener() {        @OverrIDe    public voID onClick(VIEw v) {     Toast.makeText(context,Toast.LENGTH_LONG).show();     popupWindow.dismiss();    }   });  }    /**   * 设置添加屏幕的背景透明度   * @param bgAlpha   */  public voID backgroundAlpha(float bgAlpha)  {   WindowManager.LayoutParams lp = getwindow().getAttributes();   lp.Alpha = bgAlpha; //0.0-1.0   getwindow().setAttributes(lp);  }  /**   * 菜单d出方向   *   */  public enum Location {    left,RIGHT,top,BottOM;     } } 

两个布局文件:

1.activity_main.xml,就三个button

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools"  androID:layout_wIDth="fill_parent"  androID:layout_height="fill_parent"  androID:orIEntation="vertical">   <button   androID:ID="@+ID/pop_left_btn"   androID:layout_wIDth="fill_parent"   androID:layout_height="wrap_content"   androID:text="@string/pop_left"/>    <button   androID:ID="@+ID/pop_right_btn"   androID:layout_wIDth="fill_parent"   androID:layout_height="wrap_content"   androID:text="@string/pop_right"/>    <button   androID:ID="@+ID/pop_bottom_btn"   androID:layout_wIDth="fill_parent"   androID:layout_height="wrap_content"   androID:text="@string/pop_bottom"/>    </linearLayout>

2. pop.xml,也是三个button,可以自己修改

<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="fill_parent"  androID:layout_height="fill_parent"  androID:orIEntation="vertical" >    <!-- <linearLayout   androID:layout_wIDth="wrap_content"   androID:layout_height="fill_parent"   androID:orIEntation="vertical"   androID:background="#ffffff"> -->      <button androID:ID="@+ID/open"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:text="@string/open"/>      <button androID:ID="@+ID/save"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:text="@string/save"/>      <button androID:ID="@+ID/close"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:text="@string/close"/>     <!-- </linearLayout> -->   </linearLayout> 

strings.xml

<string @R_301_6889@="pop_left">d出左侧菜单</string>  <string @R_301_6889@="pop_right">d出右侧菜单</string>  <string @R_301_6889@="pop_bottom">d出底部菜单</string>  <string @R_301_6889@="open">打开</string>  <string @R_301_6889@="save">保存</string>  <string @R_301_6889@="close">关闭</string> 

styles.xml

<style @R_301_6889@="AnimationleftFade">   <item @R_301_6889@="androID:windowEnteranimation">@anim/in_lefttoright</item>   <item @R_301_6889@="androID:windowExitAnimation">@anim/out_righttoleft</item>  </style>    <style @R_301_6889@="AnimationRightFade">   <item @R_301_6889@="androID:windowEnteranimation">@anim/in_righttoleft</item>   <item @R_301_6889@="androID:windowExitAnimation">@anim/out_lefttoright</item>  </style>    <style @R_301_6889@="AnimationBottomFade">   <item @R_301_6889@="androID:windowEnteranimation">@anim/in_bottomtotop</item>   <item @R_301_6889@="androID:windowExitAnimation">@anim/out_toptobottom</item>  </style> 

左边d出菜单动画文件:

in_lefttoright.xml:从左边入

<?xml version="1.0" enCoding="utf-8"?> <set xmlns:androID="http://schemas.androID.com/apk/res/androID">    <translate   androID:fromXDelta="-100%"   androID:toXDelta="0"   androID:duration="500"/>   </set> 

out_righttoleft.xml:从右边出

<?xml version="1.0" enCoding="utf-8"?> <set xmlns:androID="http://schemas.androID.com/apk/res/androID">    <translate androID:fromXDelta="0"   androID:toXDelta="-100%"   androID:duration="500"/>  </set> 

其他动画文件自己参考写,就是fromXDelta, fromYDelta, toXDelta和toYDelta使用。

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

总结

以上是内存溢出为你收集整理的Android PopupWindow实现右侧、左侧底部d出菜单全部内容,希望文章能够帮你解决Android PopupWindow实现右侧、左侧和底部d出菜单所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147700.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存