介绍
这篇文章给大家介绍怎么在android中利用FragmentTabhost实现导航分页,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
基本模板
public class MainActivity extends FragmentActivity { ,, private 才能;FragmentTabHost  mTabHost; private 才能;LayoutInflater  mLayoutInflater; private 才能;Class  mFragmentArray [],=, {, Fragment1.class, Fragment2.class, ,,,,,,,Fragment3.class Fragment4.class,, Fragment5.class }; private 才能;int  mImageArray [],=, {R.drawable.tab_home_btn, ,,,,,,,R.drawable.tab_message_btn R.drawable.tab_selfinfo_btn, ,,,,,,,R.drawable.tab_square_btn R.drawable.tab_more_btn }; private 才能;String  mTextArray[],=,{,“首页“,,“消息,,,“好友“,,“搜索”,,“更多“,}; public 才能;void  onCreate (Bundle savedInstanceState), { ,,,super.onCreate (savedInstanceState); ,,,setContentView (R.layout.activity_main); ,,,initView (); ,,} private 才能;void  initView (), { ,,,mLayoutInflater =, LayoutInflater.from(这个); ,,,//,找到TabHost ,,,mTabHost =, (FragmentTabHost), findViewById (android.R.id.tabhost); ,,,mTabHost.setup (,, getSupportFragmentManager (),, R.id.realtabcontent); ,mTabHost.getTabWidget () .setDividerDrawable (null);//去除分割线 ,,,//,得到片段的个数 ,,,for (int 小姐:=,0;,小姐:& lt;, mFragmentArray.length;,我+ +),{ ,,,,,//,给每个标签按钮设置图标,文字和内容 ,,,,,TabSpec TabSpec =, mTabHost.newTabSpec (mTextArray[我]) ,,,,,,,,,.setIndicator (getTabItemView(我)); ,,,,,//,将标签按钮添加进标签选项卡中 ,,,,,mTabHost.addTab (tabSpec, mFragmentArray[我],null); ,,,,,//,设置标签按钮的背景 ,,,,,mTabHost.getTabWidget () .getChildAt(我) ,,,,,,,,,.setBackgroundResource (R.drawable.selector_tab_background); ,,,} ,,}//才能给每个标签按钮设置图标和文字 private 才能;View  getTabItemView (int 指数),{ ,,,View View =, mLayoutInflater.inflate (R.layout.tab_item_view, null); ,,,ImageView ImageView =, view.findViewById (R.id.imageview); ,,,//设置图片选择器,选中的标签改变图标 ,,,switch (指数){ ,,,,,case 0: imageView.setImageResource (R.drawable.main_bottom_image_selector);断裂; ,,,,,case 1: imageView.setImageResource (R.drawable.main_bottom_image_selector2);断裂; ,,,,,case 2: imageView.setImageResource (R.drawable.main_bottom_image_selector3);断裂; ,,,,,case 3: imageView.setImageResource (R.drawable.main_bottom_image_selector4);断裂; ,,,,,case 4: imageView.setImageResource (R.drawable.main_bottom_image_selector5);断裂; ,,,} ,,,TextView TextView =, view.findViewById (R.id.textview); ,,,textView.setText (mTextArray[指数]); ,,,//设置文本选择器,选中的标签文字高亮 ,,,textView.setTextColor (R.drawable.main_bottom_text_selector); ,,,return ; ,,} }
acitivity_main。xml
& lt; ? xml version=?.0“,编码=皍tf-8" ?比; & lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android" ,android: layout_width=癴ill_parent" ,android: layout_height=癴ill_parent" ,android:取向=皏ertical",祝辞 & lt; FrameLayout android:才能id=癅 + id/realtabcontent" android:才能layout_width=癴ill_parent" android:才能layout_height=? dip" android:才能layout_weight=?“,/比; & lt; android.support.v4.app.FragmentTabHost android: id=安拍蹳android: id/tabhost",//必须使用提供的id android:才能layout_width=癴ill_parent" android:才能layout_height=皐rap_content", android:才能背景=癅drawable/bg_tabhost_bg"比; & lt; FrameLayout才能 ,,android: id=癅android: id/tabcontent",//必须使用提供的id null null null null null null null null null null null null null null null null null null null null null null null null null null null null怎么在android中利用FragmentTabhost实现导航分页