2013-02-18 28 views
0

我一直在嘗試和無法更改選項卡主機內的我的選項卡的背景顏色,我對Java很新(很好地編碼在一般真的),並與此背道而馳。任何簡潔的幫助真的很感激。需要幫助改變我的標籤主機的背景顏色

活動代碼(以及它的一些)

@SuppressWarnings("unused") 
Resources res = getResources();  // Resource object to get Drawables  
TabHost tabHost = getTabHost();  // The activity TabHost 
TabHost.TabSpec spec;     Intent intent;      

// Create an Intent to launch an Activity for the tab (to be reused)  
intent = new Intent().setClass(this, jobActivity.class);  

// Initialise a TabSpec for each tab and add it to the TabHost  
spec = tabHost.newTabSpec("job").setIndicator(" Job Details ")     

     .setContent(intent);  
tabHost.addTab(spec); 

和相關的XML文件

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@color/background_color"> 
<TabHost 
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

    <LinearLayout   
    android:orientation="vertical"   
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"   
    android:padding="5dp" 
    > 
     <HorizontalScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:fillViewport="true" 
      android:scrollbars="none" 
      > 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 

    </HorizontalScrollView> 
      <FrameLayout    
      android:id="@android:id/tabcontent"   
      android:layout_width="wrap_content"  
      android:layout_height="fill_parent" 
      android:padding="5dp" 
      > 


     </FrameLayout> 
    </LinearLayout> 

</TabHost> 
</LinearLayout> 

它應該是一個人一個不錯的簡單的答案;我希望!

提前感謝

** * ** * **編輯 - 更新,但仍然沒有工作代碼* ** * ** * ** * **** 現在已經得到了下面的工作代碼 - 但似乎setTabColors方法不工作(甚至不知道它被稱爲日誌的說明是從未見過)

public class EPCTabNotesActivity extends TabActivity{ 


public static final String LOG_TAG = "dbtest"; 



public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState);  


setContentView(R.layout.maintab);  


@SuppressWarnings("unused") 
Resources res = getResources();  // Resource object to get Drawables  
TabHost tabHost = getTabHost();  // The activity TabHost 

TabHost.TabSpec spec;    
Intent intent;       



// Create an Intent to launch an Activity for the tab (to be reused)  
intent = new Intent().setClass(this, jobActivity.class);  

// Initialise a TabSpec for each tab and add it to the TabHost  
spec = tabHost.newTabSpec("job").setIndicator(" Job Details ")     

     .setContent(intent); 

tabHost.addTab(spec);  

// Do the same for the other tabs  

// ..... More tabs 

     setTabColors(tabHost); // need to call setTabColors AFTER all the tabs are added to  thetab spec, else the For loop fails as its empty (therefore = 0) 
tabHost.setCurrentTab(0); 



} 

private void setTabColors(TabHost tabHost) { 
// TODO Auto-generated method stub 
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
{ 
     tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.RED); //unselected tab 

     Log.v(LOG_TAG, "tab color "); 
} 
} 
} 

回答

0

你可以試試這個:

tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.RED); 

我:是指其顏色要設置的選項卡。例如:下面的代碼片段會改變第一個標籤的顏色。

tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.RED); 

將此代碼置於活動文件中。

+0

對不起,但我在哪裏可以放那個代碼,什麼是'我'? – CornishDibley 2013-02-18 11:50:03

+0

將此代碼寫入您的活動文件中,'i'表示您要更改其顏色的選項卡。所以如果有3個標籤,你想改變第一個顏色,你可以用0代替我。 – 2013-02-18 12:17:35

+0

感謝關於我的答案 - 有道理。但是,我似乎有一個關於它的靜態(或非靜態)性質的問題 - 不會編譯,而且eclipse記錄指出「不能從類型TabHost中對非靜態方法getTabWidget()進行靜態引用」 - 有任何想法嗎? – CornishDibley 2013-02-18 15:56:10