欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现模仿UCweb菜单效果的方法

程序员文章站 2023-12-04 19:42:04
本文实例讲述了android实现模仿ucweb菜单效果的方法。分享给大家供大家参考。具体如下: ucweb的菜单看起来不错,自己模仿做一个,思路实现如下: 1、保留me...

本文实例讲述了android实现模仿ucweb菜单效果的方法。分享给大家供大家参考。具体如下:

ucweb的菜单看起来不错,自己模仿做一个,思路实现如下:

1、保留menu按键作用
2、用popupwindow作为菜单显示容器
3、用gridview显示所有子菜单

代码如下:

1、布局文件:

popupwindow.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:background="#50000000"
 android:layout_height="fill_parent"
 android:gravity="bottom|center_horizontal">
 <linearlayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="320dip"
  android:background="@drawable/bkg2"
  android:id="@+id/popdialog"
  android:layout_height="250dip"
  android:gravity="center_vertical|center_horizontal"
  >
  <gridview
   android:id="@+id/gridview"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:numcolumns="4"
   android:verticalspacing="10dip"
   android:horizontalspacing="10dip"
   android:stretchmode="columnwidth"
   android:gravity="center"
   />
 </linearlayout>
</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="5dip">
 <imageview android:id="@+id/item_image"
  android:layout_centerhorizontal="true" 
  android:layout_width="40dip"
  android:layout_height="40dip">
  </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" 
  style="@style/text.location"
  android:text="选项"></textview>
</relativelayout>

2、用popupwindow作为菜单显示容器:

view view = this.getlayoutinflater().inflate(r.layout.popwindowdemo, null);
pop = new popupwindow(view,320,450);//大小设置为全屏幕,这里硬编码的,可修改
pop.setoutsidetouchable(false);
pop.setbackgrounddrawable(new bitmapdrawable());
pop.setfocusable(true);//如果不加这个,grid不会响应itemclick
pop.settouchinterceptor(new ontouchlistener() {
public boolean ontouch(view v, motionevent event) {
 // todo auto-generated method stub
 if (event.gety()<240){ //这里处理,当点击gridview以外区域的时候,菜单关闭
  if (pop.isshowing())
   pop.dismiss();
 }
 log.d("demo", "popupwindow::ontouch >>> view: "
   + v + ", event: " + event);
 return false;
}
});

3、初始化gridview:

/** 菜单图片 **/
int[] menu_image_array = { r.drawable.menu_search,
   r.drawable.menu_filemanager, r.drawable.menu_downmanager,
   r.drawable.menu_fullscreen, r.drawable.menu_inputurl,
   r.drawable.menu_bookmark, r.drawable.menu_bookmark_sync_import,
   r.drawable.menu_sharepage, r.drawable.menu_quit,
   r.drawable.menu_nightmode, r.drawable.menu_refresh,
   r.drawable.menu_more };
/** 菜单文字 **/
string[] menu_name_array = { "搜索", "文件管理", "下载管理", "全屏", "网址", "书签",
 "加入书签", "分享页面", "退出", "夜间模式", "刷新", "更多" };
/**
* 构造菜单adapter
* 
* @param menunamearray
*   名称
* @param imageresourcearray
*   图片
* @return simpleadapter
*/
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;
}
menugrid = (gridview) view.findviewbyid(r.id.gridview);  
menugrid.setadapter(getmenuadapter(menu_name_array, menu_image_array));

4、注册menu弹起事件:

@override
public boolean oncreateoptionsmenu(menu menu) {
 // todo auto-generated method stub
 pop.showatlocation(findviewbyid(r.id.mainfrm), gravity.center|gravity.bottom, 0, 0);
 return false;//super.oncreateoptionsmenu(menu); 
}

5、注册menu子菜单单击事件:

menugrid.setonitemclicklistener(new onitemclicklistener() {
 public void onitemclick(adapterview<?> arg0, view arg1, int arg2,
   long arg3) {
  log.v("demo", "menugrid.setonitemclicklistener: "+arg2+" "+arg3);
  toast.maketext(uimenu.this, "click"+arg2, toast.length_short).show();
  if (pop.isshowing()) //关闭菜单
   pop.dismiss();
 }
});

6、界面截图:

Android实现模仿UCweb菜单效果的方法

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