玩转FragmentTabHost,实现底部导航栏

三步实现

  • 在布局文件下添加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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容