首页 > 解决方案 > tablayout中的标签不显示

问题描述

我有一个非常简单的代码来制作标签,这是我的布局代码:

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/red"
            app:tabGravity="fill"
            app:tabIndicatorColor="#F48917"
            app:tabIndicatorHeight="2.5dp"
            app:tabMode="fixed"
            app:tabTextColor="#fff">

        </android.support.design.widget.TabLayout>

它在一个线性布局内

这是我制作标签的java代码:

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

    TabLayout.Tab firstTab = tabLayout.newTab(); // Create a new Tab names "First Tab"
    firstTab.setText("First Tab"); // set the Text for the first Tab
    firstTab.setIcon(R.drawable.ic_launcher); // set an icon for the first tab
    tabLayout.addTab(firstTab);

问题是,它没有在android api +21上显示一个没有任何内容的空标签,但它在android 21以下显示标签

我怎样才能解决这个问题 ?这是什么问题?

这是 api 21+ 上的图像: 在此处输入图像描述 这是 21 以下 api 上的图像

在此处输入图像描述

标签: androidandroid-tabhostandroid-tablayout

解决方案


XML:

<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tabLayout"/>

代码是

TabLayout tabLayout= (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("first").setIcon(R.drawable.ic_launcher));

不要忘记添加依赖项

compile 'com.android.support:design:XXX'

其中 XXX 是支持设计的最低版本


推荐阅读