自定义标签的文本没有在Tablayout

问题描述:

强调我在TabLayout that contains a的TextView for the tab title and的ImageView做出的选项卡的CustomViewfor the icons自定义标签的文本没有在Tablayout

但是当我点击选项卡上,选定选项卡中的文本不突出! 我已经尝试了Tablayout wigdet的XML属性的每个组合,并没有任何工作。我试图谷歌的问题,但找不到任何解决方案。

MainActivity.class

的Tablayoyut与自定义视图的implemeneation。

mainTablayout = (TabLayout) findViewById(R.id.tablayout_activity_main); 
    mainViewpager = (ViewPager) findViewById(R.id.main_viewpager); 

    mainTablayout.addTab(mainTablayout.newTab().setText("Search")); 
    mainTablayout.addTab(mainTablayout.newTab().setText("Inbox")); 
    mainTablayout.addTab(mainTablayout.newTab().setText("Edit")); 
    mainTablayout.addTab(mainTablayout.newTab().setText("Profile")); 

    mainTablayout.getTabAt(0).setCustomView(R.layout.custom_tablayout).setIcon(R.drawable.ic_search); 
    mainTablayout.getTabAt(1).setCustomView(R.layout.custom_tablayout).setIcon(R.drawable.ic_tab_envelope); 
    mainTablayout.getTabAt(2).setCustomView(R.layout.custom_tablayout).setIcon(R.drawable.ic_tab_cogwheel); 
    mainTablayout.getTabAt(3).setCustomView(R.layout.custom_tablayout).setIcon(R.drawable.ic_tab_profile_1); 


    mainViewpager.setAdapter(new MainPagerAdapter(getSupportFragmentManager())); 
    mainViewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mainTablayout)); 
    mainTablayout.addOnTabSelectedListener(this); 

custom_tablayout.xml:

为定制选项卡

布局
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 


<ImageView 
    android:id="@android:id/icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" /> 

<TextView 

    android:textSize="12sp" 
    android:textStyle="bold" 
    android:id="@android:id/text1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="-6dp" /> 

</LinearLayout> 

activity_main.xml中:

这是的Wigdet

上Tablayout使用的属性
<android.support.design.widget.TabLayout 
    android:id="@+id/tablayout_activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="?actionBarSize" 
    android:layout_alignParentBottom="true" 
    android:layout_below="@id/tablayout_activity_main" 
    android:background="#f5f5f5" 
    app:tabGravity="fill" 
    app:tabIndicatorHeight="0dp" 
    app:tabMode="fixed" 
    app:tabTextColor="@color/tab_off" 
    app:tabSelectedTextColor="@color/colorPrimary" /> 

您可以尝试通过选项卡布局选择选项卡。

我正在使用com.android.support:design:23.2.1支持库。使用下面的代码可以突出显示选定的选项卡。

tabLayout.getTabAt(0).select(); 

您可以参考以下链接:https://guides.codepath.com/android/google-play-style-tabs-using-tablayout