文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android实现扩展Menu的方法

2022-06-06 09:48

关注

本文实例讲述了Android实现扩展Menu的方法。分享给大家供大家参考。具体如下:

1. java代码:


package com.tabmenu;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;

public class TabMenu extends PopupWindow{
 private GridView gvBody, gvTitle;
 private LinearLayout mLayout;
 private MenuTitleAdapter titleAdapter;
 public TabMenu(Context context,OnItemClickListener titleClick,OnItemClickListener bodyClick,
   MenuTitleAdapter titleAdapter,int colorBgTabMenu,int aniTabMenu){
  super(context);
  mLayout = new LinearLayout(context);
  mLayout.setOrientation(LinearLayout.VERTICAL);
  //选项栏
  gvTitle = new GridView(context);
  gvTitle.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  gvTitle.setNumColumns(titleAdapter.getCount());
  gvTitle.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
  gvTitle.setVerticalSpacing(1);
  gvTitle.setHorizontalSpacing(1);
  gvTitle.setGravity(Gravity.CENTER);
  gvTitle.setOnItemClickListener(titleClick);
  gvTitle.setAdapter(titleAdapter);
  gvTitle.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的时候为透明色
  this.titleAdapter=titleAdapter;
  //子选项栏
  gvBody = new GridView(context);
  gvBody.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
  gvBody.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的时候为透明色
  gvBody.setNumColumns(4);
  gvBody.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
  gvBody.setVerticalSpacing(10);
  gvBody.setHorizontalSpacing(10);
  gvBody.setPadding(10, 10, 10, 10);
  gvBody.setGravity(Gravity.CENTER);
  gvBody.setOnItemClickListener(bodyClick);
  mLayout.addView(gvTitle);
  mLayout.addView(gvBody);
  //设置默认项
  this.setContentView(mLayout);
  this.setWidth(LayoutParams.FILL_PARENT);
  this.setHeight(LayoutParams.WRAP_CONTENT);
  this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));// 设置TabMenu菜单背景
  this.setAnimationStyle(aniTabMenu);
  this.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应
 }
 public void SetTitleSelect(int index)
 {
  gvTitle.setSelection(index);
  this.titleAdapter.SetFocus(index);
 }
 public void SetBodySelect(int index,int colorSelBody)
 {
  int count=gvBody.getChildCount();
  for(int i=0;i<count;i++)
  {
   if(i!=index)
    ((LinearLayout)gvBody.getChildAt(i)).setBackgroundColor(Color.TRANSPARENT);
  }
  ((LinearLayout)gvBody.getChildAt(index)).setBackgroundColor(colorSelBody);
 }
 public void SetBodyAdapter(MenuBodyAdapter bodyAdapter)
 {
  gvBody.setAdapter(bodyAdapter);
 }
 
 static public class MenuBodyAdapter extends BaseAdapter {
  private Context mContext;
  private int fontColor,fontSize;
  private String[] texts;
  private int[] resID;
  
  public MenuBodyAdapter(Context context, String[] texts,int[] resID, int fontSize,int fontColor) 
  {
   this.mContext = context;
   this.fontColor = fontColor;
   this.texts = texts;
   this.fontSize=fontSize;
   this.resID=resID;
  }
  public int getCount() {
   return texts.length;
  }
  public Object getItem(int position) {
   return makeMenyBody(position);
  }
  public long getItemId(int position) {
   return position;
  }
  private LinearLayout makeMenyBody(int position)
  {
   LinearLayout result=new LinearLayout(this.mContext);
   result.setOrientation(LinearLayout.VERTICAL);
   result.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL); 
   result.setPadding(10, 10, 10, 10);
   TextView text = new TextView(this.mContext);
   text.setText(texts[position]);
   text.setTextSize(fontSize);
   text.setTextColor(fontColor);
   text.setGravity(Gravity.CENTER);
   text.setPadding(5, 5, 5, 5);
   ImageView img=new ImageView(this.mContext);
   img.setBackgroundResource(resID[position]);
   result.addView(img,new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)));
   result.addView(text);
   return result;
  }
  public View getView(int position, View convertView, ViewGroup parent) {
   return makeMenyBody(position);
  }
 }
 
 static public class MenuTitleAdapter extends BaseAdapter {
  private Context mContext;
  private int fontColor,unselcolor,selcolor;
  private TextView[] title;
  
  public MenuTitleAdapter(Context context, String[] titles, int fontSize,
    int fontcolor,int unselcolor,int selcolor) {
   this.mContext = context;
   this.fontColor = fontcolor;
   this.unselcolor = unselcolor;
   this.selcolor=selcolor;
   this.title = new TextView[titles.length];
   for (int i = 0; i < titles.length; i++) {
    title[i] = new TextView(mContext);
    title[i].setText(titles[i]);
    title[i].setTextSize(fontSize);
    title[i].setTextColor(fontColor);
    title[i].setGravity(Gravity.CENTER);
    title[i].setPadding(10, 10, 10, 10);
   }
  }
  public int getCount() {
   return title.length;
  }
  public Object getItem(int position) {
   return title[position];
  }
  public long getItemId(int position) {
   return title[position].getId();
  }
  
  private void SetFocus(int index)
  {
   for(int i=0;i<title.length;i++)
   {
    if(i!=index)
    {
     title[i].setBackgroundDrawable(new ColorDrawable(unselcolor));//设置没选中的颜色
     title[i].setTextColor(fontColor);//设置没选中项的字体颜色
    }
   }
   title[index].setBackgroundColor(0x00);//设置选中项的颜色
   title[index].setTextColor(selcolor);//设置选中项的字体颜色
  }
  public View getView(int position, View convertView, ViewGroup parent) {
   View v;
   if (convertView == null) {
    v = title[position];
   } else {
    v = convertView;
   }
   return v;
  }
 }
}

2. java代码:


package com.tabmenu;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class Activity01 extends Activity {
 TabMenu.MenuBodyAdapter[] bodyAdapter=new TabMenu.MenuBodyAdapter[3];
 TabMenu.MenuTitleAdapter titleAdapter;
 TabMenu tabMenu;
 int selTitle=0;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  //设置分页栏的
  titleAdapter = new TabMenu.MenuTitleAdapter(this, new String[] { "常用",
    "设置", "工具" }, 16, 0xFF222222,Color.LTGRAY,Color.WHITE);
  //定义每项分页栏的内容
  bodyAdapter[0]=new TabMenu.MenuBodyAdapter(this,new String[] { "常用1", "常用2", }, 
     new int[] { android.R.drawable.alert_dark_frame,
    android.R.drawable.ic_delete},13, 0xFFFFFFFF);
  bodyAdapter[1]=new TabMenu.MenuBodyAdapter(this,new String[] { "设置1", "设置2",
     "设置3"}, new int[] { android.R.drawable.ic_menu_edit,
     android.R.drawable.btn_default,android.R.drawable.btn_dropdown},13, 0xFFFFFFFF);
  bodyAdapter[2]=new TabMenu.MenuBodyAdapter(this,new String[] { "工具1", "工具2",
     "工具3", "工具4" }, new int[] { android.R.drawable.ic_media_ff,
    android.R.drawable.ic_menu_delete, android.R.drawable.ic_btn_speak_now,
     android.R.drawable.edit_text },13, 0xFFFFFFFF);
  tabMenu=new TabMenu(this,
     new TitleClickEvent(),
     new BodyClickEvent(),
     titleAdapter,
     0x55123456,//TabMenu的背景颜色
     R.style.PopupAnimation);//出现与消失的动画
   tabMenu.update();
   tabMenu.SetTitleSelect(0);
   tabMenu.SetBodyAdapter(bodyAdapter[0]);
 }
 class TitleClickEvent implements OnItemClickListener{
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
   selTitle=arg2;
   tabMenu.SetTitleSelect(arg2);
   tabMenu.SetBodyAdapter(bodyAdapter[arg2]);
  }
 }
 class BodyClickEvent implements OnItemClickListener{
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
   tabMenu.SetBodySelect(arg2,Color.GRAY);
   String str="第"+String.valueOf(selTitle)+"栏/n/r"
   +"第"+String.valueOf(arg2)+"项";
   Toast.makeText(Activity01.this, str, 500).show();
  }
 }
 @Override
 
 public boolean onCreateOptionsMenu(Menu menu) {
  menu.add("menu");// 必须创建一项
  return super.onCreateOptionsMenu(menu);
 }
 @Override
 
 public boolean onMenuOpened(int featureId, Menu menu) {
  if (tabMenu != null) {
   if (tabMenu.isShowing())
    tabMenu.dismiss();
   else {
    tabMenu.showAtLocation(findViewById(R.id.LinearLayout01),
      Gravity.BOTTOM, 0, 0);
   }
  }
  return false;// 返回为true 则显示系统menu
 }
}

3. 运行结果:

希望本文所述对大家的Android程序设计有所帮助。

您可能感兴趣的文章:Android中捕捉menu按键点击事件的方法Android中ActionBar以及menu的代码设置样式Android仿微信菜单(Menu)(使用C#和Java分别实现)android编程之menu按键功能实现方法Android创建Menu菜单实例Android学习笔记——Menu介绍(三)Android学习笔记——Menu介绍(二)Android学习笔记——Menu介绍(一)Android提高之自定义Menu(TabMenu)实现方法Android动态添加menu菜单的简单方法


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯