Android自己定义TabActivity(实现仿新浪微博底部菜单更新UI)

时间:2023-03-10 00:54:30
Android自己定义TabActivity(实现仿新浪微博底部菜单更新UI)

现在Android上非常多应用都採用底部菜单控制更新的UI这样的框架,比如新浪微博

Android自己定义TabActivity(实现仿新浪微博底部菜单更新UI)

点击底部菜单的选项能够更新界面。底部菜单能够使用TabHost来实现,只是用过TabHost的人都知道自己定义TabHost到底是有多麻烦的。原生TabHost的风格是不依附屏幕的底部的,要依附底部就要重写布局。

TabHost设置的Container能够管理UI的显示,UI能够用LayoutInflater动态生成。也能够是Activity。但不好管理Activity的生命周期。然后用TabHost控制显示UI的显示。

以下使用的一种方法是自己定义菜单布局+ActivityGroup+多个Activity的方式实现,以下是Demo的截图:

Android自己定义TabActivity(实现仿新浪微博底部菜单更新UI)          Android自己定义TabActivity(实现仿新浪微博底部菜单更新UI)

ActivityGroup

ActivityGroup,顾名思义就是Activity组,能够管理多个Activity的启动和销毁。ActivityGroup是继承Activity的。可是这种方法眼下已经被弃用了。尽管不推荐使用,只是还是能够用的。以后会讲推荐的做法。

我们会用这个类管理界面的实现。ActivityGroup中有一个重要的方法是getLocalActivityManager,这种方法能够销毁和启动新的Activity,并能够通过getDecorView方法获取到启动Activity的根视图显示出来。

Activity显示在ActivityGroup中的一个container中,而container是显示Activity的一个区域,这个container必须是ViewGroup或者是其子类。

首先编写一个TabActivity

package com.shamoo.activity;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window; public class TabActivity extends ActivityGroup { private ViewGroup container; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
} /**
* 通过id设置Activity显示的container,该container必须是继承ViewGroup的
*/
protected void setContainer(int resId) {
container = (ViewGroup) findViewById(resId);
} /**
* 通过Activity的class显示Activity
*/
protected void showActivity(Class<?> activityClass) { Intent intent = new Intent(this, activityClass);
// 检查container是否有显示的Activity。假设有,先移除
View activity = container.getChildAt(0);
if (activity != null) {
// 移除显示的activity的View
container.removeAllViews();
// 通过ActivityManager移除activity
getLocalActivityManager().removeAllActivities();
}
// 启动新的activity。并将该activity的根视图加入到contanier中
container.addView(getLocalActivityManager().startActivity(activityClass.getName(), intent).getDecorView());
} }

编写一个继承TabActivity的MainActivity管理界面。界面是三个Activity

package com.shamoo.activity;

import com.shamoo.activitygroupdemo.R;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends TabActivity implements OnClickListener { /**
* 显示的三个Activity的class
*/
private Class<?> activities[] = {OneActivity.class, TwoActivity.class, ThreeActivity.class}; /**
* 菜单的三个button
*/
private Button[] btn = new Button[3]; /**
* 当前的选择
*/
private int currentSelect; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.activity_main);
setContainer(R.id.fl_container);
btn[0] = (Button) findViewById(R.id.btn_one);
btn[0].setOnClickListener(this);
btn[1] = (Button) findViewById(R.id.btn_two);
btn[1].setOnClickListener(this);
btn[2] = (Button) findViewById(R.id.btn_three);
btn[2].setOnClickListener(this);
showActivity(activities[0]);
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn[currentSelect].setBackgroundResource(R.color.normal);
switch (v.getId()) {
case R.id.btn_one:
currentSelect = 0;
btn[0].setBackgroundResource(R.color.select);
showActivity(activities[0]);
break;
case R.id.btn_two:
currentSelect = 1;
btn[1].setBackgroundResource(R.color.select);
showActivity(activities[1]);
break;
case R.id.btn_three:
currentSelect = 2;
btn[2].setBackgroundResource(R.color.select);
showActivity(activities[2]);
break;
}
} }

编写activity_main.xml,该布局有底部菜单的实现。是通过LinearLayout的layout_weight配合改动背景的Button实现的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:id="@+id/rl_menu"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#008eff"
android:text="1" />
<Button
android:id="@+id/btn_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#555555"
android:text="2" />
<Button
android:id="@+id/btn_three"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#555555"
android:text="3" />
</LinearLayout> <FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/rl_menu" > </FrameLayout>
</RelativeLayout>

然后编写三个Activity,这三个Activity能够自己定义。

代码比較多,就不全贴出来了。

启动Demo之后,能够看到Activity的生命周期管理是没有问题的

Android自己定义TabActivity(实现仿新浪微博底部菜单更新UI)

Demo下载链接:http://download.csdn.net/detail/stephenzcl/7306531