【笔记——Android】关于使用TabHost和Fragment来创建菜单
关于使用TabHost和Fragment来创建菜单
使用TabHost来切换Fragment的好处是他可以自动管理点击切换,图片切换等,方便一点。
1.首先在需要放置菜单的xml地方加上TabHost的标签
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/maintab_toolbar_bg" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
2.配置菜单上的按钮布局,这里是每一个按钮的布局
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:padding="3dp"
android:src="@drawable/tab_home_btn" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
android:textSize="10sp"
android:textColor="#ffffff" />
</LinearLayout>
这里的样式为:
这里是单个的样式
3.每一个按钮需要有自己的配置xml比如
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon_home_sel" android:state_selected="true" />
<item android:drawable="@drawable/icon_home_nor" />
</selector>
这个是首页的样式,里面配置了,当该按钮被选中时和没有被选中时使用的图片。
放在任意的一个drawable文件夹里面
4.接下来是对Fragment进行配置,他的xml没有什么特别,按照原本的来就好,每一个fragment都配置好之后就可以备用了。
5.这里用来显示TabHost的那个Activity必须继承FragmentActivity。
初始化如下所示:

以下是对上述集合进行解析


浙公网安备 33010602011771号