一个在项目中运用的类似电脑底部状态栏的功能实现,该功能类似广泛使用的底部切换标签,但又有明显的不同,唯一相同点就是使用继承GroupActivity的方式实现不同activity在主界面上的切换,具体实现如下:
主界面:
package com.bar.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
private Context mContext;
private GridView gridView;
private SimpleAdapter adapter;
private List<HashMap<String, Object>> iconList;
private Map<String, Object> mapItem;
//gridview 标题与图标
public static Object[][] gridViewData = new Object[][]{{"选项一",R.drawable.icon_im_at_desk_top,ToolUtil.ACTIVITY_ID_1},
{"选项二",R.drawable.icon_map_at_desk_top,ToolUtil.ACTIVITY_ID_2},
{"选项三",R.drawable.icon_interest_at_desk_top,ToolUtil.ACTIVITY_ID_3},
{"选项四",R.drawable.icon_locus_at_desk_top,ToolUtil.ACTIVITY_ID_4},
{"选项五",R.drawable.icon_assistant_at_desk_top,ToolUtil.ACTIVITY_ID_5},
{"选项六",R.drawable.icon_setting_at_desk_top,ToolUtil.ACTIVITY_ID_6},};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
gridView = (GridView) findViewById(R.id.gridView);
init();
}
//初始化
private void init(){
iconList = new ArrayList<HashMap<String,Object>>();
for(Object[] objects : gridViewData){
mapItem = new HashMap<String, Object>();
mapItem.put("title", objects[0]);
mapItem.put("icon", objects[1]);
iconList.add((HashMap<String, Object>) mapItem);
}
adapter = new SimpleAdapter(mContext, iconList, R.layout.gridview_item, new String[]{"title","icon"}, new int[]{R.id.tvTitle,R.id.ivIcon});
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//传递选择要打开的activity ID
Handler handler = ToolUtil.getHandler(AndroidBarTestActivity.class.getName());
int activityId = Integer.parseInt(gridViewData[position][2].toString());
if(handler != null){
Message msg = new Message();
msg.what = AndroidBarTestActivity.WITCH_ACTIVITY;
msg.obj = activityId;
handler.sendMessage(msg);
}
}
});
}
}
继承了GroupActivity的框架activity:
package com.bar.test;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.R.integer;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AndroidBarTestActivity extends ActivityGroup {
private Context mContext;
public static final int WITCH_ACTIVITY = 10;
// 当前activity ID
private int currentActivityId = ToolUtil.ACTIVITY_ID_MAIN;
private LayoutInflater lInflater;
private LocalActivityManager aManager;
private ImageButton ibClose,ibHide;
private TextView tvTitle;
private static final String PRIX = "activity_id_";
private LinearLayout contentView;//当前窗口所包含的视图 即activity
private LinearLayout bottomBarView;//底部状态栏所包含的视图 即activity
private List<Integer> openActivityIdList = new ArrayList<Integer>();//底部状态栏所有打开的activity id
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.system_bar);
ToolUtil.putHandler(AndroidBarTestActivity.class.getName(), handler);
mContext = this;
lInflater = this.getLayoutInflater();
aManager = this.getLocalActivityManager();
contentView = (LinearLayout) findViewById(R.id.content);
bottomBarView = (LinearLayout) findViewById(R.id.bottom_status_bar);
ibClose = (ImageButton) findViewById(R.id.close_window_btn);
ibHide = (ImageButton) findViewById(R.id.hide_window_btn);
tvTitle = (TextView) findViewById(R.id.title);
ibClose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
closeWindow();
}
});
ibHide.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideWindow();
}
});
switchWindowView();
}
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case WITCH_ACTIVITY:
int activityId = Integer.parseInt( msg.obj.toString());
openWindow(activityId);
break;
default:
break;
}
};
};
/**
* 关闭窗口
*/
private void closeWindow(){
aManager.destroyActivity(PRIX + currentActivityId, true);
try {
final Field mActivitiesField = LocalActivityManager.class
.getDeclaredField("mActivities");
if (mActivitiesField != null) {
mActivitiesField.setAccessible(true);
@SuppressWarnings("unchecked")
final Map<String, Object> mActivities = (Map<String, Object>) mActivitiesField
.get(aManager);
if (mActivities != null) {
mActivities.remove(PRIX + currentActivityId);
}
final Field mActivityArrayField = LocalActivityManager.class
.getDeclaredField("mActivityArray");
if (mActivityArrayField != null) {
mActivityArrayField.setAccessible(true);
@SuppressWarnings("unchecked")
final ArrayList<Object> mActivityArray = (ArrayList<Object>) mActivityArrayField
.get(aManager);
if (mActivityArray != null) {
for (Object record : mActivityArray) {
final Field idField = record.getClass()
.getDeclaredField("id");
if (idField != null) {
idField.setAccessible(true);
final String _id = (String) idField.get(record);
if ((PRIX + currentActivityId).equals(_id)) {
mActivityArray.remove(record);
break;
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
openActivityIdList.remove(new Integer(currentActivityId));
hideWindow();
}
/**
* 用于显示哪个activity窗口
*/
private void switchWindowView() {
View view = null;
Intent intent = null;
switch (currentActivityId) {
case ToolUtil.ACTIVITY_ID_MAIN:
intent = new Intent(mContext,MainActivity.class);
break;
case ToolUtil.ACTIVITY_ID_1:
intent = new Intent(mContext,FirstActivity.class);
break;
case ToolUtil.ACTIVITY_ID_2:
intent = new Intent(mContext,TwoActivity.class);
break;
case ToolUtil.ACTIVITY_ID_3:
intent = new Intent(mContext,ThreeActivity.class);
break;
case ToolUtil.ACTIVITY_ID_4:
// intent = new Intent(mContext,MainActivity.class);
break;
case ToolUtil.ACTIVITY_ID_5:
// intent = new Intent(mContext,MainActivity.class);
break;
case ToolUtil.ACTIVITY_ID_6:
// intent = new Intent(mContext,MainActivity.class);
break;
default:
break;
}
if(currentActivityId == ToolUtil.ACTIVITY_ID_MAIN){
tvTitle.setText("");
}
if(intent != null){
view = aManager.startActivity(PRIX+currentActivityId, intent).getDecorView();
contentView.removeAllViews();
contentView.addView(view);
switchActivityForBottomBar();
}
}
/**
* 底部状态栏所显示的activity
*/
private void switchActivityForBottomBar(){
bottomBarView.removeAllViews();
for(Integer activityId : openActivityIdList){
int statusIconId = 0;
switch (activityId) {
case ToolUtil.ACTIVITY_ID_1:
statusIconId = R.drawable.icon_im_at_status_bar;
break;
case ToolUtil.ACTIVITY_ID_2:
statusIconId = R.drawable.icon_map_at_status;
break;
case ToolUtil.ACTIVITY_ID_3:
statusIconId = R.drawable.icon_interest_at_status_bar;
break;
case ToolUtil.ACTIVITY_ID_4:
statusIconId = R.drawable.icon_locus_at_status_bar;
break;
case ToolUtil.ACTIVITY_ID_5:
statusIconId = R.drawable.icon_assistant_at_status_bar;
break;
case ToolUtil.ACTIVITY_ID_6:
statusIconId = R.drawable.icon_setting_at_status_bar;
break;
default:
break;
}
LinearLayout iconBg = (LinearLayout) lInflater.inflate(R.layout.bottom_bar_icon_detail, null);
ImageView iconView = (ImageView) iconBg.findViewById(R.id.running_activity_icon);
iconView.setTag(PRIX + activityId);
iconView.setImageResource(statusIconId);
//选中状态
if(activityId == currentActivityId){
iconView.setBackgroundResource(R.drawable.desk_top_bottom_icon_pressed);
iconView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideWindow();
}
});
//设置标题
if(currentActivityId != ToolUtil.ACTIVITY_ID_MAIN){
tvTitle.setText(MainActivity.gridViewData[currentActivityId-1][0].toString());
}else{
tvTitle.setText("");
}
}else{
iconView.setBackgroundDrawable(null);
iconView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String tag = v.getTag().toString();
currentActivityId = Integer.parseInt(tag.substring(tag.length() - 1),tag.length());
switchWindowView();
}
});
}
bottomBarView.addView(iconBg);
}
}
/**
* 隐藏窗口
*/
private void hideWindow(){
currentActivityId = ToolUtil.ACTIVITY_ID_MAIN;
switchWindowView();
}
private void openWindow(int activityId){
if(!openActivityIdList.contains(new Integer(activityId))){
openActivityIdList.add(activityId);
}
currentActivityId = activityId;
switchWindowView();
}
}
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/systemView" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/llTop"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/desk_top_status_bar"> <ImageButton android:id="@+id/hide_window_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_alignParentLeft="true" android:background="@drawable/hide_window_btn_selector"/> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="20sp"/> <ImageButton android:id="@+id/close_window_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dip" android:layout_alignParentRight="true" android:background="@drawable/close_window_btn_selector"/> </RelativeLayout> </LinearLayout> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/bottomView" android:layout_below="@+id/llTop"> </LinearLayout> <LinearLayout android:id="@+id/bottomView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/desk_top_status_bar" android:layout_alignParentBottom="true"> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/bottom_status_bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical|left" android:scrollbars="horizontal"> </LinearLayout> </HorizontalScrollView> </LinearLayout> </RelativeLayout>
工具类:
package com.bar.test;
import java.util.HashMap;
import android.os.Handler;
public class ToolUtil {
//定义每个activity的标识id
public static final int ACTIVITY_ID_MAIN = 0;
public static final int ACTIVITY_ID_1 = 1;
public static final int ACTIVITY_ID_2 = 2;
public static final int ACTIVITY_ID_3 = 3;
public static final int ACTIVITY_ID_4 = 4;
public static final int ACTIVITY_ID_5 = 5;
public static final int ACTIVITY_ID_6 = 6;
//保存handler实例
public static HashMap<String, Handler> handlerMap = new HashMap<String, Handler>();
public static void putHandler(String activityName,Handler handler){
synchronized (handlerMap) {
handlerMap.put(activityName, handler);
}
}
public static Handler getHandler(String activityName){
return handlerMap.get(activityName);
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:id="@+id/running_activity_icon" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>