接上一篇Android 滚动Tab

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

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

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


在Activity中

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.main, menu);    return true;}

menu的布局main.xml

    


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

<?xml version="1.0" encoding="utf-8"?>    

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

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

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几张图.

更多相关文章

  1. android实现TextView多行文本滚动
  2. Android系统中设置TextView的行间距(非行高)
  3. 常用的布局和View常用属性
  4. Android(安卓)Animation动画 控制动画的执行效果,速度等
  5. Android的RelativeLayout的layout_height属性设置为wrap_content
  6. Android(安卓)- Android(安卓)Studio 安装 及 设置 [最新]
  7. Android(安卓)EditText 一些属性设置
  8. android 滚动条 相关属性
  9. Android笔记-2

随机推荐

  1. Programming access to Android(安卓)Mar
  2. 广播 (Broadcast)
  3. 面向 Android(安卓)软件开发套件(SDK)的 x8
  4. Service与Android系统设计(2)-- Parcel
  5. 老罗牛文二、在Ubuntu上下载、编译和安装
  6. android中进程与线程
  7. Activity任务栈和启动模式
  8. Android(安卓)无法解决的几处硬伤
  9. android飞行模式灰显不能操作问题分解
  10. android中使用Notification打开详情界面a