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实现

  private class MainHandler extends Handler{
  static final int MSG_VISIBLE = 1; //显示
  static final int MSG_INVISIBLE = 2; //消失
  @Override
  public void handleMessage(Message msg) {
  super.handleMessage(msg);
  Animation inAnima = new AlphaAnimation(0.1f, 1.0f); //代表按钮显示时的动画效果。可根据需求来设置
  inAnima.setDuration(1000);
  Animation outAnima = new AlphaAnimation(1.0f, 0.1f);//代表按钮消失时的动画效果。可根据需求来设置
  outAnima.setDuration(1000);
  switch(msg.what)
  {
    case MSG_VISIBLE:
      btnTop.setAnimation(inAnima);//设置显示时动画
      btnBottom.setAnimation(inAnima);//设置显示时动画
      btnTop.setVisibility(View.VISIBLE);//设置显示
      btnBottom.setVisibility(View.VISIBLE);//设置显示
      break;
    case MSG_INVISIBLE:
      btnTop.setAnimation(outAnima);//设置消失时动画
      btnBottom.setAnimation(outAnima);//设置消失时动画
      btnTop.setVisibility(View.INVISIBLE);//设置消失
      btnBottom.setVisibility(View.INVISIBLE);//设置消失
      break;
    default:
      break;
  }
  }

  public void sendMessage(int nMsg) { //在Handler中封装下sendMessage函数,提高代码简洁性
    Message msg = Message.obtain();
    msg.what = nMsg;
    this.sendMessage(msg);
  }
}

  三.设置一个变量clickCount来代表该显示还是该隐藏视图。

  private int clickCount = 0; //奇数显示,偶数隐藏 

  @Override
  protected void onCreate(Bundle savedInstanceState) { //建议主函数像左边这样,尽量简洁
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testintentactivity);
    btnTop = (Button)this.findViewById(R.id.btn_top);
    btnBottom = (Button)this.findViewById(R.id.btn_bottom);
    handler = new MainHandler(); //定义handler
    setListener();
    }

    private void setListener() {
      btnTop.setOnClickListener(this);
      btnBottom.setOnClickListener(this);
    }

  @Override
  public void onClick(View v) {
    clickCount ++; //全局变量值要变
    switch (v.getId()) {
    case R.id.btn_top:
      Toast.makeText(this, getResources().getString(R.string.top),Toast.LENGTH_SHORT).show(); //弹出Toast来测试按钮是否获取到了焦点
      break;
    case R.id.btn_bottom:
      Toast.makeText(this, getResources().getString(R.string.bottom),Toast.LENGTH_SHORT).show();//弹出Toast来测试按钮是否获取到了焦点
      break;
    default:
      break;
    }
    handler.sendMessage(MainHandler.MSG_INVISIBLE);//不管点击那个按钮。两按钮都要设置隐藏。
  }

  四.监听touch屏幕,并且通过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);
  }

posted @ 2012-08-13 16:00  大胸弟、  阅读(541)  评论(0编辑  收藏  举报