Android提高十八篇之自定义Menu(TabMenu)
本文来自http://blog.****.net/hellogv/,引用必须注明出处!
快要过年了,在这里先祝广大的技术宅兔年快乐!
用过UCWEB-Android版的人都应该对其特殊的menu有印象,把menu做成Tab-Menu(支持分页的Menu),可以容纳比Android传统的menu更丰富的内容(Android的menu超过6项则缩略在[更多]里),本文参考网上的例子(作者:CoffeeCole,email:[email protected]),对例子进行简化以及封装,使其作为一个复合控件融入自己的framework。
先来看看本文程序运行的效果:
TabMenu本身就是一个PopupWindow,PopupWindow上面放了两个GridView,第一个GridView就是分页标签,位于PopupWindow的顶部,第二个GridView是菜单,位于PopupWindow的主体。为了实现PopupWindow的弹出/退出的动画效果,本文使用了以下代码:
在工程的res文件夹里添加anim子目录,再新建文件popup_enter.xml:
- <?xmlversion="1.0"encoding="utf-8"?>
- <setxmlns:android="http://schemas.android.com/apk/res/android">
- <translateandroid:fromYDelta="100%p"android:toYDelta="0"android:duration="1000"/>
- <alphaandroid:fromAlpha="0.0"android:toAlpha="1.0"android:duration="1000"/>
- </set>
新建文件popup_exit.xml:
- <?xmlversion="1.0"encoding="utf-8"?>
- <setxmlns:android="http://schemas.android.com/apk/res/android">
- <translateandroid:fromYDelta="0"android:toYDelta="100%p"android:duration="1000"/>
- <alphaandroid:fromAlpha="1.0"android:toAlpha="0.0"android:duration="1000"/>
- </set>
在工程的values文件夹里新建文件popup_animation.xml:
<?xml
version="1.0" encoding="utf-8"?>
<resources>
<style name="PopupAnimation" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/popup_enter</item>
<item name="android:windowExitAnimation">@anim/popup_exit</item>
</style>
</resources>
main.xml的源码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutandroid:id="@+id/LinearLayout01"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextViewandroid:id="@+id/TextView01"android:layout_height="wrap_content"
- android:layout_width="fill_parent"android:text="扩展Menu----hellogv"></TextView>
- </LinearLayout>
TabMenu的封装类TabMenu.java的源码如下:
- packagecom.testTabMenu;
- importandroid.content.Context;
- importandroid.graphics.Color;
- importandroid.graphics.drawable.ColorDrawable;
- importandroid.view.Gravity;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.BaseAdapter;
- importandroid.widget.GridView;
- importandroid.widget.ImageView;
- importandroid.widget.LinearLayout;
- importandroid.widget.PopupWindow;
- importandroid.widget.TextView;
- importandroid.widget.AdapterView.OnItemClickListener;
- importandroid.widget.LinearLayout.LayoutParams;
- publicclassTabMenuextendsPopupWindow{
- privateGridViewgvBody,gvTitle;
- privateLinearLayoutmLayout;
- privateMenuTitleAdaptertitleAdapter;
- publicTabMenu(Contextcontext,OnItemClickListenertitleClick,OnItemClickListenerbodyClick,
- MenuTitleAdaptertitleAdapter,intcolorBgTabMenu,intaniTabMenu){
- super(context);
- mLayout=newLinearLayout(context);
- mLayout.setOrientation(LinearLayout.VERTICAL);
- //标题选项栏
- gvTitle=newGridView(context);
- gvTitle.setLayoutParams(newLayoutParams(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(newColorDrawable(Color.TRANSPARENT));//选中的时候为透明色
- this.titleAdapter=titleAdapter;
- //子选项栏
- gvBody=newGridView(context);
- gvBody.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
- gvBody.setSelector(newColorDrawable(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(newColorDrawable(colorBgTabMenu));//设置TabMenu菜单背景
- this.setAnimationStyle(aniTabMenu);
- this.setFocusable(true);//menu菜单获得焦点如果没有获得焦点menu菜单中的控件事件无法响应
- }
- publicvoidSetTitleSelect(intindex)
- {
- gvTitle.setSelection(index);
- this.titleAdapter.SetFocus(index);
- }
- publicvoidSetBodySelect(intindex,intcolorSelBody)
- {
- intcount=gvBody.getChildCount();
- for(inti=0;i<count;i++)
- {
- if(i!=index)
- ((LinearLayout)gvBody.getChildAt(i)).setBackgroundColor(Color.TRANSPARENT);
- }
- ((LinearLayout)gvBody.getChildAt(index)).setBackgroundColor(colorSelBody);
- }
- publicvoidSetBodyAdapter(MenuBodyAdapterbodyAdapter)
- {
- gvBody.setAdapter(bodyAdapter);
- }
- /**
- *自定义Adapter,TabMenu的每个分页的主体
- *
- */
- staticpublicclassMenuBodyAdapterextendsBaseAdapter{
- privateContextmContext;
- privateintfontColor,fontSize;
- privateString[]texts;
- privateint[]resID;
- /**
- *设置TabMenu的分页主体
- *@paramcontext调用方的上下文
- *@paramtexts按钮集合的字符串数组
- *@paramresID按钮集合的图标资源数组
- *@paramfontSize按钮字体大小
- *@paramcolor按钮字体颜色
- */
- publicMenuBodyAdapter(Contextcontext,String[]texts,int[]resID,intfontSize,intfontColor)
- {
- this.mContext=context;
- this.fontColor=fontColor;
- this.texts=texts;
- this.fontSize=fontSize;
- this.resID=resID;
- }
- publicintgetCount(){
- returntexts.length;
- }
- publicObjectgetItem(intposition){
- returnmakeMenyBody(position);
- }
- publiclonggetItemId(intposition){
- returnposition;
- }
- privateLinearLayoutmakeMenyBody(intposition)
- {
- LinearLayoutresult=newLinearLayout(this.mContext);
- result.setOrientation(LinearLayout.VERTICAL);
- result.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
- result.setPadding(10,10,10,10);
- TextViewtext=newTextView(this.mContext);
- text.setText(texts[position]);
- text.setTextSize(fontSize);
- text.setTextColor(fontColor);
- text.setGravity(Gravity.CENTER);
- text.setPadding(5,5,5,5);
- ImageViewimg=newImageView(this.mContext);
- img.setBackgroundResource(resID[position]);
- result.addView(img,newLinearLayout.LayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)));
- result.addView(text);
- returnresult;
- }
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- returnmakeMenyBody(position);
- }
- }
- /**
- *自定义Adapter,TabMenu的分页标签部分
- *
- */
- staticpublicclassMenuTitleAdapterextendsBaseAdapter{
- privateContextmContext;
- privateintfontColor,unselcolor,selcolor;
- privateTextView[]title;
- /**
- *设置TabMenu的title
- *@paramcontext调用方的上下文
- *@paramtitles分页标签的字符串数组
- *@paramfontSize字体大小
- *@paramfontcolor字体颜色
- *@paramunselcolor未选中项的背景色
- *@paramselcolor选中项的背景色
- */
- publicMenuTitleAdapter(Contextcontext,String[]titles,intfontSize,
- intfontcolor,intunselcolor,intselcolor){
- this.mContext=context;
- this.fontColor=fontcolor;
- this.unselcolor=unselcolor;
- this.selcolor=selcolor;
- this.title=newTextView[titles.length];
- for(inti=0;i<titles.length;i++){
- title[i]=newTextView(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);
- }
- }
- publicintgetCount(){
- returntitle.length;
- }
- publicObjectgetItem(intposition){
- returntitle[position];
- }
- publiclonggetItemId(intposition){
- returntitle[position].getId();
- }
- /**
- *设置选中的效果
- */
- privatevoidSetFocus(intindex)
- {
- for(inti=0;i<title.length;i++)
- {
- if(i!=index)
- {
- title[i].setBackgroundDrawable(newColorDrawable(unselcolor));//设置没选中的颜色
- title[i].setTextColor(fontColor);//设置没选中项的字体颜色
- }
- }
- title[index].setBackgroundColor(0x00);//设置选中项的颜色
- title[index].setTextColor(selcolor);//设置选中项的字体颜色
- }
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- Viewv;
- if(convertView==null){
- v=title[position];
- }else{
- v=convertView;
- }
- returnv;
- }
- }
- }
testTabMenu介绍了数据的定义以及TabMenu的使用,源码如下:
- packagecom.testTabMenu;
- importandroid.app.Activity;
- importandroid.graphics.Color;
- importandroid.os.Bundle;
- importandroid.view.Gravity;
- importandroid.view.Menu;
- importandroid.view.View;
- importandroid.widget.AdapterView;
- importandroid.widget.AdapterView.OnItemClickListener;
- importandroid.widget.Toast;
- publicclasstestTabMenuextendsActivity{
- TabMenu.MenuBodyAdapter[]bodyAdapter=newTabMenu.MenuBodyAdapter[3];
- TabMenu.MenuTitleAdaptertitleAdapter;
- TabMenutabMenu;
- intselTitle=0;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //设置分页栏的标题
- titleAdapter=newTabMenu.MenuTitleAdapter(this,newString[]{"常用",
- "设置","工具"},16,0xFF222222,Color.LTGRAY,Color.WHITE);
- //定义每项分页栏的内容
- bodyAdapter[0]=newTabMenu.MenuBodyAdapter(this,newString[]{"常用1","常用2",},
- newint[]{R.drawable.menu_test,
- R.drawable.menu_bookmark},13,0xFFFFFFFF);
- bodyAdapter[1]=newTabMenu.MenuBodyAdapter(this,newString[]{"设置1","设置2",
- "设置3"},newint[]{R.drawable.menu_edit,
- R.drawable.menu_delete,R.drawable.menu_fullscreen},13,0xFFFFFFFF);
- bodyAdapter[2]=newTabMenu.MenuBodyAdapter(this,newString[]{"工具1","工具2",
- "工具3","工具4"},newint[]{R.drawable.menu_copy,
- R.drawable.menu_cut,R.drawable.menu_normalmode,
- R.drawable.menu_quit},13,0xFFFFFFFF);
- tabMenu=newTabMenu(this,
- newTitleClickEvent(),
- newBodyClickEvent(),
- titleAdapter,
- 0x55123456,//TabMenu的背景颜色
- R.style.PopupAnimation);//出现与消失的动画
- tabMenu.update();
- tabMenu.SetTitleSelect(0);
- tabMenu.SetBodyAdapter(bodyAdapter[0]);
- }
- classTitleClickEventimplementsOnItemClickListener{
- @Override
- publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
- longarg3){
- selTitle=arg2;
- tabMenu.SetTitleSelect(arg2);
- tabMenu.SetBodyAdapter(bodyAdapter[arg2]);
- }
- }
- classBodyClickEventimplementsOnItemClickListener{
- @Override
- publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
- longarg3){
- tabMenu.SetBodySelect(arg2,Color.GRAY);
- Stringstr="第"+String.valueOf(selTitle)+"栏/n/r"
- +"第"+String.valueOf(arg2)+"项";
- Toast.makeText(testTabMenu.this,str,500).show();
- }
- }
- @Override
- /**
- *创建MENU
- */
- publicbooleanonCreateOptionsMenu(Menumenu){
- menu.add("menu");//必须创建一项
- returnsuper.onCreateOptionsMenu(menu);
- }
- @Override
- /**
- *拦截MENU
- */
- publicbooleanonMenuOpened(intfeatureId,Menumenu){
- if(tabMenu!=null){
- if(tabMenu.isShowing())
- tabMenu.dismiss();
- else{
- tabMenu.showAtLocation(findViewById(R.id.LinearLayout01),
- Gravity.BOTTOM,0,0);
- }
- }
- returnfalse;//返回为true则显示系统menu
- }
- }
本文来自http://blog.****.net/hellogv/,引用必须注明出处!
快要过年了,在这里先祝广大的技术宅兔年快乐!
用过UCWEB-Android版的人都应该对其特殊的menu有印象,把menu做成Tab-Menu(支持分页的Menu),可以容纳比Android传统的menu更丰富的内容(Android的menu超过6项则缩略在[更多]里),本文参考网上的例子(作者:CoffeeCole,email:[email protected]),对例子进行简化以及封装,使其作为一个复合控件融入自己的framework。
先来看看本文程序运行的效果:
TabMenu本身就是一个PopupWindow,PopupWindow上面放了两个GridView,第一个GridView就是分页标签,位于PopupWindow的顶部,第二个GridView是菜单,位于PopupWindow的主体。为了实现PopupWindow的弹出/退出的动画效果,本文使用了以下代码:
在工程的res文件夹里添加anim子目录,再新建文件popup_enter.xml:
- <?xmlversion="1.0"encoding="utf-8"?>
- <setxmlns:android="http://schemas.android.com/apk/res/android">
- <translateandroid:fromYDelta="100%p"android:toYDelta="0"android:duration="1000"/>
- <alphaandroid:fromAlpha="0.0"android:toAlpha="1.0"android:duration="1000"/>
- </set>
新建文件popup_exit.xml:
- <?xmlversion="1.0"encoding="utf-8"?>
- <setxmlns:android="http://schemas.android.com/apk/res/android">
- <translateandroid:fromYDelta="0"android:toYDelta="100%p"android:duration="1000"/>
- <alphaandroid:fromAlpha="1.0"android:toAlpha="0.0"android:duration="1000"/>
- </set>
在工程的values文件夹里新建文件popup_animation.xml:
<?xml
version="1.0" encoding="utf-8"?>
<resources>
<style name="PopupAnimation" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/popup_enter</item>
<item name="android:windowExitAnimation">@anim/popup_exit</item>
</style>
</resources>
main.xml的源码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutandroid:id="@+id/LinearLayout01"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextViewandroid:id="@+id/TextView01"android:layout_height="wrap_content"
- android:layout_width="fill_parent"android:text="扩展Menu----hellogv"></TextView>
- </LinearLayout>
TabMenu的封装类TabMenu.java的源码如下:
- packagecom.testTabMenu;
- importandroid.content.Context;
- importandroid.graphics.Color;
- importandroid.graphics.drawable.ColorDrawable;
- importandroid.view.Gravity;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.BaseAdapter;
- importandroid.widget.GridView;
- importandroid.widget.ImageView;
- importandroid.widget.LinearLayout;
- importandroid.widget.PopupWindow;
- importandroid.widget.TextView;
- importandroid.widget.AdapterView.OnItemClickListener;
- importandroid.widget.LinearLayout.LayoutParams;
- publicclassTabMenuextendsPopupWindow{
- privateGridViewgvBody,gvTitle;
- privateLinearLayoutmLayout;
- privateMenuTitleAdaptertitleAdapter;
- publicTabMenu(Contextcontext,OnItemClickListenertitleClick,OnItemClickListenerbodyClick,
- MenuTitleAdaptertitleAdapter,intcolorBgTabMenu,intaniTabMenu){
- super(context);
- mLayout=newLinearLayout(context);
- mLayout.setOrientation(LinearLayout.VERTICAL);
- //标题选项栏
- gvTitle=newGridView(context);
- gvTitle.setLayoutParams(newLayoutParams(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(newColorDrawable(Color.TRANSPARENT));//选中的时候为透明色
- this.titleAdapter=titleAdapter;
- //子选项栏
- gvBody=newGridView(context);
- gvBody.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
- gvBody.setSelector(newColorDrawable(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(newColorDrawable(colorBgTabMenu));//设置TabMenu菜单背景
- this.setAnimationStyle(aniTabMenu);
- this.setFocusable(true);//menu菜单获得焦点如果没有获得焦点menu菜单中的控件事件无法响应
- }
- publicvoidSetTitleSelect(intindex)
- {
- gvTitle.setSelection(index);
- this.titleAdapter.SetFocus(index);
- }
- publicvoidSetBodySelect(intindex,intcolorSelBody)
- {
- intcount=gvBody.getChildCount();
- for(inti=0;i<count;i++)
- {
- if(i!=index)
- ((LinearLayout)gvBody.getChildAt(i)).setBackgroundColor(Color.TRANSPARENT);
- }
- ((LinearLayout)gvBody.getChildAt(index)).setBackgroundColor(colorSelBody);
- }
- publicvoidSetBodyAdapter(MenuBodyAdapterbodyAdapter)
- {
- gvBody.setAdapter(bodyAdapter);
- }
- /**
- *自定义Adapter,TabMenu的每个分页的主体
- *
- */
- staticpublicclassMenuBodyAdapterextendsBaseAdapter{
- privateContextmContext;
- privateintfontColor,fontSize;
- privateString[]texts;
- privateint[]resID;
- /**
- *设置TabMenu的分页主体
- *@paramcontext调用方的上下文
- *@paramtexts按钮集合的字符串数组
- *@paramresID按钮集合的图标资源数组
- *@paramfontSize按钮字体大小
- *@paramcolor按钮字体颜色
- */
- publicMenuBodyAdapter(Contextcontext,String[]texts,int[]resID,intfontSize,intfontColor)
- {
- this.mContext=context;
- this.fontColor=fontColor;
- this.texts=texts;
- this.fontSize=fontSize;
- this.resID=resID;
- }
- publicintgetCount(){
- returntexts.length;
- }
- publicObjectgetItem(intposition){
- returnmakeMenyBody(position);
- }
- publiclonggetItemId(intposition){
- returnposition;
- }
- privateLinearLayoutmakeMenyBody(intposition)
- {
- LinearLayoutresult=newLinearLayout(this.mContext);
- result.setOrientation(LinearLayout.VERTICAL);
- result.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
- result.setPadding(10,10,10,10);
- TextViewtext=newTextView(this.mContext);
- text.setText(texts[position]);
- text.setTextSize(fontSize);
- text.setTextColor(fontColor);
- text.setGravity(Gravity.CENTER);
- text.setPadding(5,5,5,5);
- ImageViewimg=newImageView(this.mContext);
- img.setBackgroundResource(resID[position]);
- result.addView(img,newLinearLayout.LayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)));
- result.addView(text);
- returnresult;
- }
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- returnmakeMenyBody(position);
- }
- }
- /**
- *自定义Adapter,TabMenu的分页标签部分
- *
- */
- staticpublicclassMenuTitleAdapterextendsBaseAdapter{
- privateContextmContext;
- privateintfontColor,unselcolor,selcolor;
- privateTextView[]title;
- /**
- *设置TabMenu的title
- *@paramcontext调用方的上下文
- *@paramtitles分页标签的字符串数组
- *@paramfontSize字体大小
- *@paramfontcolor字体颜色
- *@paramunselcolor未选中项的背景色
- *@paramselcolor选中项的背景色
- */
- publicMenuTitleAdapter(Contextcontext,String[]titles,intfontSize,
- intfontcolor,intunselcolor,intselcolor){
- this.mContext=context;
- this.fontColor=fontcolor;
- this.unselcolor=unselcolor;
- this.selcolor=selcolor;
- this.title=newTextView[titles.length];
- for(inti=0;i<titles.length;i++){
- title[i]=newTextView(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);
- }
- }
- publicintgetCount(){
- returntitle.length;
- }
- publicObjectgetItem(intposition){
- returntitle[position];
- }
- publiclonggetItemId(intposition){
- returntitle[position].getId();
- }
- /**
- *设置选中的效果
- */
- privatevoidSetFocus(intindex)
- {
- for(inti=0;i<title.length;i++)
- {
- if(i!=index)
- {
- title[i].setBackgroundDrawable(newColorDrawable(unselcolor));//设置没选中的颜色
- title[i].setTextColor(fontColor);//设置没选中项的字体颜色
- }
- }
- title[index].setBackgroundColor(0x00);//设置选中项的颜色
- title[index].setTextColor(selcolor);//设置选中项的字体颜色
- }
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- Viewv;
- if(convertView==null){
- v=title[position];
- }else{
- v=convertView;
- }
- returnv;
- }
- }
- }
testTabMenu介绍了数据的定义以及TabMenu的使用,源码如下:
- packagecom.testTabMenu;
- importandroid.app.Activity;
- importandroid.graphics.Color;
- importandroid.os.Bundle;
- importandroid.view.Gravity;
- importandroid.view.Menu;
- importandroid.view.View;
- importandroid.widget.AdapterView;
- importandroid.widget.AdapterView.OnItemClickListener;
- importandroid.widget.Toast;
- publicclasstestTabMenuextendsActivity{
- TabMenu.MenuBodyAdapter[]bodyAdapter=newTabMenu.MenuBodyAdapter[3];
- TabMenu.MenuTitleAdaptertitleAdapter;
- TabMenutabMenu;
- intselTitle=0;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //设置分页栏的标题
- titleAdapter=newTabMenu.MenuTitleAdapter(this,newString[]{"常用",
- "设置","工具"},16,0xFF222222,Color.LTGRAY,Color.WHITE);
- //定义每项分页栏的内容
- bodyAdapter[0]=newTabMenu.MenuBodyAdapter(this,newString[]{"常用1","常用2",},
- newint[]{R.drawable.menu_test,
- R.drawable.menu_bookmark},13,0xFFFFFFFF);
- bodyAdapter[1]=newTabMenu.MenuBodyAdapter(this,newString[]{"设置1","设置2",
- "设置3"},newint[]{R.drawable.menu_edit,
- R.drawable.menu_delete,R.drawable.menu_fullscreen},13,0xFFFFFFFF);
- bodyAdapter[2]=newTabMenu.MenuBodyAdapter(this,newString[]{"工具1","工具2",
- "工具3","工具4"},newint[]{R.drawable.menu_copy,
- R.drawable.menu_cut,R.drawable.menu_normalmode,
- R.drawable.menu_quit},13,0xFFFFFFFF);
- tabMenu=newTabMenu(this,
- newTitleClickEvent(),
- newBodyClickEvent(),
- titleAdapter,
- 0x55123456,//TabMenu的背景颜色
- R.style.PopupAnimation);//出现与消失的动画
- tabMenu.update();
- tabMenu.SetTitleSelect(0);
- tabMenu.SetBodyAdapter(bodyAdapter[0]);
- }
- classTitleClickEventimplementsOnItemClickListener{
- @Override
- publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
- longarg3){
- selTitle=arg2;
- tabMenu.SetTitleSelect(arg2);
- tabMenu.SetBodyAdapter(bodyAdapter[arg2]);
- }
- }
- classBodyClickEventimplementsOnItemClickListener{
- @Override
- publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
- longarg3){
- tabMenu.SetBodySelect(arg2,Color.GRAY);
- Stringstr="第"+String.valueOf(selTitle)+"栏/n/r"
- +"第"+String.valueOf(arg2)+"项";
- Toast.makeText(testTabMenu.this,str,500).show();
- }
- }
- @Override
- /**
- *创建MENU
- */
- publicbooleanonCreateOptionsMenu(Menumenu){
- menu.add("menu");//必须创建一项
- returnsuper.onCreateOptionsMenu(menu);
- }
- @Override
- /**
- *拦截MENU
- */
- publicbooleanonMenuOpened(intfeatureId,Menumenu){
- if(tabMenu!=null){
- if(tabMenu.isShowing())
- tabMenu.dismiss();
- else{
- tabMenu.showAtLocation(findViewById(R.id.LinearLayout01),
- Gravity.BOTTOM,0,0);
- }
- }
- returnfalse;//返回为true则显示系统menu
- }
- }