• 自己定义的弹出菜单


    先上图:



    自己定义的PopMenu类:

    package com.app.view;


    import java.util.ArrayList;
    import java.util.List;


    import android.content.Context;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.PopupWindow;


    import com.app.utils.TDevice;
    import com.base.fun.R;


    /**
     * 由下向上弹出菜单
     */
    public class MyPopMenu {


    /**
    * 菜单点击事件
    */
    public static interface MenuClick {
    /**
    * 点击了菜单项
    */
    public void onClick(MenuItem item);


    /**
    * 点击了取消button
    */
    public void onCancel();


    /**
    * 点击了空白处
    */
    public void onClickBlank();
    }


    /**
    * 菜单项
    */
    public static class MenuItem {
    public int id; // 菜单ID
    public String label; // 菜单label


    public MenuItem(int id, String label) {
    this.id = id;
    this.label = label;
    }


    @Override
    public String toString() {
    return "MenuItem [id=" + id + ", label=" + label + "]";
    }
    }


    // 弹出菜单的ID、显示label
    private ArrayList<MenuItem> mMenus;


    // 菜单点击回调
    private MenuClick mCallback;


    private Context mContext;


    /**
    * 弹出菜单

    * @param menus
    *            菜单项
    * @param callback
    *            菜单点击回调
    */
    public MyPopMenu(Context ctx, ArrayList<MenuItem> menus, MenuClick callback) {
    mContext = ctx;
    mMenus = menus;
    mCallback = callback;
    initPopupWindow();
    }


    /**
    * 初始化菜单项
    */
    private View initMenus(Context ctx) {
    LayoutInflater inflater = LayoutInflater.from(ctx);


    View root = inflater.inflate(R.layout.lyt_pop_menu, null);
    if (mMenus == null && mMenus.isEmpty()) {
    return root;
    }


    root.setOnClickListener(new View.OnClickListener() {


    @Override
    public void onClick(View v) {
    if (mCallback != null) {
    // 点击空白处
    mCallback.onClickBlank();
    }
    }
    });


    ViewGroup lytMenus = (ViewGroup) root.findViewById(R.id.lyt_menus);
    lytMenus.removeAllViews();
    // 加入菜单项
    for (int i = 0, size = mMenus.size(); i < size; ++i) {
    final MenuItem item = mMenus.get(i);
    View vMenu = inflater.inflate(R.layout.lyt_menu_item, null);
    lytMenus.addView(vMenu);
    vMenu.setId(item.id); // 菜单ID
    vMenu.setTag(item);


    // 第一个菜单是不显示上面的“切割线的”
    if (i == 0) {
    vMenu.findViewById(R.id.line).setVisibility(View.GONE);
    }
    Button btn = (Button) vMenu.findViewById(R.id.btn_menu);
    btn.setText(item.label);
    btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    if (mCallback != null) {
    mCallback.onClick(item);
    }
    }
    });
    }


    // 取消button
    root.findViewById(R.id.btn_cancel).setOnClickListener(
    new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    if (mCallback != null) {
    mCallback.onCancel();
    }
    }
    });


    return root;
    }


    private PopupWindow mPopupWindow;


    /**
    * 设置菜单enable状态
    */
    public void setEnabelMenu(int menuId, boolean enabel) {
    if (mPopupWindow != null) {
    View root = mPopupWindow.getContentView();
    if (root != null) {
    ViewGroup lytMenus = (ViewGroup) root
    .findViewById(R.id.lyt_menus);
    for (int i = 0, count = lytMenus.getChildCount(); i < count; ++i) {
    View menu = lytMenus.getChildAt(i);
    if (menu.getId() == menuId) {
    menu.setEnabled(enabel);
    menu.findViewById(R.id.btn_menu).setEnabled(enabel);
    break;
    }
    }
    }
    }
    }


    private void initPopupWindow() {
    if (mPopupWindow == null) {
    mPopupWindow = new PopupWindow(mContext);
    mPopupWindow = new PopupWindow((int) TDevice.getScreenWidth(),
    (int) TDevice.getScreenHeight());


    View vMenus = initMenus(mContext);
    mPopupWindow.setContentView(vMenus);
    }
    }


    /**
    * 显示菜单
    */
    public void show(View parentView) {
    initPopupWindow();


    if (mPopupWindow.isShowing()) {
    return;
    }
    mPopupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
    }


    /**
    * 隐藏菜单
    */
    public void hide() {
    mPopupWindow.dismiss();
    }
    }


    layout文件:

    1、菜单item

    <?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" >


        <View
            android:id="@+id/line"
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:background="@color/color_listview_divider" />


        <Button
            android:id="@+id/btn_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:text="@string/str_cancel" />


    </LinearLayout>


    2、菜单layout:

    <?

    xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lyt_popmenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#7f000000" >


        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dip"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="@dimen/space_15"
            android:background="@drawable/menu_bkg"
            android:text="@string/str_cancel" />


        <LinearLayout
            android:id="@+id/lyt_menus"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/btn_cancel"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:background="@drawable/menu_bkg"
            android:orientation="vertical" >


            <include layout="@layout/lyt_menu_item" />


            <include layout="@layout/lyt_menu_item" />


            <include layout="@layout/lyt_menu_item" />
        </LinearLayout>


    </RelativeLayout>


    使用代码:


    // 点击头像弹出菜单

    private MyPopMenumPopMenus;

    // 菜单项

    private ArrayList<MenuItem>mMenus;

    // 菜单回调

    private MenuClickmMenuClickListener =new MenuClick() {


    @Override

    public void onClick(MenuItem item) {

    if (item !=null) {

    if (MENU_ID_DELETE == item.id) {

    // 删除...

    } else if (MENU_ID_TAKEPHOTO == item.id) {

    jumpToTakePhoto();

    } else if (MENU_ID_SELECT == item.id) {

    jumpToSelectPic();

    }

    }

    onCancel();

    }


    @Override

    public void onCancel() {

    if (mPopMenus !=null) {

    mPopMenus.hide();

    }

    }


    @Override

    public void onClickBlank() {

    onCancel();

    }

    };


    // 点击头像弹出的菜单项

    privatefinalint MENU_ID_DELETE = 0;

    privatefinalint MENU_ID_TAKEPHOTO = 1;

    privatefinalint MENU_ID_SELECT = 2;


    /**

    * 点击头像,弹出菜单

    */

    private void showHeaderMenus() {

    if (mPopMenus ==null) {

    mMenus = new ArrayList<MenuItem>();

    mMenus.add(new MenuItem(MENU_ID_DELETE,"删除"));

    mMenus.add(new MenuItem(MENU_ID_TAKEPHOTO,"拍照"));

    mMenus.add(new MenuItem(MENU_ID_SELECT,"从相冊里选择"));


    mPopMenus =new MyPopMenu(this,mMenus,mMenuClickListener);

    // 灰掉“删除菜单”

    mPopMenus.setEnabelMenu(MENU_ID_DELETE,false);

    }

    mPopMenus.show(findViewById(R.id.lyt_header));

    }





  • 相关阅读:
    BZOJ3672/UOJ7 [Noi2014]购票
    POJ3718 Facer's Chocolate Dream
    BZOJ1453:[WC]Dface双面棋盘
    BZOJ2957:楼房重建
    AtCoder Grand Contest 009 D:Uninity
    BZOJ2877:[NOI2012]魔幻棋盘
    BZOJ3065:带插入区间K小值
    BZOJ3489:A simple rmq problem
    浅谈主席树
    AtCoder Regular Contest 080 E:Young Maids
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7382365.html
Copyright © 2020-2023  润新知