- 浏览: 3508838 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (1491)
- Hibernate (28)
- spring (37)
- struts2 (19)
- jsp (12)
- servlet (2)
- mysql (24)
- tomcat (3)
- weblogic (1)
- ajax (36)
- jquery (47)
- html (43)
- JS (32)
- ibatis (0)
- DWR (3)
- EXTJS (43)
- Linux (15)
- Maven (3)
- python (8)
- 其他 (8)
- JAVASE (6)
- java javase string (0)
- JAVA 语法 (3)
- juddiv3 (15)
- Mule (1)
- jquery easyui (2)
- mule esb (1)
- java (644)
- log4j (4)
- weka (12)
- android (257)
- web services (4)
- PHP (1)
- 算法 (18)
- 数据结构 算法 (7)
- 数据挖掘 (4)
- 期刊 (6)
- 面试 (5)
- C++ (1)
- 论文 (10)
- 工作 (1)
- 数据结构 (6)
- JAVA配置 (1)
- JAVA垃圾回收 (2)
- SVM (13)
- web st (1)
- jvm (7)
- weka libsvm (1)
- weka屈伟 (1)
- job (2)
- 排序 算法 面试 (3)
- spss (2)
- 搜索引擎 (6)
- java 爬虫 (6)
- 分布式 (1)
- data ming (1)
- eclipse (6)
- 正则表达式 (1)
- 分词器 (2)
- 张孝祥 (1)
- solr (3)
- nutch (1)
- 爬虫 (4)
- lucene (3)
- 狗日的腾讯 (1)
- 我的收藏网址 (13)
- 网络 (1)
- java 数据结构 (22)
- ACM (7)
- jboss (0)
- 大纸 (10)
- maven2 (0)
- elipse (0)
- SVN使用 (2)
- office (1)
- .net (14)
- extjs4 (2)
- zhaopin (0)
- C (2)
- spring mvc (5)
- JPA (9)
- iphone (3)
- css (3)
- 前端框架 (2)
- jui (1)
- dwz (1)
- joomla (1)
- im (1)
- web (2)
- 1 (0)
- 移动UI (1)
- java (1)
- jsoup (1)
- 管理模板 (2)
- javajava (1)
- kali (7)
- 单片机 (1)
- 嵌入式 (1)
- mybatis (2)
- layui (7)
- asp (12)
- asp.net (1)
- sql (1)
- c# (4)
- andorid (1)
- 地价 (1)
- yihuo (1)
- oracle (1)
最新评论
-
endual:
https://blog.csdn.net/chenxbxh2 ...
IE6 bug -
ice86rain:
你好,ES跑起来了吗?我的在tomcat启动时卡在这里Hibe ...
ES架构技术介绍 -
TopLongMan:
...
java public ,protect,friendly,private的方法权限(转) -
贝塔ZQ:
java实现操作word中的表格内容,用插件实现的话,可以试试 ...
java 读取 doc poi读取word中的表格(转) -
ysj570440569:
Maven多模块spring + springMVC + JP ...
Spring+SpringMVC+JPA
在android开发的过程中系统自带的菜单往往满足不了开发中的一些需求,比如说一排最多只能放置三个菜单,坐多只能放置6个,再多的话就会折叠起来,如果我们想再一排显示4个或5个菜单那么就要自己想办法处理。
这里我用布局的隐藏并加上动画来模拟菜单的效果。
要点:
1、隐藏和显示菜单,我使用了一个线性布局把菜单封装起来。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_alignParentBottom="true" android:background="@drawable/menubackground" android:layout_width="fill_parent" android:layout_height="144px" android:orientation="vertical" android:gravity="center" android:visibility="gone" android:id="@+id/lines"> <LinearLayout android:orientation="horizontal" android:gravity="center" android:layout_width="fill_parent" android:layout_height="72px" > <ImageButton android:layout_marginLeft="8dip" android:id="@+id/menu_btn_index" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menu_index_selector" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menu_news_selector" android:id="@+id/menu_btn_news" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menu_lib_selector" android:id="@+id/menu_btn_lib" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menu_add_selector" android:id="@+id/menu_btn_add" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menu_set_selector" android:id="@+id/menu_btn_set" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:gravity="center" android:layout_width="fill_parent" android:layout_height="72px"> <ImageButton android:layout_marginLeft="8dip" android:id="@+id/menu_btn_index" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menu_index_selector" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menu_news_selector" android:id="@+id/menu_btn_news" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menu_lib_selector" android:id="@+id/menu_btn_lib" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menu_add_selector" android:id="@+id/menu_btn_add" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menu_quit_selector" android:id="@+id/menu_btn_quit" /> </LinearLayout> </LinearLayout>
2、模拟菜单的效果,增加动画,布局显示的时候增加一个渐渐底部生气的效果,隐藏的时候增加一个缓缓下落的效果,显示菜单动画文件:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="00" android:toYDelta="140" android:duration="200" /> </set>
隐藏菜单动画文件:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="140" android:toYDelta="00" android:duration="200" /> </set>
动画调用:
/** * 显示菜单栏, 重新实现的Option menu. * */ private void showAppMenu() { if (menuShowAnimation == null) { menuShowAnimation = AnimationUtils .loadAnimation(mContext, R.anim.menuhide); } myLayout.startAnimation(menuShowAnimation); myLayout.setVisibility(View.VISIBLE); } /** * 隐藏菜单栏, 重新实现的Option menu. * */ private void hideAppMenu() { myLayout.setVisibility(View.GONE); if (menuHideAnimation == null) menuHideAnimation =AnimationUtils .loadAnimation(mContext, R.anim.menushow); myLayout.startAnimation(menuHideAnimation); }
3、控制菜单的隐藏和显示,需要重写三个方法public boolean onCreateOptionsMenu(Menu menu),public boolean dispatchKeyEvent(KeyEvent event) 和public boolean dispatchTouchEvent(MotionEvent event)
@Override public boolean onCreateOptionsMenu(Menu menu) { if(mCustomMenu==null) mCustomMenu=new CustomMenu(CustomMenuActivity.this,CustomMenuActivity.this); mCustomMenu.CreateMenu(); return false; } @Override public boolean dispatchKeyEvent(KeyEvent event) { if(mCustomMenu!=null) return mCustomMenu.dispatchKeyEvent(event,super.dispatchKeyEvent(event)); return super.dispatchKeyEvent(event); } @Override public boolean dispatchTouchEvent(MotionEvent event) { if(mCustomMenu!=null) return mCustomMenu.dispatchTouchEvent(event,super.dispatchTouchEvent(event)); return super.dispatchTouchEvent(event); }
4、实现菜单点击时候被点击菜单状态的般变化,这里我使用了selector来实现,菜单我使用ImageButton将selector赋值给ImageButton 的background即可:一个菜单项
<ImageButton android:layout_marginLeft="8dip" android:id="@+id/menu_btn_index" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menu_index_selector" />menu_index_selector 文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2009 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@drawable/menu_index" /> <item android:state_pressed="true" android:drawable="@drawable/menu_index1" /> <item android:state_focused="true" android:drawable="@drawable/menu_index1" /> <item android:drawable="@drawable/menu_index" /> </selector>5、页面的调用使用:<include>标签来进行引用:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <include layout="@layout/menu_layout"/> </RelativeLayout>
这样的话一个模拟的自定义菜单就基本完成了,菜单控制完整代码java类:
package com.demo.utils; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageButton; import android.widget.LinearLayout; import com.demo.HelloWorld.R; /** * @author Administrator * xsl xushilin@kingtoneinfo.com * @version: 创建时间:2011-8-30 上午11:16:19 * 说 明: * 修改历史: */ public class CustomMenu { private LinearLayout myLayout; private Context mContext; private Activity mActivity; private Animation menuShowAnimation = null; private Animation menuHideAnimation = null; private Resources res; public int screen_height; private ImageButton imgIndex,imgSet,imgNews,imgAdd,imgQuit,imgLib; public CustomMenu(Context context,Activity activity){ mContext=context; mActivity=activity; res = mContext.getResources(); screen_height = res.getDisplayMetrics().heightPixels; myLayout=(LinearLayout)activity.findViewById(R.id.lines); imgIndex=(ImageButton)activity.findViewById(R.id.menu_btn_index); imgSet=(ImageButton)activity.findViewById(R.id.menu_btn_set); imgNews=(ImageButton)activity.findViewById(R.id.menu_btn_news); imgAdd=(ImageButton)activity.findViewById(R.id.menu_btn_add); imgQuit=(ImageButton)activity.findViewById(R.id.menu_btn_quit); imgLib=(ImageButton)activity.findViewById(R.id.menu_btn_lib); //返回首页 imgIndex.setOnClickListener(new OnClickListener(){ public void onClick(View v) { //TODO do somthing } }); //设置 imgSet.setOnClickListener(new OnClickListener(){ public void onClick(View v) { //TODO do somthing } }); //查询 imgNews.setOnClickListener(new OnClickListener(){ public void onClick(View v) { //TODO do somthing } }); //编辑 imgAdd.setOnClickListener(new OnClickListener(){ public void onClick(View v) { //TODO do somthing } }); //退出系统 imgQuit.setOnClickListener(new OnClickListener(){ public void onClick(View v) { //TODO do somthing } }); //素材库 imgLib.setOnClickListener(new OnClickListener(){ public void onClick(View v) { //TODO do somthing } }); } public void CreateMenu(){ if(myLayout.getVisibility()==View.GONE) showAppMenu(); //myLayout.setVisibility(View.VISIBLE); else hideAppMenu(); //myLayout.setVisibility(View.GONE); } /** * 显示菜单栏, 重新实现的Option menu. * */ private void showAppMenu() { if (menuShowAnimation == null) { menuShowAnimation = AnimationUtils .loadAnimation(mContext, R.anim.menuhide); } myLayout.startAnimation(menuShowAnimation); myLayout.setVisibility(View.VISIBLE); } /** * 隐藏菜单栏, 重新实现的Option menu. * */ private void hideAppMenu() { myLayout.setVisibility(View.GONE); if (menuHideAnimation == null) menuHideAnimation =AnimationUtils .loadAnimation(mContext, R.anim.menushow); myLayout.startAnimation(menuHideAnimation); } public boolean dispatchTouchEvent(MotionEvent event,boolean b) { if (myLayout.getVisibility() == View.VISIBLE) { int y = (int) event.getRawY(); if (y < screen_height - myLayout.getHeight()) { hideAppMenu(); return true; } } return b; } public boolean dispatchKeyEvent(KeyEvent event,boolean b) { int act = event.getAction(); int code = event.getKeyCode(); // app menu like option menu if (code == KeyEvent.KEYCODE_MENU){ if (act == KeyEvent.ACTION_DOWN){ if (myLayout.getVisibility() == View.VISIBLE) { hideAppMenu(); } else { showAppMenu(); } return true; } }else if (code == KeyEvent.KEYCODE_BACK){ if (myLayout.getVisibility() == View.VISIBLE) { hideAppMenu(); return true; } } return b; } }
activity调用菜单完整代码:
package com.demo.ui; import com.demo.HelloWorld.R; import com.demo.utils.CustomMenu; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.Menu; import android.view.MotionEvent; /** * @author XSL * xsl xushilin@kingtoneinfo.com * @version: 创建时间:2011-8-30 上午11:13:14 * 说 明: * 修改历史: */ public class CustomMenuActivity extends Activity { private CustomMenu mCustomMenu=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_menu); } @Override public boolean onCreateOptionsMenu(Menu menu) { if(mCustomMenu==null) mCustomMenu=new CustomMenu(CustomMenuActivity.this,CustomMenuActivity.this); mCustomMenu.CreateMenu(); return false; } @Override public boolean dispatchKeyEvent(KeyEvent event) { if(mCustomMenu!=null) return mCustomMenu.dispatchKeyEvent(event,super.dispatchKeyEvent(event)); return super.dispatchKeyEvent(event); } @Override public boolean dispatchTouchEvent(MotionEvent event) { if(mCustomMenu!=null) return mCustomMenu.dispatchTouchEvent(event,super.dispatchTouchEvent(event)); return super.dispatchTouchEvent(event); } }
实现的效果如下:
作者:蓝之风
出处:http://www.cnblogs.com/vaiyanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
发表评论
-
android离线
2019-11-04 21:25 264https://www.jianshu.com/p/fdfc4 ... -
android 异步更新
2015-07-11 22:42 1466http://www.apkbus.com/forum.php ... -
仿微米
2015-06-26 10:22 0http://www.javaapk.com/source/4 ... -
仿微信
2015-06-26 10:21 1877http://www.javaapk.com/source/1 ... -
网易客户端
2015-06-26 10:20 1565http://www.javaapk.com/source/4 ... -
仿朋友圈
2015-06-26 10:18 1668http://www.javaapk.com/source/8 ... -
仿朋友圈
2015-06-26 10:13 1780http://www.eoeandroid.com/threa ... -
全国ABC查询
2015-06-26 10:07 1280IM 功能 环信云 信息统计 Umeng 分享Umeng 更新 ... -
android 判断网络
2015-06-25 16:00 1223http://www.cnblogs.com/renqingp ... -
android 多彩表格
2015-05-31 16:06 1436http://blog.csdn.net/conowen/ar ... -
android socket
2015-05-31 16:01 1597http://blog.csdn.net/conowen/ar ... -
微信播放语音3
2015-02-25 12:00 1394http://download.csdn.net/detail ... -
微信语音播放2
2015-02-25 11:57 1298http://download.csdn.net/detail ... -
微信语音播放
2015-02-25 11:56 1644http://www.apkbus.com/android-1 ... -
超强的资料整合
2015-01-05 11:04 1198http://zz563143188.iteye.com/bl ... -
最强开发资源平台大合集
2015-01-04 14:50 1851程序员代码应用开发平 ... -
Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
2014-09-04 15:45 1337http://blog.csdn.net/vipzjyno1/ ... -
android应用推广
2014-09-03 23:25 1281应用推广不必愁,有米 ... -
android 图片异步加载
2014-09-03 16:47 1099http://www.csdn.net/article/201 ... -
android hander的使用
2014-09-02 23:46 1325http://www.cnblogs.com/xiaoran1 ...
相关推荐
Android2.2版本中,如果你要修改菜单默认的背景颜色,网络上有许多的方法。在此就不说了。但是,如果要在2.2以上的版本修改菜单的样式,例如修改背景颜色,图片等,使用这些方法是没有任何作用的。因此自己写了一个...
android自定义菜单使用技巧,其中还包含QuickContactBadge控件的使用
简单实用的Android系统菜单与自定义菜单的实现方法,并对菜单的调用机制进行了全面验证。 开发环境:XP3+Eclipse+Android2.2+JDK6.0 测试环境:Android2.2模拟器
android 菜单 menu android 菜单 menu android 菜单 menu android 菜单 menu android 菜单 menu
Android系统菜单与自定义菜单,好用
一个可向四周展开的自定义菜单控件
用自定义View的方式来实现圆形的遥控器菜单。更多关于android自定义的知识可访问:http://blog.sina.com.cn/s/blog_9c4f0e790102vyt3.html
本示例完整实现了自定义下拉菜单的功能,初学者可快速掌握自定义下拉菜单的技巧,有关具体的实现细节请参考博文:http://blog.csdn.net/l1028386804/article/details/48101651
较简单的实现,有不足和实现的不到位的还请指教 属性动画处两种实现方式,注释中都有说明 ...学习和实现主要参考博客 http://blog.csdn.net/harvic880925?viewmode=contents 中自定义控件三部曲之动画篇的前面几篇
android 一套自定义菜单 有图标 自定义一个tabmenu 继承PopupWindow menu做成Tab-Menu(支持分页Menu)
Android Studio 底部自定义菜单,菜单上有消息数提醒,很实用
android 自定义菜单实现 (仿UC浏览器) 仿UC浏览器 的android 源码工程文件。
android自定义控件(组合控件)练习,优酷菜单控件
在Android的应用中,经常会见到底部菜单.而在企业级的Android应用中,也存在同样的需求,但与微信这些大众软件的区别在于企业级的Android应用由于UI页面很多,每个页面都需要有底部菜单,而且每个页面的底部菜单按钮...
自定义的菜单源码其中之二,其他请查看我不知道有没有密码,if(有)www.moage.com
android 菜单栏 仿微信 自定义View初阶学习 记录下 方便以后再次使用
Android自定义TreeView三级菜单
android 自定义下拉菜单模式——Spinner与setDropDownViewResource