2013-12-12 101 views
2

這是真的困惑了我,因爲每當我把我的FragmentTabHost的StripEnabled它不這樣做,我想它是這樣。FragmentTabHost TabWidget標籤欄不能自定義

首先這裏是FragmentTabHost代碼:

mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
     mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); 

     Bundle b = new Bundle(); 

     b.putString("0", "tab1"); 
     mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(null,getResources().getDrawable(R.drawable.selector_tab1)), 
       Fragment1.class, b); 

     b = new Bundle(); 
     b.putString("1", "tab2"); 
     mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(null, getResources().getDrawable(R.drawable.selector_tab2)), 
       Fragment2.class, b); 

和XML文件:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <FrameLayout 
     android:id="@+id/realtabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     /> 

    <android.support.v4.app.FragmentTabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0"/> 
    </android.support.v4.app.FragmentTabHost> 

</LinearLayout> 

現在,當我加入代碼的這些線只是正常工作:

mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE); 

它成功地隱藏了選項卡上的所有分隔符,但是當我使用以下代碼行禁用tabstrip時:

mTabHost.getTabWidget().setStripEnabled(false); 

tabStrip仍然存在,但是當我將它設置爲true時,未選定項目的條變成灰色,我無法真正說出原因。

好了,我在這裏的主要目的是改變標籤欄的顏色或以其他方式完全刪除,但這個問題我真的不能肯定,我應該怎麼做。我試着使用充氣的視圖,但選擇器不再工作,所以我不能告訴瞭如果選項卡被選中或不。希望有人能夠幫助我解決這個問題。但我真的希望有人能幫助我如何改變帶顏色,因爲這變得非常煩人。

回答

3

我已經通過選擇變化的背景資源改變了它。我使用了9個補丁圖像來確保一切都合適。這裏是添加所有選項卡後的代碼:

tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_selector); 

只是它爲所有的孩子,它的工作原理。儘管解決了我的問題,但它並不是最好的解決方案:)

+0

我花了年齡弄清楚這一個。你是一個該死的英雄:') –