2011-04-07 80 views
15

我有一個TabHost這樣的:安卓:更改標籤文字顏色編程

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@android:id/tabhost" 
android:background="@drawable/tabs_bg"> 

<LinearLayout 
    android:id="@+id/LinearLayout01" 
    android:orientation="vertical" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"> 
    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_marginBottom="5dip"> 
    </TabWidget> 
    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent"> 
    </FrameLayout> 
</LinearLayout> 

而且我添加標籤來此TabHost程序是這樣的:

tabHost = (TabHost)findViewById(android.R.id.tabhost); 
    tabHost.setOnTabChangedListener(this); 

    /* tid1 is firstTabSpec Id. Its used to access outside. */ 
    TabSpec firstTabSpec = tabHost.newTabSpec("tid1"); 
    TabSpec secondTabSpec = tabHost.newTabSpec("tid2"); 
    TabSpec ThirdTabSpec = tabHost.newTabSpec("tid3"); 

    /* TabSpec setIndicator() is used to set name for the tab. */ 
    /* TabSpec setContent() is used to set content for a particular tab. */ 
    firstTabSpec.setIndicator("Tab1", getResources().getDrawable(R.drawable.tab1)); 
    secondTabSpec.setIndicator("Tab2", getResources().getDrawable(R.drawable.tab2)); 
    ThirdTabSpec.setIndicator("Tab3", getResources().getDrawable(R.drawable.tab3)); 

    firstTabSpec.setContent(new Intent(this,FirstTab.class)); 
    secondTabSpec.setContent(new Intent(this,SecondTab.class)); 
    ThirdTabSpec.setContent(new Intent(this,ThirdTab.class)); 

    /* Add tabSpec to the TabHost to display. */ 
    tabHost.addTab(firstTabSpec); 
    tabHost.addTab(secondTabSpec); 
    tabHost.addTab(ThirdTabSpec); 

    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
    { 
     tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#121312")); 
    } 

    tabHost.getTabWidget().setCurrentTab(0); 
    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#f1a026")); 

這裏是onTabChanged事件:

public void onTabChanged(String tabId) { 
    // TODO Auto-generated method stub 
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
    { 
     tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#121312")); 
    } 

    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#f1a026")); 
} 

在onTabChanged事件中,我也想更改所有選項卡的文本顏色。請幫助我如何更改活動中選項卡的文本顏色?

感謝,

回答

52

要更改標籤的文本顏色,你需要得到被設置爲標籤的標題的觀點,即TextView的,你可以這樣改:

TabHost tabhost = getTabHost(); 
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
    { 
     TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); 
     tv.setTextColor(.....); 
    } 

希望這有幫助....

+0

喜爾漢!在這種情況下android.R.id.title是什麼? – 2011-04-07 09:14:43

+0

@ Farhan,在我的代碼中沒有id「title」。請檢查我的代碼,讓我知道我應該取代android.R.id.title? – 2011-04-07 10:07:04

+0

正如我所說的,當使用標籤時,它會自動添加框架....它就像當你在setIndicator(第一個參數)中設置文本時,這個文本被設置在一個ID爲android.R.id.title的textview上....簡單地得到它並改變顏色... – Farhan 2011-04-07 10:39:52

4

當您使用findViewById(id)時,您要求系統查找與當前ViewGroup ID爲「id」相對任何視圖。這意味着你在你的代碼中做的是這個 .findViewById(id),所以它會在當前視圖中查找「id」。和做findViewById(android.R.id.tabHost)是不是很聰明,因爲它根本就不存在......

當你getTabHost(),但是,你問的系統獲得的獨特 tabHost您活動,不管它有任何視圖作爲根,即tabHost可以附加到沒有任何好處。

作爲一個結論,你應該總是我們的TabHostActivity getTabHost

希望我是清楚的

11

對我來說@Farhan的解決方案沒有奏效,因爲getChildCount()保持,同時具有四個選項卡返回1。使用getTabCount()getChildTabViewAt()解決了這個問題對我來說:

for (int tabIndex = 0 ; tabIndex < mTabHost.getTabWidget().getTabCount() ; tabIndex ++) { 
    View tab = mTabHost.getTabWidget().getChildTabViewAt(tabIndex); 
    TextView t = (TextView)tab.findViewById(android.R.id.title); 
    t.setTextColor(getResources().getColor(R.color.white)); 
} 

我還以爲我張貼這種替代具有相同問題的人。

25

對於新的設計支持選項卡布局;你可以在你的xml中定義它
app:tabTextColor="@color/tab_inactive" app:tabSelectedTextColor="@color/tab_active"
E.g. -

<android.support.design.widget.TabLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/tabanim_tabs" 
      app:tabTextColor="@color/tab_inactive" 
      app:tabSelectedTextColor="@color/tab_active" 
      android:textAllCaps="false" 
      /> 


通過編程可以來達到的是這樣的:

tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.color.tab_selector)); 
     tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.indicator)); 
+1

你能發佈一個示例代碼片段準確顯示在哪裏添加這個?我無法在Android Studio – CyprUS 2016-05-18 01:09:35

+1

中找到這樣的選項的完美答案。儘管這是一個不同的問題,我只想指出,我們在示例中看到的textAllCaps =「false」目前沒有效果,但要實現移除CAP的行爲,您需要創建一個新樣式並將其分配給Tablayout ,查看下面的問題 http://stackoverflow.com/questions/31295116/androidtextallcaps-false-not-working-for-tablayout-design-support – 2017-05-10 13:45:08

+0

謝謝指出它@ A.Alqadomi。我粘貼了我的工作示例中的代碼片段,以便代碼在那裏,與答案無關。 – AnswerDroid 2017-11-07 10:11:52