Android开发:TabHost嵌套Activity,实现底部菜单栏

项目需要,现在需要把主页换成GridView和底部菜单栏组合的形式,但是这和当初设计的有点出入,代码全部使用Activity写的。现在有很多好用的底部菜单栏的控件,但是基本都是嵌套Fragment。后来想到用TabHost嵌套Activity来实现底部菜单栏的功能。接下来,我们开始吧~

效果图:



新建Home页,取名HomeActivity,在这里来组合TabHost和Activity,直接看代码吧:

注释:

HomeActivity :父Activity

IndexActivity:子Activity

DailyCheckUploadForIndexActivity:子Activity

DailyThingsUploadForIndexActivity:子Activity

TaskMainForIndexActivity:子Activity

SystemSettingForIndexActivity:子Activity


1、

public class HomeActivity extends TabActivity {//如果子Activity中有startActivityForResult,需要继承TabActivity;否则不需要
    //用于添加每一个选项卡的id
    private String[] tags = {"A_tag", "B_tag", "C_tag", "D_tag", "E_tag"};
    //所添加选项卡的文本信息
    private String[] titles = {"主页", "上报病害", "上报事项", "工作任务", "系统设置"};
    //所添加选项卡的图片信息
    private int[] images = {R.drawable.bottom_zhuye_gray, R.drawable.bottom_shangbaobinghai_gray,
            R.drawable.bottom_shangbaoshixiang_gray, R.drawable.bottom_gongzuorenwu_gray, R.drawable.bottom_setting_gray};
    //用于跳转至不同的Activity
    private Intent[] intents = new Intent[5];
    TextView tv_1;
    TextView tv_2;
    TextView tv_3;
    TextView tv_4;
    TextView tv_5;
    ImageView iv_1;
    ImageView iv_2;
    ImageView iv_3;
    ImageView iv_4;
    ImageView iv_5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_layout);
        final TabHost tabHost = getTabHost();
        TabWidget tabWidget = getTabWidget();
        tabWidget.setBackgroundColor(Color.WHITE);
        //坑人,下面LocalActivityManager没必要加,否则和调用getLocalActivityManager().getCurrentActivity()冲突,返回           //值总为空
        //初始化activity管理者
//        LocalActivityManager manager = new LocalActivityManager(HomeActivity.this, false);
        //通过管理者保存当前页面状态
//        manager.dispatchCreate(savedInstanceState);
        //将管理者类对象添加至TabHost
//        tabHost.setup(manager);
        tabHost.setup();
        init_intent();
        for (int i = 0; i < intents.length; i++) {
            //加载底部导航栏布局
            LayoutInflater inflater = this.getLayoutInflater();
            View view = inflater.inflate(R.layout.tab, null);
            TextView textView = (TextView) view.findViewById(R.id.tv_item);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            textView.setText(titles[i]);
            imageView.setImageResource(images[i]);
            //创建选项卡
            TabHost.TabSpec spec = tabHost.newTabSpec(tags[i]);
            spec.setIndicator(view);
            //设置每个页面的内容
            spec.setContent(intents[i]);
            //将创建的选项卡添加至tabHost上
            tabHost.addTab(spec);
        }

//        tabHost.setCurrentTab(2);

//获取每个子菜单中的控件,用于菜单颜色的改变

        tv_1 = (TextView) tabHost.getTabWidget().getChildTabViewAt(0)
                .findViewById(R.id.tv_item);
        iv_1 = (ImageView) tabHost.getTabWidget().getChildTabViewAt(0)
                .findViewById(R.id.image);
        tv_2 = (TextView) tabHost.getTabWidget().getChildTabViewAt(1)
                .findViewById(R.id.tv_item);
        iv_2 = (ImageView) tabHost.getTabWidget().getChildTabViewAt(1)
                .findViewById(R.id.image);
        tv_3 = (TextView) tabHost.getTabWidget().getChildTabViewAt(2)
                .findViewById(R.id.tv_item);
        iv_3 = (ImageView) tabHost.getTabWidget().getChildTabViewAt(2)
                .findViewById(R.id.image);
        tv_4 = (TextView) tabHost.getTabWidget().getChildTabViewAt(3)
                .findViewById(R.id.tv_item);
        iv_4 = (ImageView) tabHost.getTabWidget().getChildTabViewAt(3)
                .findViewById(R.id.image);
        tv_5 = (TextView) tabHost.getTabWidget().getChildTabViewAt(4)
                .findViewById(R.id.tv_item);
        iv_5 = (ImageView) tabHost.getTabWidget().getChildTabViewAt(4)
                .findViewById(R.id.image);
        iv_1.setImageDrawable(getResources().getDrawable(R.drawable.bottom_zhuye_blue));
        tv_1.setTextColor(getResources().getColor(R.color.blue_tab));
        tv_1.getPaint().setFakeBoldText(true);
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {//设置选中的按钮字体颜色
                switch (tabId) {
                    case "A_tag":
                        iv_1.setImageDrawable(getResources().getDrawable(R.drawable.bottom_zhuye_blue));
                        tv_1.setTextColor(getResources().getColor(R.color.blue_tab));
                        tv_1.getPaint().setFakeBoldText(true);
                        iv_2.setImageDrawable(getResources().getDrawable(R.drawable.bottom_shangbaobinghai_gray));
                        tv_2.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_2.getPaint().setFakeBoldText(false);
                        iv_3.setImageDrawable(getResources().getDrawable(R.drawable.bottom_shangbaoshixiang_gray));
                        tv_3.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_3.getPaint().setFakeBoldText(false);
                        iv_4.setImageDrawable(getResources().getDrawable(R.drawable.bottom_gongzuorenwu_gray));
                        tv_4.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_4.getPaint().setFakeBoldText(false);
                        iv_5.setImageDrawable(getResources().getDrawable(R.drawable.bottom_setting_gray));
                        tv_5.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_5.getPaint().setFakeBoldText(false);
                        break;
                    case "B_tag":
                        iv_1.setImageDrawable(getResources().getDrawable(R.drawable.bottom_zhuye_gray));
                        tv_1.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_1.getPaint().setFakeBoldText(false);
                        iv_2.setImageDrawable(getResources().getDrawable(R.drawable.bottom_shangbaobinghai_blue));
                        tv_2.setTextColor(getResources().getColor(R.color.blue_tab));
                        tv_2.getPaint().setFakeBoldText(true);
                        iv_3.setImageDrawable(getResources().getDrawable(R.drawable.bottom_shangbaoshixiang_gray));
                        tv_3.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_3.getPaint().setFakeBoldText(false);
                        iv_4.setImageDrawable(getResources().getDrawable(R.drawable.bottom_gongzuorenwu_gray));
                        tv_4.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_4.getPaint().setFakeBoldText(false);
                        iv_5.setImageDrawable(getResources().getDrawable(R.drawable.bottom_setting_gray));
                        tv_5.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_5.getPaint().setFakeBoldText(false);
                        break;
                    case "C_tag":
                        iv_1.setImageDrawable(getResources().getDrawable(R.drawable.bottom_zhuye_gray));
                        tv_1.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_1.getPaint().setFakeBoldText(false);
                        iv_2.setImageDrawable(getResources().getDrawable(R.drawable.bottom_shangbaobinghai_gray));
                        tv_2.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_2.getPaint().setFakeBoldText(false);
                        iv_3.setImageDrawable(getResources().getDrawable(R.drawable.bottom_shangbaoshixiang_blue));
                        tv_3.setTextColor(getResources().getColor(R.color.blue_tab));
                        tv_3.getPaint().setFakeBoldText(true);
                        iv_4.setImageDrawable(getResources().getDrawable(R.drawable.bottom_gongzuorenwu_gray));
                        tv_4.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_4.getPaint().setFakeBoldText(false);
                        iv_5.setImageDrawable(getResources().getDrawable(R.drawable.bottom_setting_gray));
                        tv_5.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_5.getPaint().setFakeBoldText(false);
                        break;
                    case "D_tag":
                        iv_1.setImageDrawable(getResources().getDrawable(R.drawable.bottom_zhuye_gray));
                        tv_1.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_1.getPaint().setFakeBoldText(false);
                        iv_2.setImageDrawable(getResources().getDrawable(R.drawable.bottom_shangbaobinghai_gray));
                        tv_2.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_2.getPaint().setFakeBoldText(false);
                        iv_3.setImageDrawable(getResources().getDrawable(R.drawable.bottom_shangbaoshixiang_gray));
                        tv_3.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_3.getPaint().setFakeBoldText(false);
                        iv_4.setImageDrawable(getResources().getDrawable(R.drawable.bottom_gongzuorenwu_blue));
                        tv_4.setTextColor(getResources().getColor(R.color.blue_tab));
                        tv_4.getPaint().setFakeBoldText(true);
                        iv_5.setImageDrawable(getResources().getDrawable(R.drawable.bottom_setting_gray));
                        tv_5.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_5.getPaint().setFakeBoldText(false);
                        break;
                    case "E_tag":
                        iv_1.setImageDrawable(getResources().getDrawable(R.drawable.bottom_zhuye_gray));
                        tv_1.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_1.getPaint().setFakeBoldText(false);
                        iv_2.setImageDrawable(getResources().getDrawable(R.drawable.bottom_shangbaobinghai_gray));
                        tv_2.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_2.getPaint().setFakeBoldText(false);
                        iv_3.setImageDrawable(getResources().getDrawable(R.drawable.bottom_shangbaoshixiang_gray));
                        tv_3.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_3.getPaint().setFakeBoldText(false);
                        iv_4.setImageDrawable(getResources().getDrawable(R.drawable.bottom_gongzuorenwu_gray));
                        tv_4.setTextColor(getResources().getColor(R.color.gray_pressed));
                        tv_4.getPaint().setFakeBoldText(false);
                        iv_5.setImageDrawable(getResources().getDrawable(R.drawable.bottom_setting_blue));
                        tv_5.setTextColor(getResources().getColor(R.color.blue_tab));
                        tv_5.getPaint().setFakeBoldText(true);
                        break;
                    default:
                        break;
                }
            }
        });
    }


    //每个页面放置的Activity

    public void init_intent() {

    //Intent添加 addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),每次切换底部菜单时会重新加载Activity

        Intent intent_0 = new Intent(this, IndexActivity.class);
        intent_0.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        Intent intent_1 = new Intent(this, DailyCheckUploadForIndexActivity.class);
        intent_1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Intent intent_2 = new Intent(this, DailyThingsUploadForIndexActivity.class);
        intent_2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Intent intent_3 = new Intent(this, TaskMainForIndexActivity.class);
        intent_3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Intent intent_4 = new Intent(this, SystemSettingForIndexActivity.class);
        intent_4.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intents[0] = intent_0;
        intents[1] = intent_1;
        intents[2] = intent_2;
        intents[3] = intent_3;
        intents[4] = intent_4;
    }

//用于解决子Activity中onActivityResult()方法无效的情况
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //获取当前活动的Activity实例
        Activity subActivity = this.getLocalActivityManager().getCurrentActivity();
//判断是否实现返回值接口
        if (subActivity instanceof OnTabActivityResultListener) {
            //获取返回值实例接口
            OnTabActivityResultListener listener = (OnTabActivityResultListener) subActivity;
            //转发请求到子Activity
            listener.onTabActivityResult(requestCode, resultCode, data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    /**
     * 启动HomeActivity
     *
     * @param context 上下文
     */
    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, HomeActivity.class);
        context.startActivity(intent);
    }

}

2、HomeActivity 的布局文件

<?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:fitsSystemWindows="true"
    android:orientation="vertical">
    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
            <!--添加选项卡容器-->
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/gray_pressed" />
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="60dp" />
        </LinearLayout>
    </TabHost>

</LinearLayout>

3、tab布局

<?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:gravity="center"
    android:orientation="vertical">
    <!--tabWeight布局-->
    <ImageView
        android:id="@+id/image"
        android:layout_width="24dp"
        android:layout_height="24dp" />
    <TextView
        android:id="@+id/tv_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/gray_pressed"
        android:textSize="@dimen/fontsize_12" />
</LinearLayout>

4、如果子Activity中没有startActivityForResult()和onActivityResult()方法,到这里就可以实现底部菜单栏了。



5、 如果子Activity中有startActivityForResult(),接着往下看:

如果子Activity中startActivityForResult()调用其他Activity时候遭遇到onActivityResult方法不响应

解决四步法:

a.通过父Activity启动其他Activity;

b.实现父Activity的onActivityResult方法,将该处接收到的请求转发给当前活动的子Activity;

c.自定义一个转发接口用来实现第2步中的转发功能;

d.子Activity中实现第3步接口中的方法用来接收返回信息;


 代码:

a.通过父Activity启动其他Activity

getParent().startActivityForResult(new Intent(...), REQUEST_CODE);  


b.实现父Activity的onActivityResult方法,将该处接收到的请求转发给当前活动的子Activity

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //获取当前活动的Activity实例
        Activity subActivity = this.getLocalActivityManager().getCurrentActivity();
//判断是否实现返回值接口
        if (subActivity instanceof OnTabActivityResultListener) {
            //获取返回值实例接口
            OnTabActivityResultListener listener = (OnTabActivityResultListener) subActivity;
            //转发请求到子Activity
            listener.onTabActivityResult(requestCode, resultCode, data);
        }
        super.onActivityResult(requestCode, resultCode, data);

    }


c.自定义一个转发接口用来实现第2步中的转发功能

public interface OnTabActivityResultListener {
    public void onTabActivityResult(int requestCode, int resultCode, Intent data);

}


d.子Activity中实现第3步接口中的方法用来接收返回信息

@Override  
    public void onTabActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {  
            //实现该处逻辑  
        }  
    }  

e.子Activity一定要实现implements   OnTabActivityResultListener ,重写onTabActivityResult()

public void onTabActivityResult(int requestCode, int resultCode, Intent data) {  
        if (resultCode == RESULT_OK) {  
            switch (requestCode) {  
            case SUB_REQUEST_CODE:  
                //Sub_History_visit();  
                break;  
  
            default:  
                break;  
            }  
        }  
    }  


6、如果父Activity的onActivityResult()方法中 getLocalActivityManager().getCurrentActivity()==null,检查Activity 中是否有对LocalActivityManager 进行操作,有的话,删除或者注释。

 到这里TabHost嵌套Activity底部菜单栏就实现了

如有问题请加Q:1255376541

学习累了,休息一下吧!关注微信公众号“休闲1刻”,或扫描下方二维码,休闲段子等着你~





猜你喜欢

转载自blog.csdn.net/android157/article/details/80448798