zoukankan      html  css  js  c++  java
  • android:同时弹出顶部和底部菜单的做法

    android同时弹出顶部和底部菜单

      在android开发中会碰到这样的需求,要同时弹出顶部和底部的菜单。目前已经上市的APP中有91熊猫读书和QQ阅读器带这样的功能。

    点击Menu和点击屏幕都会弹出菜单。有很多方法可以实现。我的方法是在RelativaLayout中设置好菜单布局,然后在监听事件中使其

    显示/隐藏。具体做法如下:

      一:布局。可根据需求做一些复杂的设计。在这儿用两个按钮btn_top和btn_bottom。

    <Button
      android:id="@+id/btn_top"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="#AAAAAA"
      android:text="@string/top" android:textSize="30dip"
      android:textColor="#FF0000"
      android:visibility="invisible" /> <!-- 默认隐藏-->
    <Button
      android:id="@+id/btn_bottom"
      android:layout_height="wrap_content" android:layout_width="fill_parent"
      android:background="#AAAAAA"
      android:text="@string/bottom" android:textSize="30dip"
      android:textColor="#FF0000"
      android:layout_alignParentBottom="true"
      android:visibility="invisible" /> <!-- 默认隐藏-->

      //这两个视图放在最上一层。在同一个layout设置需要的主布局。

      二:在代码中设置handler.因为主线程不能操作UI,只能通过handler实现

     1 private class MainHandler extends Handler{
     2   static final int MSG_VISIBLE = 1; //显示
     3   static final int MSG_INVISIBLE = 2; //消失
     4   @Override
     5   public void handleMessage(Message msg) {
     6   super.handleMessage(msg);
     7   Animation inAnima = new AlphaAnimation(0.1f, 1.0f); //代表按钮显示时的动画效果。可根据需求来设置
     8   inAnima.setDuration(1000);
     9   Animation outAnima = new AlphaAnimation(1.0f, 0.1f);//代表按钮消失时的动画效果。可根据需求来设置
    10   outAnima.setDuration(1000);
    11   switch(msg.what)
    12   {
    13     case MSG_VISIBLE:
    14       btnTop.setAnimation(inAnima);//设置显示时动画
    15       btnBottom.setAnimation(inAnima);//设置显示时动画
    16       btnTop.setVisibility(View.VISIBLE);//设置显示
    17       btnBottom.setVisibility(View.VISIBLE);//设置显示
    18       break;
    19     case MSG_INVISIBLE:
    20       btnTop.setAnimation(outAnima);//设置消失时动画
    21       btnBottom.setAnimation(outAnima);//设置消失时动画
    22       btnTop.setVisibility(View.INVISIBLE);//设置消失
    23       btnBottom.setVisibility(View.INVISIBLE);//设置消失
    24       break;
    25     default:
    26       break;
    27   }
    28   }
    29 
    30   public void sendMessage(int nMsg) { //在Handler中封装下sendMessage函数,提高代码简洁性
    31     Message msg = Message.obtain();
    32     msg.what = nMsg;
    33     this.sendMessage(msg);
    34   }
    35 }

      三.onCreate函数,onCreate函数尽量简洁,能封装出去的就封装出去,然后调用就可以了。设置一个变量

    clickCount来代表该显示还是该隐藏视图。

     1 private int clickCount = 0; //奇数显示,偶数隐藏 
     2 
     3   @Override
     4   protected void onCreate(Bundle savedInstanceState) { //建议主函数像左边这样
     5     super.onCreate(savedInstanceState);
     6     setContentView(R.layout.testintentactivity);
     7     btnTop = (Button)this.findViewById(R.id.btn_top);
     8     btnBottom = (Button)this.findViewById(R.id.btn_bottom);
     9     handler = new MainHandler(); //定义handler
    10     setListener();
    11     }
    12 
    13     private void setListener() {
    14       btnTop.setOnClickListener(this);
    15       btnBottom.setOnClickListener(this);
    16     }
    17 
    18   @Override
    19   public void onClick(View v) {
    20     clickCount ++; //全局变量值要变
    21     switch (v.getId()) {
    22     case R.id.btn_top:
    23       Toast.makeText(this, getResources().getString(R.string.top),Toast.LENGTH_SHORT).show(); //弹出Toast来测试按钮是否获取到了焦点
    24       break;
    25     case R.id.btn_bottom:
    26       Toast.makeText(this, getResources().getString(R.string.bottom),Toast.LENGTH_SHORT).show();//弹出Toast来测试按钮是否获取到了焦点
    27       break;
    28     default:
    29       break;
    30     }
    31     handler.sendMessage(MainHandler.MSG_INVISIBLE);//不管点击哪个按钮。两按钮都要设置隐藏。
    32   }

      四.通过onTouchEvent来监听屏幕的点击,并且通过onKeyDown监听Menu键

    @Override
      public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){
        clickCount++;
        if(clickCount % 2 == 0){ //偶数隐藏
        handler.sendMessage(MainHandler.MSG_INVISIBLE);
        }else{ //奇数消失
        handler.sendMessage(MainHandler.MSG_VISIBLE);
        }
      }
      return true;
    }
    
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_MENU){ //不要通过onCreateOptionsMenu来监听Menu.它会调用系统的一些默认属性,达不到我们想要的效果
        clickCount++;
        if(clickCount % 2 == 0){
        handler.sendMessage(MainHandler.MSG_INVISIBLE);
        }else{
        handler.sendMessage(MainHandler.MSG_VISIBLE);
        }
        }
        return super.onKeyDown(keyCode, event);
      }

      可以转载,但请注明出处,谢谢!

      作者:Carman  2012-08-13 15:27:10

      邮箱:carman_loneliness@163.com

     

  • 相关阅读:
    Javascript 多浏览器兼容性问题及解决方案
    Vue-学习。
    angular-动画。
    Angular-学习。
    JQuery-学习。
    Bootstrap框架。
    Swiper-轮播图。
    jquery validation表单验证插件2。
    jquery validation表单验证插件。
    Grunt-学习。
  • 原文地址:https://www.cnblogs.com/carmanloneliness/p/2636484.html
Copyright © 2011-2022 走看看