关于FragmentTabHost的使用

发布时间:2020-07-29 22:47:20 作者:小诚诚cc
来源:网络 阅读:683

    近期刚学了Fragment,突然想在Fragment中实现TabHost,查阅相关资料后发现现在TabHost已经被FragmentTabHost替代了,因此就想着学习一下,并记录下来,接下来把一些心得分享一下。

一、使用场景

    首先FragmentTabHost不仅可以在FragmentActivity中使用,也可以在Fragment中使用,通过与Fragment的结合实现TabHost的效果

二、使用步骤

    1、布局文件  

    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout 

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"

        android:layout_height="match_parent" >

    

        <android.support.v4.app.FragmentTabHost

        android:id="@android:id/tabhost"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

    

         <LinearLayout

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:orientation="vertical" >

    

                <TabWidget

                    android:id="@android:id/tabs"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:layout_weight="0"

                    android:orientation="horizontal" />

    

                <FrameLayout

                    android:id="@android:id/tabcontent"

                    android:layout_width="0dp"

                    android:layout_height="0dp"

                    android:layout_weight="0" />

    

                <FrameLayout

                    android:id="@+id/realtabcontent"

                    android:layout_width="match_parent"

                    android:layout_height="0dp"

                    android:layout_weight="1" />

            </LinearLayout>

        </android.support.v4.app.FragmentTabHost>

    </RelativeLayout>

    2、代码:

        1)继承FragmentActivity——OnCreate()中:

            FragmentTabHost fragmentTabHost =                                                                                      (FragmentTabHost)findViewById(android.R.id.tabhost);

            fragmentTabHost.setup(this, getFragmentManager(), R.id.realtabcontent);

            fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab1").setIndicator("我的意                                         见"), SettingFragment.class, null);

            fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab2").setIndicator("常见问                                         题"), NotificationFragment.class, null);

        2)继承Fragment——OnCreateView()中:

             View layout = inflater.inflate(R.layout.fragment_sliding_feedback, null);

             FragmentTabHost fragmentTabHost =                                                                             (FragmentTabHost)layout.findViewById(android.R.id.tabhost);

            fragmentTabHost.setup(this, getChildFragmentManager(), R.id.realtabcontent);

            fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab1").setIndicator("我的意                                         见"), SettingFragment.class, null);

            fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab2").setIndicator("常见问                                         题"), NotificationFragment.class, null);

            return layout;





    

推荐阅读:
  1. PostMan的使用
  2. bbcp的使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

记录 资料

上一篇:OSSIM下自动化配置管理工具Ansible实战

下一篇:angularJS分页

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》