TabHost+Fragment的应用

作者: 一s独秀 | 来源:发表于2018-01-09 10:55 被阅读0次

很久没有应用了,东西都忘记了,总结出来,曾经的东西在捡起来一次,加强下记忆

上代码:

  • 在XML的布局:
 <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/backgroudColor">
        <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:background="@color/wirteColor"/>
        <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/wirteColor">
                <LinearLayout
                    android:id="@+id/line_one"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/line_two"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/line_three"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                </LinearLayout>
        </FrameLayout>
</TabHost>
  • java类中的实现:
TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
//添加“主页”Tab到TabHost控件中
 mTabHost.addTab(mTabHost.newTabSpec("home")//Tag
                .setIndicator("HOME")//设置Tab标签和图标
                .setContent(R.id.line_one));        //设置Tab内容
//添加“消息”Tab到TabHost控件中
 mTabHost.addTab(mTabHost.newTabSpec("news")
                .setIndicator("NEWS")
                .setContent(R.id.line_two));
//添加“个人”Tab到TabHost控件中
mTabHost.addTab(mTabHost.newTabSpec("mine")
                .setIndicator("MINE")
                .setContent(R.id.line_three));
//设置当前默认显示第一个Tab
mTabHost.setCurrentTab(0);
 //TabHost改变监听
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        Log.d("===>>>onTabChanged",tabId.toString());
    }
});
  • 下来我们要创建属于三个界面的Fragment(HomeFragment.NewsFragment,MineFragment)
  • 最后分别添加到TabHost的tabcontent中,tabcontent里面有几个子空间,它就有几个tab
//home
HomeFragment nomeFragment= new HomeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_one, homeFragment).commit();
//news
NewsFragment newsFragment = new NewsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_two, newsFragment ).commit();
//mine
MineFragment mineFragment= new MineFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_three, mineFragment).commit();
  • 当然了Tab的标签和图标可以自定义一个View,通过drawable来设置每个Tab选中时的颜色等...(下面只试一个:在layout下定义一个home_item.xml

<!--        === layout中 ===          -->
//只在布局中添加下面就行了,再去drawable创建选中时的字体颜色--home_item_selected
<TextView
        android:textSize="@dimen/sp18"
        android:textColor="@drawable/home_item_selected"
        android:text="@string/string_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

<!--        === drawable中 ===          -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/appthemeColor" />
    <item android:color="@color/textColor" />
</selector>

好了,就这么简单的完了,当然了,TabHost+Fragment的写法有好几种,我这里只用了最简单的这一种,当然,现在由于项目的需求,有些也用不上此方法,因为这个加载数据是一次性全部执行,三个Fragment的生命周期一起执行的

相关文章

  • TabHost+Fragment的应用

    很久没有应用了,东西都忘记了,总结出来,曾经的东西在捡起来一次,加强下记忆 上代码: 在XML的布局: java类...

  • Android控件使用之FragmentTabHost使用

    前言 现在大部分App底部都有一个菜单,实现这个功能也有好多办法: TabHost+Fragment RadioG...

  • 自定义TabHost+Fragment结合使用

    自定义TabHost+Fragment结合使用,实现切换页面,不销毁Fragment界面的功能 1.自定义TabH...

  • 应用案例——视图的应用

    案例的目的 掌握视图的创建、查询、更新和删除操作。假如有来自河北和山东的三个理科学生报考北京大学和清华大学,现在需...

  • this的应用

    例子

  • Android通过包名获取其他应用程序的名称

    查看本应用的应用名称 : 查看其它应用的应用名称 :

  • 应用

    【应用】 $应用$ 【应用名称】王者荣耀【应用名称】 【应用介绍】5v5对战手游【应用介绍】 $应用$ 【应用名称...

  • Day17 - Flutter - 应用信息配置

    概述 应用标识 应用名称 应用图标 应用启动图 一、应用标识 1.1. Android应用标识Android应用标...

  • 应用文的实际应用

    应用文写作语文课程标准(2011年版)中明确要求:学写常见的应用文(5—6年级)、写常见的应用(7—9年级)。众所...

  • iOS应用程序间的跳转

    一.从应用A直接跳转到应用B 以应用A(网易)与应用B(微信)说明,网易应用跳转至微信应用; 1.设置应用B的UR...

网友评论

    本文标题:TabHost+Fragment的应用

    本文链接:https://www.haomeiwen.com/subject/udrwnxtx.html