ActivityGroup实现Tab效果

相对于TabActivity来说,ActivityGroup实现Tab更加灵活。

xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <LinearLayout  
        android:id="@+id/containerBody"
        android:layout_width="match_parent"
    	android:layout_height="match_parent"
    	android:layout_weight="1"
    	android:orientation="vertical" ></LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_weight="5"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/toolbar_cost" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/toolbar_count" />
        
    </LinearLayout>

</LinearLayout>
Activity类:
package com.zzj.ui;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

@SuppressWarnings("deprecation")
public class MyActivityGroup extends ActivityGroup {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activitygroup);
		init();
	}

	private void init() {
		final LinearLayout containerBody = (LinearLayout) this
				.findViewById(R.id.containerBody);
		final LocalActivityManager activityManager = getLocalActivityManager();//获取Activity管理器
		containerBody.addView(activityManager.startActivity("tag1",
				new Intent(MyActivityGroup.this, Tab1.class)).getDecorView());

		ImageView imageView = (ImageView) this.findViewById(R.id.imageView1);
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				containerBody.removeAllViews();
				containerBody.addView(activityManager.startActivity(
						"tag1",
						new Intent(MyActivityGroup.this, Tab1.class)
								.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView());
			}
		});

		ImageView imageView2 = (ImageView) this.findViewById(R.id.imageView2);
		imageView2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				containerBody.removeAllViews();
				containerBody.addView(activityManager.startActivity(
						"tag2",
						new Intent(MyActivityGroup.this, Tab2.class)
								.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView());
			}
		});
	}
}
Tab1和Tab2的布局文件和类文件就省略了,就是普通的Activity。

效果:


你可能感兴趣的:(ActivityGroup实现Tab效果)