当前位置:操作系统 > 安卓/Android >>

Android自定义Menu(菜单)

1
新建自定义Menu————>TabMenu.java如下:
001
package com.ncw;
002
 
003
import android.content.Context;
004
import android.graphics.Color;
005
import android.graphics.drawable.ColorDrawable;
006
import android.view.Gravity;
007
import android.view.View;
008
import android.view.ViewGroup;
009
import android.widget.BaseAdapter;
010
import android.widget.GridView;
011
import android.widget.ImageView;
012
import android.widget.LinearLayout;
013
import android.widget.PopupWindow;
014
import android.widget.AdapterView.OnItemClickListener;
015
import android.widget.LinearLayout.LayoutParams;
016
 
017
public class TabMenu extends PopupWindow {
018
    private GridView gridView;
019
    private LinearLayout mLayout;
020
 
021
    public TabMenu(Context context, OnItemClickListener bodyClick, int colorBgTabMenu) {
022
        super(context);
023
 
024
        mLayout = new LinearLayout(context);
025
        mLayout.setOrientation(LinearLayout.VERTICAL);
026
 
027
        gridView = new GridView(context);
028
        gridView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
029
                LayoutParams.WRAP_CONTENT));
030
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));// 选中的时候为透明色
031
        gridView.setNumColumns(4);
032
        gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
033
        gridView.setVerticalSpacing(0);
034
        gridView.setHorizontalSpacing(0);
035
        gridView.setPadding(0, 0, 0, 0);
036
        gridView.setGravity(Gravity.CENTER);
037
        gridView.setOnItemClickListener(bodyClick);
038
        mLayout.addView(gridView);
039
 
040
        // 设置默认项
041
        this.setContentView(mLayout);
042
        this.setWidth(LayoutParams.FILL_PARENT);
043
        this.setHeight(LayoutParams.WRAP_CONTENT);
044
        this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));// 设置TabMenu菜单背景
045
        this.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应
046
    }
047
 
048
    public void SetBodySelect(int index, int colorSelBody) {
049
        int count = gridView.getChildCount();
050
        for (int i = 0; i < count; i++) {
051
            if (i != index)
052
                ((LinearLayout) gridView.getChildAt(i))
053
                        .setBackgroundColor(Color.TRANSPARENT);
054
        }
055
        ((LinearLayout) gridView.getChildAt(index))
056
                .setBackgroundColor(colorSelBody);
057
    }
058
 
059
    public void SetBodyAdapter(MenuBodyAdapter bodyAdapter) {
060
        gridView.setAdapter(bodyAdapter);
061
    }
062
 
063
    /**
064
     * 自定义Adapter,TabMenu的每个分页的主体
065
     *
066
     */
067
    static public class MenuBodyAdapter extends BaseAdapter {
068
        private Context mContext;
069
        private int[] resID;
070
 
071
        /**
072
         * 设置TabMenu的分页主体
073
         *
074
         * @param context
075
         *            调用方的上下文
076
         * @param resID
077
         */
078
        public MenuBodyAdapter(Context context, int[] resID) {
079
            this.mContext = context;
080
            this.resID = resID;
081
        }
082
 
083
        @Override
084
        public int getCount() {
085
            return resID.length;
086
        }
087
 
088
        public Object getItem(int position) {
089
 
090
            return makeMenyBody(position);
091
        }
092
 
093
        public long getItemId(int position) {
094
            return position;
095
        }
096
 
097
        private LinearLayout makeMenyBody(int position) {
098
            LinearLayout result = new LinearLayout(this.mContext);
099
            result.setOrientation(LinearLayout.VERTICAL);
100
            result.setGravity(Gravity.CENTER_HORIZONTAL
101
                    | Gravity.CENTER_VERTICAL);
102
           
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,