,,,,近期刚学了片段,突然想在片段中实现TabHost,查阅相关资料后发现现在TabHost已经被FragmentTabHost替代了,因此就想着学习一下,并记录下来,接下来把一些心得分享一下。
一、使用场景
,,,,首先FragmentTabHost不仅可以在FragmentActivity中使用,也可以在片段中使用,通过与片段的结合实现TabHost的效果
二、使用步骤
,,1、布局文件,
,,,, & lt; ?xml version=" 1.0 " encoding=" utf - 8 " ?在
,,,, & lt; RelativeLayout
,,,,,,xmlns: android=" http://schemas.android.com/apk/res/android "
,,,,,,android: layout_width=" match_parent "
,,,,,,android: layout_height=" match_parent "在
,,,,
,,,,,,& lt; android.support.v4.app.FragmentTabHost
,,,,,,android: id=" @android: id/tabhost”
,,,,,,android: layout_width=" match_parent "
,,,,,,android: layout_height=" wrap_content "在
,,,,
,,,,,,& lt; LinearLayout
,,,,,,,,,,android: layout_width=" match_parent "
,,,,,,,,,,android: layout_height=" match_parent "
,,,,,,,,,,android:取向=按怪薄痹?/p>
,,,,
,,,,,,,,,,& lt; TabWidget
,,,,,,,,,,,,android: id=" @android: id/标签”
,,,,,,,,,,,,android: layout_width=" match_parent "
,,,,,,,,,,,,android: layout_height=" wrap_content "
,,,,,,,,,,,,android: layout_weight=" 0 "
,,,,,,,,,,,,面向android:="水平"/祝辞
,,,,
,,,,,,,,,,& lt; FrameLayout
,,,,,,,,,,,,android: id=" @android: id/tabcontent”
,,,,,,,,,,,,android: layout_width=" 0 dp "
,,,,,,,,,,,,android: layout_height=" 0 dp "
,,,,,,,,,,,,android: layout_weight=" 0 "/祝辞
,,,,
,,,,,,,,,,& lt; FrameLayout
,,,,,,,,,,,,android: id=癅 + id/realtabcontent”
,,,,,,,,,,,,android: layout_width=" match_parent "
,,,,,,,,,,,,android: layout_height=" 0 dp "
,,,,,,,,,,,,android: layout_weight=" 1 "/祝辞
,,,,,,,,& lt;/LinearLayout>
,,,,,,& lt;/android.support.v4.app.FragmentTabHost>
,,,, & lt;/RelativeLayout>
,,,, 2代码:
,,,,,,,, 1)继承FragmentActivity——OnCreate()中:
,,,,,,,,,,,, FragmentTabHost FragmentTabHost=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,(FragmentTabHost) findViewById (android.R.id.tabhost);
,,,,,,fragmentTabHost。设置(这getFragmentManager ()、R.id.realtabcontent);
,,,,,,fragmentTabHost.addTab (fragmentTabHost.newTabSpec (tab1”)。setIndicator(“我的意,,,,,,,,,,,,,,,,,,,,见”),SettingFragment。类,null);
,,,,,,fragmentTabHost.addTab (fragmentTabHost.newTabSpec (“tab2”)。setIndicator(“常见问,,,,,,,,,,,,,,,,,,,,题”),NotificationFragment。null