android FragmentTabhost导航分页
程序员文章站
2024-02-01 20:50:34
基本模板 acitivity_main.xml tab_item_view.xml main_bottom_image_selector图片选择器 注:模板有5个tab,需要5个图片选择器,还需要5个文本选择器,还需要创建5个fragment。 ......
基本模板
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(this); // 找到tabhost mtabhost = (fragmenttabhost) findviewbyid(android.r.id.tabhost); mtabhost.setup(this, getsupportfragmentmanager(), r.id.realtabcontent); mtabhost.gettabwidget().setdividerdrawable(null);//去除分割线 // 得到fragment的个数 for (int i = 0; i < mfragmentarray.length; i++) { // 给每个tab按钮设置图标、文字和内容 tabspec tabspec = mtabhost.newtabspec(mtextarray[i]) .setindicator(gettabitemview(i)); // 将tab按钮添加进tab选项卡中 mtabhost.addtab(tabspec, mfragmentarray[i], null); // 设置tab按钮的背景 mtabhost.gettabwidget().getchildat(i) .setbackgroundresource(r.drawable.selector_tab_background); } } //给每个tab按钮设置图标和文字 private view gettabitemview(int index) { view view = mlayoutinflater.inflate(r.layout.tab_item_view, null); imageview imageview = view.findviewbyid(r.id.imageview); //设置图片选择器,选中的tab改变图标 switch (index){ case 0:imageview.setimageresource(r.drawable.main_bottom_image_selector);break; case 1:imageview.setimageresource(r.drawable.main_bottom_image_selector2);break; case 2:imageview.setimageresource(r.drawable.main_bottom_image_selector3);break; case 3:imageview.setimageresource(r.drawable.main_bottom_image_selector4);break; case 4:imageview.setimageresource(r.drawable.main_bottom_image_selector5);break; } textview textview = view.findviewbyid(r.id.textview); textview.settext(mtextarray[index]); //设置文本选择器,选中的tab文字高亮 textview.settextcolor(r.drawable.main_bottom_text_selector); return view; } }
acitivity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <framelayout android:id="@+id/realtabcontent" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <android.support.v4.app.fragmenttabhost android:id="@android:id/tabhost" //必须使用提供的id android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/bg_tabhost_bg"> <framelayout android:id="@android:id/tabcontent" //必须使用提供的id android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.fragmenttabhost> </linearlayout>
tab_item_view.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <imageview android:id="@+id/imageview" android:layout_width="40dp" android:layout_height="40dp" android:layout_margin="3dp" /> <textview android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="1dp" android:textsize="12sp" android:layout_marginbottom="2dp"/> </linearlayout>
main_bottom_image_selector图片选择器
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="false" android:drawable="@drawable/ic_launcher" /> <item android:state_selected="true" android:drawable="@drawable/ic_launcher_round" /> </selector>
注:模板有5个tab,需要5个图片选择器,还需要5个文本选择器,还需要创建5个fragment。