我为TabLayout that contains aTextViewfor the tab title andImageViewfor the icons中的选项卡做了一个CustomView
但是,当我单击一个选项卡时,所选选项卡的文本不会突出显示!我已经尝试了每一种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上使用的属性。
<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" />发布于 2016-11-25 09:01:28
您可以尝试通过选项卡布局选择选项卡。
我使用com.android.support:design:23.2.1支持下面的代码,您可以突出显示选中的选项卡。
tabLayout.getTabAt(0).select();您可以参考以下链接:https://guides.codepath.com/android/google-play-style-tabs-using-tablayout
https://stackoverflow.com/questions/40783497
复制相似问题