Activity Group iPhoneスタイルのボトムタブメニューを実現

9349 ワード

java 
public class ActsGroup extends ActivityGroup {

	private LinearLayout bodyView;
	private LinearLayout home, gamebox, team, more;
	private int flag = 0; //  , 
//	private String parameter = Constant.BUTTON_HOME;//  

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE); // 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acts_group);
		initMainView();
		//  
		Bundle bundle = getIntent().getExtras();
		if (null != bundle) {
			flag = bundle.getInt("flag");
		}
		//   
		showView(flag);

		home.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				flag = 0;
				showView(flag);
			
			}
		});
		gamebox.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				flag = 1;
				showView(flag);

			}
		});
		team.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				flag = 2;
				showView(flag);
			
			}
		});
		more.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				flag = 3;
				showView(flag);
				

			}
		});

	}

	/*
	 *  
	 */
	public void initMainView() {
		bodyView = (LinearLayout) findViewById(R.id.bodyL);
		home = (LinearLayout) findViewById(R.id.home);
		gamebox = (LinearLayout) findViewById(R.id.gamebox);
		team = (LinearLayout) findViewById(R.id.team);
		more = (LinearLayout) findViewById(R.id.more);
	}

	//  
	public void showView(int flag) {
		switch (flag) {
		case 0:
			showHome();
			break;
		case 1:
			showGamebox();
			break;
		case 2:
			showTeam();
			break;
		case 3:
			showMore();
			break;
		default:
			break;
		}
	}

	public void showHome() {
		bodyView.removeAllViews();
		bodyView.addView(getLocalActivityManager().startActivity("home",
				new Intent(ActsGroup.this, MainActivity.class)).getDecorView());
		home.setBackgroundResource(R.drawable.tab_highlight);
		gamebox.setBackgroundResource(R.drawable.tab_background);
		more.setBackgroundResource(R.drawable.tab_background);
		team.setBackgroundResource(R.drawable.tab_background);
	}

	public void showGamebox() {
			bodyView.removeAllViews();
			bodyView.addView(getLocalActivityManager().startActivity(
					"gamebox", new Intent(ActsGroup.this, Menu2Activity.class))
					.getDecorView());
			gamebox.setBackgroundResource(R.drawable.tab_highlight);
			home.setBackgroundResource(R.drawable.tab_background);
			more.setBackgroundResource(R.drawable.tab_background);
			team.setBackgroundResource(R.drawable.tab_background);
	}

	public void showTeam() {
		bodyView.removeAllViews();
		bodyView.addView(getLocalActivityManager().startActivity("team",
				new Intent(ActsGroup.this, Menu2Activity.class)).getDecorView());
		team.setBackgroundResource(R.drawable.tab_highlight);
		home.setBackgroundResource(R.drawable.tab_background);
		more.setBackgroundResource(R.drawable.tab_background);
		gamebox.setBackgroundResource(R.drawable.tab_background);
	}
	public void showMore() {
		bodyView.removeAllViews();
		bodyView.addView(getLocalActivityManager().startActivity("more",
				new Intent(ActsGroup.this, Menu2Activity.class)).getDecorView());
		more.setBackgroundResource(R.drawable.tab_highlight);
		home.setBackgroundResource(R.drawable.tab_background);
		team.setBackgroundResource(R.drawable.tab_background);
		gamebox.setBackgroundResource(R.drawable.tab_background);
	}
}
 
xml 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:orientation="vertical"
    android:layout_height="fill_parent">
  	<!-- -->
	<LinearLayout android:orientation="vertical" 
		          android:layout_width="fill_parent" 
		          android:layout_height="fill_parent"
		          android:id="@+id/bodyL" 
		          android:layout_weight="0.95">
    </LinearLayout>
	<!--   -->
	<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
	             android:id="@+id/bottomlist"
		         android:orientation="horizontal" 
		         android:layout_width="fill_parent"
		         android:layout_height="wrap_content" 
		         android:layout_weight="0.05">		 
		<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
			          android:orientation="vertical" 
			          android:layout_width="wrap_content"
			          android:layout_height="fill_parent" 
			          android:id="@+id/home"
		              android:layout_weight="5" 
		              android:gravity="center_horizontal">
			<ImageView android:background="@drawable/home" 
			           android:layout_gravity="top|center" 
			           android:layout_height="35dp" 
			           android:layout_width="32dp" 
			           android:layout_marginTop="4dp"/>
			<TextView  android:layout_width="wrap_content"
				       android:layout_height="20dp" 
				       android:text="@string/home"	
				/>
		</LinearLayout> 
		<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
			          android:orientation="vertical" 
			          android:layout_width="wrap_content"
			          android:layout_height="fill_parent" 
			          android:id="@+id/gamebox"  
			          android:layout_weight="5" 
			          android:gravity="center_horizontal">
			<ImageView android:background="@drawable/gamebox" 
			           android:layout_gravity="top|center" 
			           android:layout_height="35dp" 
			           android:layout_width="32dp" 
			           android:layout_marginTop="4dp"/>
			<TextView android:layout_width="wrap_content"
				      android:layout_height="20dp" 
				      android:text="@string/gamebox"
				/>
		</LinearLayout>
		<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
			          android:orientation="vertical" 
			          android:layout_width="wrap_content"
			          android:id="@+id/team" 
			          android:layout_height="fill_parent"
			          android:layout_weight="5" 
			          android:gravity="center_horizontal">
			<ImageView android:background="@drawable/team" 
			          android:layout_gravity="top|center" 
			          android:layout_height="35dp" 
			          android:layout_width="32dp" 
			          android:layout_marginTop="4dp"
			 />
			<TextView android:layout_width="wrap_content"
				      android:layout_height="20dp"
			          android:text="@string/team"
			/>
		</LinearLayout>
		<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
			         android:orientation="vertical" 
			         android:layout_width="wrap_content"
			         android:id="@+id/more" 
			         android:layout_height="fill_parent"
			         android:layout_weight="5" 
			         android:gravity="center_horizontal">
			<ImageView android:background="@drawable/more" 
			         android:layout_gravity="top|center" 
			         android:layout_height="35dp" 
			         android:layout_width="32dp" 
			         android:layout_marginTop="4dp"
			   />
			<TextView android:layout_width="wrap_content"
				     android:layout_height="20dp" 
				     android:text="@string/more"
				/>
		</LinearLayout>
	</LinearLayout>
</LinearLayout>