2017-12-03 174 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改變立場。

相關問題