Android仿QQ空间底部菜单


之前曾经在网上看到Android仿QQ空间底部菜单的Demo,发现这个Demo有很多Bug,布局用了很多神秘数字。于是研究了一下QQ空间底部菜单的实现,自己写了一个,供大家参考。效果如下图所示:

点击中间的按钮后->

1、实现原理很简单,底部菜单是一个水平分布的LinearLayout,里面又是五个LinearLayout,它们的layout_weight都为1,意味着底部菜单的子控件将屏幕宽度平均分为5部分。五个LinearLayout除了中间那个,其余都在里面放置ImageView和TextView(中间先空着,什么都不放,后面用来放底盘和加号的)。

2、中间的加号和底盘是用FramLayout实现的,现在底部居中的位置放置底盘,然后在相同位置放置加号,就搞定了。

3、设置加号的触摸事件,弹窗是用PopupWindow实现的,然后再把加号的图片替换成乘号就搞定了。代码如下所示:

ButtomMenuActivity.java:

package com.shamoo.qqbuttommenu;

import com.shamoo.qqbuttommenu.R;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RadioButton;
import android.widget.TabHost;

public class ButtomMenuActivity extends TabActivity {
 FrameLayout fmpan;
 TabHost tabHost;
    ImageView image;
    FrameLayout fm;
    LayoutInflater inflater;
 private RadioButton tab_home, tab_second;
    PopupWindow popup;
 
    public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tab); 
  initView();
  fm.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    image.setImageResource(R.drawable.toolbar_plusback);
    showWindow(fmpan);
      }
  });
 }

 private void initView() {
  inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  fmpan = (FrameLayout)findViewById(R.id.tab1);
  fm = (FrameLayout)findViewById(R.id.btn_ck);
  image = (ImageView)findViewById(R.id.image1);
 }
 
 private void showWindow(View parent) { 
  if(popup == null) { 
   LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
   View view = layoutInflater.inflate(R.layout.write_tab, null); 
        // 创建一个PopuWidow对象 
   popup = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,320);
   // 设置焦点在弹窗上 
   popup.setFocusable(true); 
   // 设置允许在外点击消失 
   popup.setOutsideTouchable(true);
   // 设置弹窗消失事件监听
   popup.setOnDismissListener(new OnDismissListener() {
    public void onDismiss() {
     // TODO Auto-generated method stub
     image.setImageResource(R.drawable.toolbar_plus);
    }
   });
   // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景 
   popup.setBackgroundDrawable(new BitmapDrawable());
   popup.setTouchInterceptor(new OnTouchListener() { 
    public boolean onTouch(View view, MotionEvent event) { 
     if(event.getAction() == MotionEvent.ACTION_OUTSIDE) { 
      popup.dismiss();
          image.setImageResource(R.drawable.toolbar_plus);       
          return true; 
         } 
     return false; 
    } 
   });
  }
  if(!popup.isShowing()) {
   popup.showAsDropDown(parent, Gravity.CENTER, 0);
  }
 } 
}

  • 1
  • 2
  • 下一页

相关内容

    暂无相关文章