Android 自定义Menu

Android的Menu键, 逐渐淡出历史舞台, 请看看Say Goodbye to the Menu button. Menu键消失不意味着Menu功能的消失, 恰恰相反Menu功能在Action Bar上面得到更广阔的发展. 效果如下:

010742194.gif

自定义Menu都是使用自定义的PopupWindow或者AlertDialog代替传统的Menu.

这里我使用了ActionProvider+PopupWindow实现自定义Menu. 这也是Google官方推荐的方式.


在Activity中

1
2
3
4
5
@Override
public  boolean  onCreateOptionsMenu(Menu menu) {
     getMenuInflater().inflate(R.menu.main, menu);
     return  true ;
}

menu的布局main.xml

1
2
3
4
5
6
7
8
< menu  xmlns:android = "http://schemas.android.com/apk/res/android"  >
     < item
         android:id = "@+id/action_settings"
         android:orderInCategory = "100"
         android:showAsAction = "ifRoom"
         android:actionProviderClass = "com.lichen.remind.actionbar.BlinkActionProvider"
         android:title = "@string/action_settings" />
</ menu >


自定义布局文件blink_action_provider.xml, 目标是加载到MenuItem的位置.

1
2
3
4
5
6
7
8
9
<? xml  version = "1.0"  encoding = "utf-8" ?>
< RelativeLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"  >
     < ImageView  android:id = "@+id/menu_blink"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:contentDescription = "@string/menu_blink" />
</ RelativeLayout >

这里的布局只是一个图片. 可以给它添加Listener, 然后动态添加PopupWindow.

android:actionProviderClass="com.lichen.remind.actionbar.BlinkActionProvider", 需要继承ActionProvider, 实现其onCreateActionView().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public  class  BlinkActionProvider  extends  ActionProvider  implements  OnClickListener {
     private  Context mContext;
     private  LayoutInflater mLayoutInflater;
     private  PopupWindow mPopWindow;
     // 注意构造,需要super(context);
     public  BlinkActionProvider(Context context) {
         super (context);
         mContext = context;
     }
     @Override
     @Deprecated
     public  View onCreateActionView() {
         mLayoutInflater = LayoutInflater.from(mContext);
         View rootView = mLayoutInflater.inflate(R.layout.blink_action_provider,
                 null );
         ImageView menuBlink = (ImageView) rootView
                 .findViewById(R.id.menu_blink);
         menuBlink.setBackgroundResource(R.drawable.blink_menu);
         menuBlink.setOnClickListener( this );
         return  rootView;
     }
     @Override
     public  void  onClick(View view) {
         /** 自定义PopupWindow */
         ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(
                 R.layout.fragment_about_me,  null true );
         mPopWindow =  new  PopupWindow(menuView, LayoutParams.WRAP_CONTENT,
                 LayoutParams.WRAP_CONTENT,  true );
         // 设置背景透明色
         mPopWindow.setBackgroundDrawable( new  ColorDrawable(Color.TRANSPARENT));
         /**设置背景图
         mPopWindow.setBackgroundDrawable(mContext.getResources().getDrawable(
                 R.drawable.balloon));*/
         mPopWindow.setOutsideTouchable( true ); // 设置触摸外面时消失
         mPopWindow.setAnimationStyle(android.R.style.Animation_Dialog); // 设置动画效果
         mPopWindow.showAsDropDown(view); // 显示位置在锚点view的左边底部
         /** 点击TextView */
         TextView tv = (TextView) menuView.findViewById(R.id.about_me);
         tv.setOnClickListener( new  View.OnClickListener() {
             public  void  onClick(View v) {
                 Toast.makeText(mContext,  "点击了BlinkMenu" , Toast.LENGTH_SHORT)
                         .show();
                 mPopWindow.dismiss();
             }
         });
     }
}

这里的R.layout.fragment_about_me,R.id.about_me请参考上一篇的布局文件


其实, 不仅关注技术, 可以更多的关注设计理念. 如Menu的变化趋势.

真正好的设计, 我以为是需要有对Android足够深入的理解, 而不是仅仅PS几张图.



本文转自chainli 51CTO博客,原文链接:http://blog.51cto.com/lichen/1216279,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值