首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义选项卡的文本在选项卡布局中没有突出显示

自定义选项卡的文本在选项卡布局中没有突出显示
EN

Stack Overflow用户
提问于 2016-11-24 10:11:17
回答 1查看 450关注 0票数 0

我为TabLayout that contains aTextViewfor the tab title andImageViewfor the icons中的选项卡做了一个CustomView

但是,当我单击一个选项卡时,所选选项卡的文本不会突出显示!我已经尝试了每一种XML属性的组合,但是没有什么效果。我试着用谷歌搜索这个问题,但没有找到任何解决办法。

MainActivity.class

具有自定义视图的Tablayoyut的实现。

代码语言:javascript
复制
    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:

自定义选项卡的布局。

代码语言:javascript
复制
<?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上使用的属性。

代码语言:javascript
复制
<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" />
EN

回答 1

Stack Overflow用户

发布于 2016-11-25 09:01:28

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

我使用com.android.support:design:23.2.1支持下面的代码,您可以突出显示选中的选项卡。

代码语言:javascript
复制
tabLayout.getTabAt(0).select();

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40783497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档