当前位置: 移动技术网 > IT编程>移动开发>Android > Android PopupWindow实现右侧、左侧和底部弹出菜单

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

飞向别人的床歌词,徐咏琳,圣光万灵药

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

项目代码:

项目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(); 
    
  } 
 }; 
 /** 
  * 添加新笔记时弹出的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, layoutparams.wrap_content, layoutparams.fill_parent, 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, 0, 500); 
  }else if(location.right.ordinal() == from){ 
   popupwindow.showatlocation(getlayoutinflater().inflate(r.layout.activity_main, null), gravity.right, 0, 500); 
  }else if(location.bottom.ordinal() == from){ 
   popupwindow.showatlocation(getlayoutinflater().inflate(r.layout.activity_main, null), gravity.bottom|gravity.center_horizontal, 0, 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, "open", toast.length_long).show(); 
    popupwindow.dismiss(); 
   } 
  }); 
   
  close.setonclicklistener(new onclicklistener() { 
    
   @override 
   public void onclick(view v) { 
    toast.maketext(context, "open", 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); 
 } 
 /** 
  * 菜单弹出方向 
  * 
  */ 
 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 name="pop_left">弹出左侧菜单</string> 
 <string name="pop_right">弹出右侧菜单</string> 
 <string name="pop_bottom">弹出底部菜单</string> 
 <string name="open">打开</string> 
 <string name="save">保存</string> 
 <string name="close">关闭</string> 

styles.xml

<style name="animationleftfade"> 
  <item name="android:windowenteranimation">@anim/in_lefttoright</item> 
  <item name="android:windowexitanimation">@anim/out_righttoleft</item> 
 </style> 
  
 <style name="animationrightfade"> 
  <item name="android:windowenteranimation">@anim/in_righttoleft</item> 
  <item name="android:windowexitanimation">@anim/out_lefttoright</item> 
 </style> 
  
 <style name="animationbottomfade"> 
  <item name="android:windowenteranimation">@anim/in_bottomtotop</item> 
  <item name="android:windowexitanimation">@anim/out_toptobottom</item> 
 </style> 

左边弹出菜单动画文件:

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使用。

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网