2017-12-03 212 views
-1

我有这个代码改变标签布局的文本颜色, ,但它根本不起作用!如何更改标签布局的文本颜色?

app:tabTextColor不起作用,它不能改变颜色为白色。

<android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/white" 
     app:tabIndicatorColor="@color/blue" 
     app:tabIndicatorHeight="5dp" 
     app:tabTextColor="@color/white" /> 
+0

你的背景是白色的,你希望你的tabtextcolor是白色的 – Bek

+0

你想解释什么。你能上传你的截图吗? – Bek

回答

3

您可以自定义您的TabLayout文本。

创建Java代码中的TextView或XML这样

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:textSize="15sp" 
    android:textColor="@color/tabs_default_color" 
    android:gravity="center" 
    android:layout_height="match_parent" 
/> 

请务必保持的ID,因为它是在这里,因为这个ID的TabLayout检查,如果你从代码中使用自定义的TextView

然后膨胀此布局并在该TextView上设置自定义字体并将此自定义视图添加到该选项卡。

for (int i = 0; i < tabLayout.getTabCount(); i++) { 
    //noinspection ConstantConditions 
TextView tv=(TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null) 
tv.setTextColor(customColor) 
tabLayout.getTabAt(i).setCustomView(tv); 

} 
1

尝试使用它 -

<android.support.design.widget.TabLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabMode="fixed" 
     app:tabGravity="fill" 
     android:background="@color/colorWhite" 
     app:tabTextColor="@color/colorBlack" 
     app:tabSelectedTextColor="@color/colorPrimary"/> 
1

使用此代码,这将有助于在所有API级别的API 18〜API 26

tabLayout.setupWithViewPager(viewPager,true); 
     tabLayout.setSelected(true); 

     tabLayout.setTabTextColors(getResources().getColor(R.color.colorHintTextLight), 
        getResources().getColor(R.color.colorPrimaryTextLight)); 

<color name="colorHintTextLight">#80FFFFFF</color> 
    <color name="colorPrimaryTextLight">#FFFFFF</color> 

,这将有助于u.it将帮助时tablayout改变立场。

相关问题