button在底部的多个view切换<实例一正常切换>

1. 镶嵌View的主ActivityGroup

package com.isomobile.widgets;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends ActivityGroup implements View.OnClickListener {
    private final static Class<?>[] sActivityClasses = {
            Activity1.class, Activity2.class, Activity3.class, Activity4.class, Activity5.class
    };

    private final static int[] sResIds = {
            R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5
    };

    private final static String[] sActivityIds = {
            "Activity1", "Activity2", "Activity3", "Activity4", "Activity5"
    };

    private RelativeLayout mViewContainer;

    private Button[] mBtns = new Button[sResIds.length];
    
    private int mCurId = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupViews();
    }

    private void setupViews() {
        mViewContainer = (RelativeLayout) findViewById(R.id.container);
        final Button[] btns = mBtns;
        for(int i=0; i< btns.length; i++) {
            btns[i] = (Button) findViewById(sResIds[i]);
            btns[i].setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        if(mCurId == id) {
            return ;
        }
        mCurId = id;
        processViews(id);
    }

    private void processViews(int rid) {
        mViewContainer.removeAllViews();
        final int index = getButtonIndex(rid);
        final View tempView = getLocalActivityManager().startActivity(sActivityIds[index],
                new Intent(this, sActivityClasses[index]).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();
        mViewContainer.addView(tempView);
    }

    private int getButtonIndex(int rid) {
        final int length = sResIds.length;
        for (int i = 0; i < length; i++) {
            if (rid == sResIds[i]) {
                return i;
            }
        }
        return 0;
    }
}

2. 其它子Activity(Activity1~5代码基本雷同这里只给出一个):

package com.isomobile.widgets;

import android.app.Activity;
import android.os.Bundle;

public class Activity1 extends Activity {

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

3. Main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout
        android:id="@+id/container"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
    </RelativeLayout>
    <include
        layout="@layout/bottom_buttons" />
</RelativeLayout>

4.bottom_buttons.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:weightSum="5.0"
    android:orientation="horizontal">
    <Button
        android:id="@+id/btn1"
        android:layout_gravity="bottom"
        android:gravity="bottom"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1.0" />
    <Button
        android:id="@+id/btn2"
        android:layout_gravity="bottom"
        android:gravity="bottom"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1.0" />
    <Button
        android:id="@+id/btn3"
        android:layout_gravity="bottom"
        android:gravity="bottom"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1.0" />
    <Button
        android:id="@+id/btn4"
        android:layout_gravity="bottom"
        android:gravity="bottom"
        android:layout_width="0dip"
        android:layout_weight="1.0"
        android:layout_height="fill_parent" />
    <Button
        android:id="@+id/btn5"
        android:layout_gravity="bottom"
        android:gravity="bottom"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1.0" />
</LinearLayout>

效果图片如下:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值