美文网首页Android 遇到的问题
TabLayout 和 ViewPager 关联后标题不显示问题

TabLayout 和 ViewPager 关联后标题不显示问题

作者: 穿越平行宇宙 | 来源:发表于2019-03-22 11:46 被阅读0次
    • 运行代码如下
    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
     
        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMinWidth="80dp"
            app:tabMode="scrollable">
     
            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="爱情" />
     
            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="喜剧" />
     
            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="动画" />
     
            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="科幻" />
     
            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="动作" />
     
            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="经典" />
        </android.support.design.widget.TabLayout>
     
        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
    
    • Java文件中的代码
    image.png

    运行结果:

    image

    额造,我的tab栏去哪了?

    因为TabLayout设置关联viewpager后,会清空所有tab栏,因此设置关联后,再用代码显示tab标题就可以:

    image.png

    结果:

    image

    于是乎,tab栏就都显示啦...

    相关文章

      网友评论

        本文标题:TabLayout 和 ViewPager 关联后标题不显示问题

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