• android封装的menu自定义菜单列表


    自己封装的menu自定义菜单列表,首先必须创建一个类继承activity,让后要调用自定义菜单的类直接继承。

    package com.cng;

    import java.util.ArrayList;
    import java.util.HashMap;



    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.TabActivity;
    import android.content.DialogInterface;
    import android.content.SharedPreferences;
    import android.content.DialogInterface.OnKeyListener;
    import android.content.Intent;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.GridView;
    import android.widget.SimpleAdapter;
    import android.widget.AdapterView.OnItemClickListener;



    public class MenuActivityAdapter extends Activity
    {
        AlertDialog menuDialog;// menu菜单Dialog
        GridView menuGrid;
        View menuView;
        final private int menuSettings=Menu.FIRST; 
        private static final int REQ_SYSTEM_SETTINGS = 0;   


       
        /** 菜单图片 **/
        int[] menu_image_array = {  R.drawable.xuetang_light,R.drawable.tizhong_light,
                R.drawable.maibo_light,
                R.drawable.tiwen_light, R.drawable.xueyang_light,
                 R.drawable.naodian_light,R.drawable.xindian_light,
                R.drawable.xueya_light,
                R.drawable.denglu_light,
                R.drawable.shezhi_light};
        /** 菜单文字 **/
        String[] menu_name_array = { "", "", "", "", "", "",
                "", "", "", ""};
       
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);女装品牌排行榜
           
           
            menuView = View.inflate(this, R.layout.gridview_menu, null);
            // 创建AlertDialog
            menuDialog = new AlertDialog.Builder(this).create();
            menuDialog.setView(menuView);
            menuDialog.setOnKeyListener(new OnKeyListener() {
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_MENU)// 监听按键
                        dialog.dismiss();
                    return false;
                }
            });

            menuGrid = (GridView) menuView.findViewById(R.id.gridview);
            menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));
            /** 监听menu选项 **/
            menuGrid.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    Intent intent=new Intent();
                    switch (arg2) {
                   
                    case 0:// 搜索
                        intent.setClass(MenuActivityAdapter.this, GlucoseList.class);
                        startActivity(intent);
                        menuDialog.dismiss();
                        break;
                    case 1:// 搜索
                        intent.setClass(MenuActivityAdapter.this, WeightList.class);
                        startActivity(intent);
                        menuDialog.dismiss();
                        break;
                    case 2:// 搜索
                        intent.setClass(MenuActivityAdapter.this, MaiboList.class);
                        startActivity(intent);
                        menuDialog.dismiss();
                        break;
                    case 3:// 搜索
                        intent.setClass(MenuActivityAdapter.this, TiwenList.class);
                        startActivity(intent);
                        menuDialog.dismiss();
                        break;
                    case 4:// 搜索
                        intent.setClass(MenuActivityAdapter.this, XueyangList.class);
                        startActivity(intent);
                        menuDialog.dismiss();
                        break;
                    case 5:// 搜索
                        intent.setClass(MenuActivityAdapter.this, NaodianList.class);
                        startActivity(intent);
                        menuDialog.dismiss();
                        break;
                    case 6:// 搜索
                        intent.setClass(MenuActivityAdapter.this, xinlv.class);
                        startActivity(intent);
                        menuDialog.dismiss();
                        break;
                    case 7:// 搜索
                        intent.setClass(MenuActivityAdapter.this, XueyaList.class);
                        startActivity(intent);
                        menuDialog.dismiss();
                        break;
                    case 8:// 搜索
                        intent.setClass(MenuActivityAdapter.this, DengluList.class);
                        startActivity(intent);
                        menuDialog.dismiss();
                        break;
                    case 9:// 搜索
                        startActivityForResult(new Intent(MenuActivityAdapter.this, Settings.class), REQ_SYSTEM_SETTINGS);
                        menuDialog.dismiss();
                        break;
                    }
                   
                   
                }
            });
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            menu.add("menu");// 必须创建一项
            return super.onCreateOptionsMenu(menu);
        }
       
        private SimpleAdapter getMenuAdapter(String[] menuNameArray,
                int[] imageResourceArray) {
            ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
            for (int i = 0; i < menuNameArray.length; i++) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("itemImage", imageResourceArray[i]);
                map.put("itemText", menuNameArray[i]);
                data.add(map);
            }
            SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
                    R.layout.item_menu, new String[] { "itemImage", "itemText" },
                    new int[] { R.id.item_image, R.id.item_text });
            return simperAdapter;
        }
        @Override
        public boolean onMenuOpened(int featureId, Menu menu) {
            if (menuDialog == null) {
                menuDialog = new AlertDialog.Builder(this).setView(menuView).show();
            } else {
                menuDialog.show();
            }
            return false;// 返回为true 则显示系统menu
        }
         //Settings设置界面返回的结果  
        protected  void onActivityResult(int requestCode, int resultCode, Intent data) { 
            if(requestCode == REQ_SYSTEM_SETTINGS) 
            { 
                //获取设置界面PreferenceActivity中各个Preference的值  
                String loginSwitchKey = getResources().getString(R.string.auto_login_switch_key); 
                String customizelist1Key = getResources().getString(R.string.customize_main_list1_key);
                String serverAddressKey = getResources().getString(R.string.server_address_key);
                //取得属于整个应用程序的SharedPreferences  
                SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); 
                Boolean updateSwitch = settings.getBoolean(loginSwitchKey, true); 
                String updateFrequency = settings.getString(customizelist1Key, "体重");
                String serverAddress = settings.getString(serverAddressKey, "172.16.10.254");
                //打印结果  
                Log.v("CheckBoxPreference_Main", updateSwitch.toString()); 
                Log.v("ListPreference_Main", updateFrequency);
                Log.v("EditTextPreference_Main", serverAddress);
               
            } 
            else 
            { 
                //其他Intent返回的结果  
            } 
        } 
    }
    Gridview_menu.xml代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <GridView
             android:id="@+id/gridview"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:numColumns="5"
             android:verticalSpacing="1dip"
             android:horizontalSpacing="1dip"
             android:stretchMode="columnWidth"
             android:gravity="center"
             />
     
    </LinearLayout>

    Item_menu.xml代码

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout_Item"货运专家
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:paddingBottom="1dip">
        <ImageView android:id="@+id/item_image"
            android:layout_centerHorizontal="true" android:layout_width="wrap_content"
            android:layout_height="wrap_content"></ImageView>
        <TextView android:layout_below="@id/item_image" android:id="@+id/item_text"
            android:layout_centerHorizontal="true" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="选项"></TextView>
    </RelativeLayout>

  • 相关阅读:
    [置顶] 【原创分享】嵌入式linux应用之U-BOOT移植定制篇--20130822
    [置顶] java 连接 mysql 数据库步骤
    [置顶] 【原创】无线LED条屏信息报警项目---2012.05
    用Python正则表达式搜索统计命令行管道中的所有数字
    从SharePoint 2013迁移到SharePoint Online
    SharePoint Framework 构建你的第一个web部件(一)
    SharePoint Framework 配置你的SharePoint客户端web部件开发环境
    SharePoint Framework 配置Office 365开发者租户
    SharePoint Framework 开发工具和库
    SharePoint Framework 概述
  • 原文地址:https://www.cnblogs.com/sky7034/p/2140188.html
Copyright © 2020-2023  润新知