添加导航栏后,TabLayout不显示选项卡 [英] TabLayout not showing tabs after adding navigation bar

查看:223
本文介绍了添加导航栏后,TabLayout不显示选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ViewPager + TabLayout.它工作正常,但是添加导航栏后,TabLayout不显示选项卡.我寻找解决方案,但没有一个能帮上忙.如果有人告诉我为什么会这样,那将真的很有帮助.

i am working with ViewPager + TabLayout. It was working fine, but after i added navigation bar, the TabLayout is not showing tabs. I searched for solution but none of them helped. It will be really helpful if anyone tells me why this happened.

MainActivity.java

MainActivity.java

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

    SimpleFragmentPageAdapter adapter = new SimpleFragmentPageAdapter(this, getSupportFragmentManager());

    viewPager.setAdapter(adapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    tabLayout.setupWithViewPager(viewPager);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

content_main.xml

content_main.xml

    <android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    style="@style/CategoryTab"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabTextColor="#ddd"
    app:tabSelectedTextColor="#fff" />

    <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

推荐答案

请参考此代码.将标签页布局添加到应用栏布局内.尝试此操作

Please refer this code. Add the tab layout inside a appbar layout.Try this

<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>

这篇关于添加导航栏后,TabLayout不显示选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆