关闭

【Android】PopupMenu的使用

150人阅读 评论(0) 收藏 举报
分类:

效果图:

     


使用PopupMenu可以很方便地在某个控件下方显示简单的菜单,并实现菜单项的点击响应。

(1)在res/menu目录下生成一个menu的xml文件,如此处的pop_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item 
        android:id="@+id/pop_menu_item_share"
        android:title="@string/pop_menu_share"
        />
    
    <item 
        android:id="@+id/pop_menu_item_setting"
        android:title="@string/pop_menu_setting"
        />

</menu>

(2)主页面布局,这里只有一个TextView,activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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="com.ydz.test026_popupmenu.MainActivity" >

    <TextView
        android:id="@+id/textview_pop_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_green_dark"
        android:text="@string/text_pop"
        android:textSize="30sp" />

</RelativeLayout>

(3)Activity中的逻辑处理:

package com.ydz.test026_popupmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private TextView mTextviewPopMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextviewPopMenu = (TextView) findViewById(R.id.textview_pop_menu);
        mTextviewPopMenu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建PopupMenu,其中第二个参数为要关联显示的控件
                PopupMenu tPopupMenu = new PopupMenu(MainActivity.this, mTextviewPopMenu);
                // 装载视图,Menu在构造函数中已经生成,此处getMenu直接用即可
                tPopupMenu.getMenuInflater().inflate(R.menu.pop_menu, tPopupMenu.getMenu());
                // 设置item的点击监听
                tPopupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.pop_menu_item_share:
                                Toast.makeText(getApplicationContext(), "Click Share",
                                        Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.pop_menu_item_setting:
                                Toast.makeText(getApplicationContext(), "Click Setting",
                                        Toast.LENGTH_SHORT).show();
                                break;
                            default:
                                break;
                        }
                        return true;
                    }
                });
                // 显示
                tPopupMenu.show();
            }
        });
    }
}


如果某个menu选项只是用于显示而不用响应事件处理的话,可以对该item设置enable属性,如:

<item 
        android:id="@+id/pop_menu_item_setting"
        android:title="@string/pop_menu_setting"
        android:enabled="false"
        />







0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7785次
    • 积分:129
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    文章存档
    最新评论