初学Android,引用了这篇文章的代码 http://www.cnblogs.com/jiezzy/archive/2012/08/15/2640584.html

使用PopupWindow制作自定义的菜单

先是Layout文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" > <Button
android:id="@+id/menu_btnBackUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/backup"
android:layout_margin="0.5dp"
android:width="100dp" /> <Button
android:id="@+id/menu_btnRevert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_margin="0.5dp"
android:background="@drawable/importing"
android:width="100dp" /> <Button
android:id="@+id/menu_btnCompany"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/company"
android:layout_margin="0.5dp"
android:width="100dp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" > <Button
android:id="@+id/menu_btnCustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0.5dp"
android:layout_marginTop="5dp"
android:background="@drawable/customer"
android:width="100dp" /> <Button
android:id="@+id/menu_btnTeSe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0.5dp"
android:layout_marginTop="5dp"
android:background="@drawable/tese"
android:width="100dp" /> <Button
android:id="@+id/menu_btnTool"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0.5dp"
android:layout_marginTop="5dp"
android:background="@drawable/setting"
android:width="100dp" />
</LinearLayout> </LinearLayout>

然后是一个Activity

package com.tdcontactapp.lz;

import com.tdcontactapp.CustomContactsActivity;
import com.tdcontactapp.EditAccountActivity;
import com.tdcontactapp.GroupContactsActivity;
import com.tdcontactapp.R;
import com.tdcontactapp.RecvertActivity;
import com.tdcontactapp.RemindActivity; import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow; public class LzMenuActivity extends Activity {
protected PopupWindow mPopupWindow = null;
protected Activity Context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showBottomMenu();
} public void showBottomMenu() {
Context mContext = this;
LayoutInflater mLayoutInfalter = (LayoutInflater) this
.getSystemService(LAYOUT_INFLATER_SERVICE);
View menuView = mLayoutInfalter.inflate(R.layout.lz_popup, null); mPopupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT); mPopupWindow
.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mPopupWindow.setOutsideTouchable(true); // 设置是否允许在外点击使其消失,到底有用没? mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
mPopupWindow.update();
mPopupWindow.setTouchable(true);
mPopupWindow.setFocusable(true); menuView.setFocusableInTouchMode(true);
menuView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((keyCode == KeyEvent.KEYCODE_MENU)
&& (mPopupWindow.isShowing())) {
mPopupWindow.dismiss();// 这里写明模拟menu的PopupWindow退出就行
return true;
}
return false;
}
});
/* 设置点击menu以外其他地方以及返回键退出 */ Button btnBackup=(Button)menuView.findViewById(R.id.menu_btnBackUp);
Button btnRevert=(Button)menuView.findViewById(R.id.menu_btnRevert);
Button btnCompany=(Button)menuView.findViewById(R.id.menu_btnCompany);
Button btnCustom=(Button)menuView.findViewById(R.id.menu_btnCustom);
Button btnTese=(Button)menuView.findViewById(R.id.menu_btnTeSe);
Button btnTool=(Button)menuView.findViewById(R.id.menu_btnTool); btnBackup.setOnClickListener(new btnClick("menu_btnBackUp"));
btnRevert.setOnClickListener(new btnClick("menu_btnRevert"));
btnCompany.setOnClickListener(new btnClick("menu_btnCompany"));
btnCustom.setOnClickListener(new btnClick("menu_btnCustom"));
btnTese.setOnClickListener(new btnClick("menu_btnTeSe"));
btnTool.setOnClickListener(new btnClick("menu_btnTool"));
} private class btnClick implements OnClickListener{
private String name;
public btnClick(String name){
this.name=name;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
if(name.equals("menu_btnBackUp")){
intent.setClass(LzMenuActivity.this,CustomContactsActivity.class);
}
if(name.equals("menu_btnRevert")){
intent.setClass(LzMenuActivity.this,RecvertActivity.class);
}
if(name.equals("menu_btnCompany")){
intent.setClass(LzMenuActivity.this,GroupContactsActivity.class);
}
if(name.equals("menu_btnCustom")){
intent.setClass(LzMenuActivity.this,CustomContactsActivity.class);
}
if(name.equals("menu_btnTeSe")){
intent.setClass(LzMenuActivity.this,RemindActivity.class);
}
if(name.equals("menu_btnTool")){
intent.setClass(LzMenuActivity.this,EditAccountActivity.class);
}
startActivity(intent);
} } @Override
public boolean onCreateOptionsMenu(Menu menu) {
//Toast.makeText(LzMenuActivity.this, "菜单事件", Toast.LENGTH_LONG).show();
menu.add("menu");// 必须创建一项
return super.onCreateOptionsMenu(menu);
} @Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (mPopupWindow != null) {
if (!mPopupWindow.isShowing()) {
/* 最重要的一步:弹出显示 在指定的位置(parent) 最后两个参数 是相对于 x / y 轴的坐标 */
View v= ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
if(v==null)
return false;
mPopupWindow.showAtLocation(v,
Gravity.BOTTOM, 0, 0);
}
}
return false;// 返回为true 则显示系统menu
} }

只要另一个需要显示自定义菜单 的Activity 继承上面的类,就可以显示菜单

Android PopupWindow菜单的更多相关文章

  1. android PopupWindow使用实例

    注:点空白或菜单外隐藏popupwindow菜单: 但是,若点击有点击事件的组件则要再写代码手动隐藏: @Override public boolean onTouchEvent(MotionEven ...

  2. Android PopupWindow的使用技巧(转)

    Android PopupWindow的使用技巧 PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 public PopupWindow(V ...

  3. Android PopupWindow Dialog 关于 is your activity running 崩溃详解

    Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...

  4. Android侧滑菜单代码实现

    前两天学习了hyman老师讲的Android侧滑菜单的实现,经过自己的整理分享出来给大家学习一下 现在很多APP都有菜单侧滑的功能,本篇文章主要讲解使用自定义的HorizontalScrollView ...

  5. Android PopupWindow的使用和分析

    Android PopupWindow的使用和分析 PopupWindow使用 PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activi ...

  6. android 三级菜单 BaseExpandableListAdapter

    在网上搜了非常长时间.没有找到合适的Android三级菜单.所以就自己动手写了一个,主要使用了BaseExpandableList来实现,通过三个布局文件来完毕相应的菜单项,详细实现请參照下图. wa ...

  7. Android滑动菜单框架完全解析,教你如何一分钟实现滑动菜单特效

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744400 之前我向大家介绍了史上最简单的滑动菜单的实现方式,相信大家都还记得.如 ...

  8. 【转】Android 系统菜单与自定义菜单

    Android 系统菜单与自定义菜单实现方法如下:系统菜单显示DefaultMenu.java package com.wxz.menu; import com.wxz.menu.R; import  ...

  9. BottomBar之Android底部菜单

    BottomBar之Android底部菜单 前言:开源项目BottomBar,实现Android底部菜单(常用菜单,BottomBar实现动画(上下式)+消息菜单,BottomBar+ViewPage ...

随机推荐

  1. 使用bootstrap建立响应式网页——通栏轮播图(carousel)

    1.bootstrap提供了js插件——轮播图 我们还是照旧,直接拿过来用,需要改的地方再说. 2.修改 小屏幕看小图,大屏图看大图:这个可以利用自定义属性(data-XXX)data-img-lg( ...

  2. Spring Aop详尽教程

    一.概念 AOP(Aspect Oriented Programming):面向切面编程. 面向切面编程(也叫面向方面编程),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用AO ...

  3. 深耕教育行业,RealSeer联合黑晶科技发布“AR超级教室”

    近日,RealSeer开发者大赛见面会最后一站在北京举行,现场云集了不少AR创业者和开发者,各位大咖嘉宾都拿出干货与大家分享交流,公话未来AR行业发展趋势.现场RealMax联合黑晶科技发布了新品&q ...

  4. Jquery获得 selection的text 和 option值

    Jquery获得 selection的text 和 option值 <select id="accountStatus" editable="false" ...

  5. Linux vim编辑器

    1.工作模式 命令模式(a,i,o等键)——>输入模式 输入模式(Esc键)——>命令模式 命令模式(:键)——>末行模式 末行模式(Esc键)——>命令模式 2.vim使用命 ...

  6. chapter 13_1 算术类的元方法

    假设用table来表示集合,用function去计算集合的交集.并集. 为了保持名称空间的整齐,将这些函数存入一个名为Set的table中. 现在,假设用“+”来计算两个集合的并集,那么就要让所有用于 ...

  7. REST认识

    大家对REST的认识? 谈到REST大家的第一印象就是通过http协议的GET,POST,DELETE,PUT方法实现对url资源的CRUD(创建.读取.更新和删除)操作.比如http://www.a ...

  8. MySQL 多表查询分页

    SELECT v.*, vt.id vid, vt.vote_id, vt. option, vt.poll FROM vote v JOIN vote_option vt ON v.id = vt. ...

  9. IOS 第三方库之-MBProgressHUD的使用详解

    转自作者: weidfyr  http://www.aiuxian.com/article/p-3121607.html 1,MBProgressHUD常用属性和用法Demo - (void)test ...

  10. Android:assests和raw的区别

    转自:http://www.cnblogs.com/lianghui66/p/3248636.html ------------------------------------------------ ...