2011-04-07 16 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")); 

और यहाँ ऑनबैब चेंज इवेंट:

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")); 
} 

ऑनबैब चेंजेड ईवेंट में, मैं सभी टैब के टेक्स्ट रंग को भी बदलना चाहता हूं। कृपया मेरी मदद करें कि मैं ईवेंट में टैब का टेक्स्ट रंग कैसे बदल सकता हूं?

धन्यवाद,

उत्तर

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 क्या है? –

+0

@ फ़ारहान, मेरे कोड में आईडी "शीर्षक" के साथ कुछ भी नहीं है। कृपया मेरे कोड की समीक्षा करें और मुझे बताएं कि मुझे एंड्रॉइड.R.id.title को क्या बदलना चाहिए? –

+0

जैसा कि मैंने कहा था कि टैब का उपयोग करते समय, यह स्वचालित रूप से ढांचे द्वारा जोड़ा जाता है ....इसकी तरह जब आप setIndicator (पहला तर्क) में टेक्स्ट सेट करते हैं, तो यह टेक्स्ट id android.R.id.title के साथ टेक्स्टव्यू पर सेट होता है .... बस इसे प्राप्त करें और रंग बदलें ... – Farhan

4

जब आप findViewById (आईडी) का उपयोग करते हैं तो आप सिस्टम को "आईडी" के साथ किसी भी दृश्य को देखने के लिए वर्तमान दृश्य समूह में अपेक्षाकृत देखें। इसका मतलब है कि आप अपने कोड में क्या करते हैं यह .findViewById (आईडी) है, इसलिए यह वर्तमान दृश्य में "आईडी" की तलाश करेगा। और findViewById (android.R.id.tabHost) कर बहुत होशियार है क्योंकि यह सिर्फ मौजूद नहीं है नहीं है ...

आप getTabHost (करते हैं) फिर भी, आप प्रणाली अद्वितीय की 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

क्या आप नमूना स्निपेट पोस्ट कर सकते हैं यह दिखा रहा है कि यह कहां जोड़ना है? मुझे एंड्रॉइड स्टूडियो – CyprUS

+1

सही उत्तर में ऐसा विकल्प नहीं मिल रहा है। हालांकि यह एक अलग मुद्दा है, मैं बस उस बात को इंगित करना चाहता हूं कि टेक्स्ट AllCaps = "false" हम उदाहरण में देखते हैं कि वर्तमान में सीएपी को हटाने के व्यवहार को प्राप्त करने के लिए कोई प्रभाव नहीं पड़ता है, आपको एक नई शैली बनाने और इसे टैबलेट पर असाइन करने की आवश्यकता होगी , नीचे दिए गए प्रश्न को देखें http://stackoverflow.com/questions/31295116/androidtextallcaps-false-not-working-for-tablayout-design-support –

+0

इसे @ ए अलाकाडोमी को इंगित करने के लिए धन्यवाद। मैंने अपने काम करने वाले नमूने से कोड स्निपेट चिपकाया, ताकि कोड वहां हो, जिसका जवाब के साथ कुछ लेना देना नहीं है। – AnswerDroid

संबंधित मुद्दे