时间:2021-05-20
本教程为大家分享了Android PopupWindow弹出菜单的具体代码,供大家参考,具体内容如下
项目代码: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(); } }; /** * 添加新笔记时弹出的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) { // 这里如果返回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
2. pop.xml,也是三个Button,可以自己修改
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
左边弹出菜单动画文件:
in_lefttoright.xml:从左边入
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使用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
学习了AndroidPopupWindow的使用技巧和【AndroidUI设计与开发】7.底部菜单栏(四)PopupWindow实现显示仿腾讯新闻底部弹出菜单,
在前面PopupWindow实现显示仿腾讯新闻底部弹出菜单有用到Animation动画效果来实现菜单的显示和隐藏,本文就来介绍下吧。1.Animation动画类
本文实例为大家分享了Android实现底部弹出按钮菜单的具体代码,在Android实现底部缓慢弹出菜单的升级,供大家参考,具体内容如下只贴出关键代码caseR.
Android底部半透明弹出框PopUpWindow,供大家参考,具体内容如下layout布局:布局示意:代码部分:/**在当前页面调用initPopUpWin
在popupWindow里面再弹出popupWindow的时候会报这样的错误ERROR/AndroidRuntime(888):android.view.Win