Android界面之ActivityGroup的使用

首先我们先创建一个底部的布局文件

<?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="horizontal" >

    <LinearLayout
        android:id="@+id/lay_one"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/museum_selected" />

        <TextView
            android:id="@+id/tv_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="one" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lay_two"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/consult_selected" />

        <TextView
            android:id="@+id/tv_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="two" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lay_three"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/nav_selected" />

        <TextView
            android:id="@+id/tv_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="three" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lay_four"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img_four"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/more_selected" />

        <TextView
            android:id="@+id/tv_four"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="four" />
    </LinearLayout>

</LinearLayout>


这是第一个布局,其他三个布局和这个一样,就不打了

<?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" >
    

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="one"/>
</LinearLayout>


然后再把第一个布局和底部布局引用到主布局当中


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <include
        android:id="@+id/One"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@layout/activity_one" />

    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        layout="@layout/activity_bottom" />

</RelativeLayout>

接下来是Java文件



mport android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends ActivityGroup implements OnClickListener {

	private LinearLayout lay_one, lay_two, lay_three, lay_four, lay_bottom;
	private LinearLayout One, Two, Three, Four;
	private ImageView image_one, image_two, image_three, image_four;
	private TextView tv_one, tv_two, tv_three, tv_four;
	private int flag = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
		showView(flag);
		image_one.setBackgroundResource(R.drawable.museum);
		tv_one.setTextColor(Color.RED);
	}

	private void initView() {
		lay_one = (LinearLayout) findViewById(R.id.lay_one);
		lay_two = (LinearLayout) findViewById(R.id.lay_two);
		lay_three = (LinearLayout) findViewById(R.id.lay_three);
		lay_four = (LinearLayout) findViewById(R.id.lay_four);

		One = (LinearLayout) findViewById(R.id.One);
		Two = (LinearLayout) findViewById(R.layout.activity_two);
		Three = (LinearLayout) findViewById(R.layout.activity_three);
		Four = (LinearLayout) findViewById(R.layout.activity_four);

		image_one = (ImageView) findViewById(R.id.img_one);
		image_two = (ImageView) findViewById(R.id.img_two);
		image_three = (ImageView) findViewById(R.id.img_three);
		image_four = (ImageView) findViewById(R.id.img_four);

		tv_one = (TextView) findViewById(R.id.tv_one);
		tv_two = (TextView) findViewById(R.id.tv_two);
		tv_three = (TextView) findViewById(R.id.tv_three);
		tv_four = (TextView) findViewById(R.id.tv_four);

		lay_one.setOnClickListener(this);
		lay_two.setOnClickListener(this);
		lay_three.setOnClickListener(this);
		lay_four.setOnClickListener(this);

	}

	public void onClick(View v) {
		image_one.setBackgroundResource(R.drawable.museum_selected);
		image_two.setBackgroundResource(R.drawable.consult_selected);
		image_three.setBackgroundResource(R.drawable.nav_selected);
		image_four.setBackgroundResource(R.drawable.more_selected);

		tv_one.setTextColor(Color.BLACK);
		tv_two.setTextColor(Color.BLACK);
		tv_three.setTextColor(Color.BLACK);
		tv_four.setTextColor(Color.BLACK);

		switch (v.getId()) {
		case R.id.lay_one:
			image_one.setBackgroundResource(R.drawable.museum);
			tv_one.setTextColor(Color.RED);
			flag = 0;
			showView(flag);
			break;
		case R.id.lay_two:
			image_two.setBackgroundResource(R.drawable.consult);
			tv_two.setTextColor(Color.RED);
			flag = 1;
			showView(flag);
			break;
		case R.id.lay_three:
			image_three.setBackgroundResource(R.drawable.nav);
			tv_three.setTextColor(Color.RED);
			flag = 2;
			showView(flag);
			break;
		case R.id.lay_four:
			image_four.setBackgroundResource(R.drawable.more);
			tv_four.setTextColor(Color.RED);
			flag = 3;
			showView(flag);
			break;

		}

	}

	private void showView(int flag) {
		switch (flag) {
		case 0:
			One.removeAllViews();
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, OneActivity.class);
			Window w = getLocalActivityManager().startActivity("One", intent);

			View v = w.getDecorView();
			One.addView(v);

			break;
		case 1:
			One.removeAllViews();
			Intent intent1 = new Intent();
			intent1.setClass(MainActivity.this, TwoActivity.class);
			Window w1 = getLocalActivityManager().startActivity("Two", intent1);
			View v1 = w1.getDecorView();
			One.addView(v1);

			break;
		case 2:
			One.removeAllViews();
			Intent intent2 = new Intent();
			intent2.setClass(MainActivity.this, ThreeActivity.class);
			Window w2 = getLocalActivityManager().startActivity("Three",
					intent2);
			View v2 = w2.getDecorView();
			One.addView(v2);

			break;
		case 3:
			One.removeAllViews();
			Intent intent3 = new Intent();
			intent3.setClass(MainActivity.this, FourActivity.class);
			Window w3 = getLocalActivityManager()
					.startActivity("Four", intent3);
			View v3 = w3.getDecorView();
			One.addView(v3);

			break;

		}

	}

}

代码中间的四个Activity,绑定前面的四个布局,具体的需求根据自己的情况决定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值