底部导航栏-----FragmentTabHost

时间:2023-03-08 22:20:02
底部导航栏-----FragmentTabHost

【说明】

  1、主界面上添加父容器:FragmentTabHost

    属于v4兼容包
    需要指定该id为android:id/tabhost,不能修改,表示由android系统来托管这个id。
    本身是一个FrameLayout的子类

  2、显示内容区域

    作为每选一个导航按钮,将显示导航对应的内容。这个内容区域需要使用一个容器来表示。
    必须设定这个容器的id为android:id/tabcontent

  3、导航区域  

    需要指定导航元素——TabWidget

【注意】

  1、指定id时为android:id/tabhost,绑定时使用android.R.id.tabhost.

  2、每一个Tab对应的Fragment的会填充到tabcontext上

【效果】

  底部导航栏-----FragmentTabHost

【项目结构】

  底部导航栏-----FragmentTabHost

【步骤】

  ①修改activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@android:id/tabhost"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"> </FrameLayout> <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"> </TabWidget>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>

  ②创建main_tab_item.xml底部导航的单个布局

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/blackBg"> <ImageView
android:id="@+id/ivTab"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:padding="5sp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/tvTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/writeTx"
android:text="聊天"
android:textSize="12sp"/> </LinearLayout>