首页 > 系统 > Android > 正文

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

2019-10-24 20:36:01
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Android实现模仿UCweb菜单效果的方法,较为详细的分析了Android模仿UCweb菜单效果的页面布局及功能实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

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

1、保留menu按键作用

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

3、用GridView显示所有子菜单

代码如下:

1、布局文件:

popupwindow.xml:

 

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout 
  3. xmlns:android="http://schemas.android.com/apk/res/android" 
  4. android:layout_width="fill_parent" 
  5. android:background="#50000000" 
  6. android:layout_height="fill_parent" 
  7. android:gravity="bottom|center_horizontal"
  8. <LinearLayout 
  9. xmlns:android="http://schemas.android.com/apk/res/android" 
  10. android:layout_width="320dip" 
  11. android:background="@drawable/bkg2" 
  12. android:id="@+id/popdialog" 
  13. android:layout_height="250dip" 
  14. android:gravity="center_vertical|center_horizontal" 
  15. <GridView 
  16. android:id="@+id/gridview" 
  17. android:layout_width="fill_parent" 
  18. android:layout_height="fill_parent" 
  19. android:numColumns="4" 
  20. android:verticalSpacing="10dip" 
  21. android:horizontalSpacing="10dip" 
  22. android:stretchMode="columnWidth" 
  23. android:gravity="center" 
  24. /> 
  25. </LinearLayout> 
  26. </LinearLayout> 

item_menu.xml:

 

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:id="@+id/RelativeLayout_Item" 
  4. android:layout_width="fill_parent" android:layout_height="wrap_content" 
  5. android:paddingBottom="5dip"
  6. <ImageView android:id="@+id/item_image" 
  7. android:layout_centerHorizontal="true" 
  8. android:layout_width="40dip" 
  9. android:layout_height="40dip"
  10. </ImageView> 
  11. <TextView android:layout_below="@id/item_image" 
  12. android:id="@+id/item_text" 
  13. android:layout_centerHorizontal="true" 
  14. android:layout_width="wrap_content" 
  15. android:layout_height="wrap_content" 
  16. style="@style/Text.Location" 
  17. android:text="选项"></TextView> 
  18. </RelativeLayout> 

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

 

 
  1. View view = this.getLayoutInflater().inflate(R.layout.popwindowdemo, null); 
  2. pop = new PopupWindow(view,320,450);//大小设置为全屏幕,这里硬编码的,可修改 
  3. pop.setOutsideTouchable(false); 
  4. pop.setBackgroundDrawable(new BitmapDrawable()); 
  5. pop.setFocusable(true);//如果不加这个,Grid不会响应ItemClick 
  6. pop.setTouchInterceptor(new OnTouchListener() { 
  7. public boolean onTouch(View v, MotionEvent event) { 
  8. // TODO Auto-generated method stub 
  9. if (event.getY()<240){ //这里处理,当点击gridview以外区域的时候,菜单关闭 
  10. if (pop.isShowing()) 
  11. pop.dismiss(); 
  12. Log.d("Demo""popupWindow::onTouch >>> view: " 
  13. + v + ", event: " + event); 
  14. return false
  15. }); 

3、初始化gridview:

 

 
  1. /** 菜单图片 **/ 
  2. int[] menu_image_array = { R.drawable.menu_search, 
  3. R.drawable.menu_filemanager, R.drawable.menu_downmanager, 
  4. R.drawable.menu_fullscreen, R.drawable.menu_inputurl, 
  5. R.drawable.menu_bookmark, R.drawable.menu_bookmark_sync_import, 
  6. R.drawable.menu_sharepage, R.drawable.menu_quit, 
  7. R.drawable.menu_nightmode, R.drawable.menu_refresh, 
  8. R.drawable.menu_more }; 
  9. /** 菜单文字 **/ 
  10. String[] menu_name_array = { "搜索""文件管理""下载管理""全屏""网址""书签"
  11. "加入书签""分享页面""退出""夜间模式""刷新""更多" }; 
  12. /** 
  13. * 构造菜单Adapter 
  14.  
  15. * @param menuNameArray 
  16. * 名称 
  17. * @param imageResourceArray 
  18. * 图片 
  19. * @return SimpleAdapter 
  20. */ 
  21. private SimpleAdapter getMenuAdapter(String[] menuNameArray, 
  22. int[] imageResourceArray) { 
  23. ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); 
  24. for (int i = 0; i < menuNameArray.length; i++) { 
  25. HashMap<String, Object> map = new HashMap<String, Object>(); 
  26. map.put("itemImage", imageResourceArray[i]); 
  27. map.put("itemText", menuNameArray[i]); 
  28. data.add(map); 
  29. SimpleAdapter simperAdapter = new SimpleAdapter(this, data, 
  30. R.layout.item_menu, new String[] { "itemImage""itemText" }, 
  31. new int[] { R.id.item_image, R.id.item_text }); 
  32. return simperAdapter; 
  33. menuGrid = (GridView) view.findViewById(R.id.gridview);  
  34. menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array)); 

4、注册Menu弹起事件:

 

 
  1. @Override 
  2. public boolean onCreateOptionsMenu(Menu menu) { 
  3. // TODO Auto-generated method stub 
  4. pop.showAtLocation(findViewById(R.id.mainfrm), Gravity.CENTER|Gravity.BOTTOM, 0, 0); 
  5. return false;//super.onCreateOptionsMenu(menu);  

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

 

 
  1. menuGrid.setOnItemClickListener(new OnItemClickListener() { 
  2. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
  3. long arg3) { 
  4. Log.v("Demo""menuGrid.setOnItemClickListener: "+arg2+" "+arg3); 
  5. Toast.makeText(UIMenu.this"Click"+arg2, Toast.LENGTH_SHORT).show(); 
  6. if (pop.isShowing()) //关闭菜单 
  7. pop.dismiss(); 
  8. }); 

6、界面截图:

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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表