自定义菜单

思路:使用新的控件,使它的行为接近菜单的特性


菜单的特性:

显示:按menu键

关闭:1) 再按menu键

2) 按back键

// 监听back键,如果用户点击了back

// 判断如果菜单显示,则关闭菜单,同时,不关闭activity

3) 处于暂停状态

4) 点击某个菜单项

5) 点击菜单和通知栏以外的区域(不会响应点击事件),

1. 创建菜单:在onCreate()中添加如下代码:
                mainLayout = findViewById(R.id.main);
                View contentView = getLayoutInflater().inflate(R.layout.optionsmenu, null);
        mOptionsMenu = new PopupWindow(contentView , LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        contentView.findViewById(R.id.TextView02).setOnClickListener(this);
        contentView.findViewById(R.id.view1).setOnClickListener(this);
            
2. 点菜单键显示隐藏菜单,按back键隐藏菜单
      @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        switch (keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if (mOptionsMenu.isShowing())
            {
                mOptionsMenu.dismiss();
                return false; 
            }
            break;
        case KeyEvent.KEYCODE_MENU:
            if (mOptionsMenu.isShowing())
            {
                mOptionsMenu.dismiss();
            }
            else
            {
                mOptionsMenu.showAtLocation(mainLayout, Gravity.BOTTOM, 0, 0);
            }
            break;
        default:
            break;
        }
        return super.onKeyDown(keyCode, event);
    }                   
3. 暂停状态时隐藏菜单
        @Override
    protected void onPause()
    {
        super.onPause();
        if (mOptionsMenu.isShowing())
        {
            mOptionsMenu.dismiss();
        }
    }
4. 点击某个菜单项 隐藏菜单 ;点击菜单和通知栏以外的区域(不会响应点击事件)
        @Override
    public void onClick(View v)
    {
        // 不同菜单的点击事件
        switch (v.getId())
        {
        case R.id.TextView02:
            break;
        case R.id.view1:
                
            break;
        default:
            break;
        }
        mOptionsMenu.dismiss();
    }
5. 布局文件两个
    activity_main.xml布局:
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_btn_refresh"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="37dp"
        android:text="Button" />
        </RelativeLayout>
    optionsmenu.xml布局:
        
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A0F7E6C7"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >
        <Button
            android:id="@+id/TextView02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_btn_refresh"
            android:drawableTop="@android:drawable/ic_menu_add"
            android:gravity="center_horizontal"
            android:text="设置"
            android:textAppearance="?android:attr/textAppearanceMedium" />
        <Button
            android:id="@+id/TextView01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@android:drawable/ic_menu_add"
            android:gravity="center_horizontal"
            android:text="升级"
            android:textAppearance="?android:attr/textAppearanceMedium" />
        <Button
            android:id="@+id/TextView04"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@android:drawable/ic_menu_add"
            android:gravity="center_horizontal"
            android:text="书签"
            android:textAppearance="?android:attr/textAppearanceMedium" />
        <Button
            android:id="@+id/TextView03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@android:drawable/ic_menu_add"
            android:gravity="center_horizontal"
            android:text="刷新"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>
    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/menus"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
        </RelativeLayout>

201450964.jpg