美文网首页
玩转FragmentTabHost,实现底部导航栏

玩转FragmentTabHost,实现底部导航栏

作者: timber_wu | 来源:发表于2017-07-23 15:33 被阅读42次

三步实现

  • 在布局文件下添加FragmentTabHost导航栏(三个注意事项
    1.在其上添加一个FrameLayout--底部变换真正内容在这里展示
    2.FragmentTabHost的id为android:id="@android:id/tabhost"
    3.子布局FrameLayout的id为android:id="@android:id/tabcontent")
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    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"
    android:orientation="vertical"
    tools:context="com.wws.admin.cnscwwstest.MainActivity">

        <FrameLayout
            android:id="@+id/real_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            />
    
      <android.support.v4.app.FragmentTabHost
          android:id="@android:id/tabhost"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/white">
    
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>
     </android.support.v4.app.FragmentTabHost>
    
    </LinearLayout>
    
  • Activity继承FragmentActivity或其子布局

  • FragmentTabHost实例实现setup(Context context, FragmentManager manager, int containerId)方法

使用方法
1.实现setup方法后
2.实例化TabHost.TabSpec类,实现

       public TabSpec setIndicator(View view)

View即为底部导航栏的布局
3.将TabSpec实例和对应的内容添加到TabHost

public void addTab(@NonNull TabHost.TabSpec tabSpec, @NonNull Class<?> clss,
@Nullable Bundle args)

相关文章

网友评论

      本文标题:玩转FragmentTabHost,实现底部导航栏

      本文链接:https://www.haomeiwen.com/subject/qqrckxtx.html