Tabs of TabLayout not showing

前端 未结 6 680
借酒劲吻你
借酒劲吻你 2020-12-30 08:19

I have a main activity, which hosts a fragment, which in turn hosts a TabLayout (with a ViewPager). The tab bar is shown, baut the tabs themselves are not shown.

Her

相关标签:
6条回答
  • 2020-12-30 08:52

    like @Nathaniel Ford said,this should a bug, I change to use design library 23.0.1。google fixed it,so change build.gradle to compile 'com.android.support:design:23.0.1'. ps:you also must change your compileSdkVersionto 23

    0 讨论(0)
  • 2020-12-30 08:53

    Set tab icons after setupWithViewPager()

    private void setTabs {
       tabLayout.setupWithViewPager(viewPager);
       setupTabIcons();
    } 
    
    private void setupTabIcons() {
       tabLayout.getTabAt(0).setIcon(tabIcons[0]);
       tabLayout.getTabAt(1).setIcon(tabIcons[1]);
       tabLayout.getTabAt(2).setIcon(tabIcons[2]);
    }
    
    0 讨论(0)
  • 2020-12-30 08:59

    None of the other answers worked for me, I tried all of them

    However, this one did: TabLayout not showing tabs after adding navigation bar

    According to that answer, you have to enclose your TabLayout in a AppBarLayout. Why? Who knows. But at least it works.

    Example Code (taken from that post):

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:id="@+id/appBarLayout2">
    
    
        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabLayout2"
            app:tabMode="fixed"
            app:tabGravity="fill"
    
            ></android.support.design.widget.TabLayout>
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:id="@+id/viewPager2"
        android:layout_below="@+id/appBarLayout2">
    
    </android.support.v4.view.ViewPager>
    
    0 讨论(0)
  • 2020-12-30 09:00

    If you are using android:tabPadding attribute in Tablayout of xml file,remove it.

    0 讨论(0)
  • 2020-12-30 09:04

    This fixed it for me:

    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    

    https://code.google.com/p/android/issues/detail?id=180462

    0 讨论(0)
  • 2020-12-30 09:09

    You can use FragmentStatePagerAdapter instead of FragmentPagerAdapter. It may help you.

    0 讨论(0)
提交回复
热议问题