智者樂山山如畫, 仁者樂水水無涯。 從從容容一盃酒, 平平淡淡一盞茶。 細雨朦朧小石橋, 春風盪漾小竹筏。 夜無明月花獨舞, 腹有詩書气自華。 吾生有崖,也知無崖,以有崖逐無崖,殆也

Android PopupWindow菜单

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

使用PopupWindow制作自定义的菜单

先是Layout文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <LinearLayout
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:gravity="center_horizontal"
11         android:orientation="horizontal" >
12 
13         <Button
14             android:id="@+id/menu_btnBackUp"
15             android:layout_width="wrap_content"
16             android:layout_height="wrap_content"
17             android:layout_marginTop="5dp"
18             android:background="@drawable/backup"
19             android:layout_margin="0.5dp"
20             android:width="100dp" />
21 
22         <Button
23             android:id="@+id/menu_btnRevert"
24             android:layout_width="wrap_content"
25             android:layout_height="wrap_content"
26             android:layout_marginTop="5dp"
27             android:layout_margin="0.5dp"
28             android:background="@drawable/importing"
29             android:width="100dp" />
30 
31         <Button
32             android:id="@+id/menu_btnCompany"
33             android:layout_width="wrap_content"
34             android:layout_height="wrap_content"
35             android:layout_marginTop="5dp"
36             android:background="@drawable/company"
37             android:layout_margin="0.5dp"
38             android:width="100dp" />
39     </LinearLayout>
40 
41     <LinearLayout
42         android:layout_width="match_parent"
43         android:layout_height="match_parent"
44         android:gravity="center_horizontal"
45         android:orientation="horizontal" >
46 
47         <Button
48             android:id="@+id/menu_btnCustom"
49             android:layout_width="wrap_content"
50             android:layout_height="wrap_content"
51             android:layout_margin="0.5dp"
52             android:layout_marginTop="5dp"
53             android:background="@drawable/customer"
54             android:width="100dp" />
55 
56         <Button
57             android:id="@+id/menu_btnTeSe"
58             android:layout_width="wrap_content"
59             android:layout_height="wrap_content"
60             android:layout_margin="0.5dp"
61             android:layout_marginTop="5dp"
62             android:background="@drawable/tese"
63             android:width="100dp" />
64 
65         <Button
66             android:id="@+id/menu_btnTool"
67             android:layout_width="wrap_content"
68             android:layout_height="wrap_content"
69             android:layout_margin="0.5dp"
70             android:layout_marginTop="5dp"
71             android:background="@drawable/setting"
72             android:width="100dp" />
73     </LinearLayout>
74 
75 </LinearLayout>
View Code

 

然后是一个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
    }

}
View Code

 

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

posted @ 2013-07-17 10:10  後生哥哥  阅读(1044)  评论(0编辑  收藏  举报
智者樂山山如畫, 仁者樂水水無涯。 從從容容一盃酒, 平平淡淡一盞茶。 細雨朦朧小石橋, 春風盪漾小竹筏。 夜無明月花獨舞, 腹有詩書气自華。 吾生有崖,也知無崖,以有崖逐無崖,殆也