歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android仿QQ空間底部菜單

Android仿QQ空間底部菜單

日期:2017/3/1 9:55:45   编辑:Linux編程

之前曾經在網上看到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);
}
}
}

Copyright © Linux教程網 All Rights Reserved