Tablayout中未突出显示自定义选项卡的文本

时间:2016-11-24 10:11:18

标签: android

我为CustomView TextView TabLayout that contains a ImageView for the tab title and

中的标签创建了for the icons

但是当我点击标签时,所选标签的文字不会突出显示! 我已经尝试了tablayout wigdet的每个XML属性组合,但没有任何结果。我试图谷歌这个问题,但找不到任何解决方案。

MainActivity.class

使用自定义视图实现Tablayoyut。

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

这是Tablayout wigdet上使用的属性

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

1 个答案:

答案 0 :(得分:0)

您可以尝试通过标签布局选择标签。

我正在使用com.android.support:design:23.2.1支持库。使用下面的代码,您可以突出显示所选标签。

tabLayout.getTabAt(0).select();

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

相关问题