135-1821-9792

关于FragmentTabHost的使用-创新互联

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

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、成都微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了清河免费建站欢迎大家使用!

一、使用场景

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

二、使用步骤

  1、布局文件

    

    

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

      android:layout_width="match_parent"

      android:layout_height="match_parent" >

      

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

       android:layout_width="match_parent"

       android:layout_height="wrap_content">

       

          android:layout_width="match_parent"

          android:layout_height="match_parent"

          android:orientation="vertical" >

          

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

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="0"

            android:orientation="horizontal" />

          

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

            android:layout_width="0dp"

            android:layout_height="0dp"

            android:layout_weight="0" />

          

            android:id="@+id/realtabcontent"

            android:layout_width="match_parent"

            android:layout_height="0dp"

            android:layout_weight="1" />

        

      

    

    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;

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享题目:关于FragmentTabHost的使用-创新互联
网页网址:http://kswsj.com/article/diosdc.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号