>>分享Android开发相关的技术 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 19156 个阅读者 刷新本主题
 * 贴子主题:  Android 自定义Menu 回复文章 点赞(0)  收藏  
作者:mary    发表时间:2020-05-15 07:50:41     消息  查看  搜索  好友  邮件  复制  引用

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

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

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

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

  在Activity中
@Override

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);

    return true;

}

  menu的布局 main.xml

  <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的位置.

<?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().

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



----------------------------
原文链接:https://blog.51cto.com/lichen/1216279

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2020-05-15 17:17:36 重新编辑]
  Java面向对象编程-->面向对象开发方法概述之开发思想(上)
  JavaWeb开发-->Web运作原理(Ⅱ)
  JSP与Hibernate开发-->第一个helloapp应用
  Java网络编程-->基于UDP的数据报和套接字
  精通Spring-->通过Vuex进行状态管理
  Vue3开发-->Vue CLI脚手架工具
  Android网络编程之WebKit应用
  Android开发实践:编译VLC-for-Android
  Android制作Tabs界面的常用方法
  Android Application Theme的实现及管理
  Android开发实践:用脚本编译Android工程
  Android网络开发-创建请求队列
  Android 解码播放GIF图像
  Android 4.0 : 复制APK,复制动态库的Android.mk 文件
  Android ListView高度问题
  Android性能优化技巧
  android使用工具性能优化
  Android 加载大图/多图,有效避免OOM
  Android 内容提供者(Content Provider)
  Android 服务(Service)
  Matrix源码分析
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。