2011-12-24 142 views
1

我已使用選項卡。我想改變標籤的背景顏色或主題。當我點擊該標籤出現在默認顏色爲灰色,但我想顯示該標籤在自定義顏色。我如何更改android中選項卡的背景顏色?

這裏我的代碼:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 

    > 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
       > 

     > 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="-30dp" 
      android:background="#FF0000" 

      /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 

     </FrameLayout> 
    </LinearLayout> 

</TabHost> 

類文件是

public class AndroidtabActivity extends TabActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
//  View title = getWindow().findViewById(android.R.id.title); 
//  View titleBar = (View) title.getParent(); 
//  titleBar.setBackgroundResource(R.drawable.top_bar2); 


     Resources res = getResources(); 
     TabHost tabHost = getTabHost(); 
     //tabHost = (TabHost) findViewById(R.id.tabhost); 
     tabHost.setup(); 
     //tabHost.setBackgroundResource(R.drawable.pink); 


     TabHost.TabSpec spec; 
     //tabHost.getTabWidget().setDividerDrawable(R.drawable.top_bar); 
     Intent in; 
     in = new Intent().setClass(this, MainActivity.class); 
     spec=tabHost.newTabSpec("calc").setIndicator("calculate").setContent(in); 
     tabHost.addTab(spec); 
     in = new Intent().setClass(this, TutorialZoomActivity1.class); 
     spec=tabHost.newTabSpec("help").setIndicator("help").setContent(in); 
     tabHost.addTab(spec); 
     tabHost.setCurrentTab(2); 
     // spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.flash)); 




    } 
} 

請告訴我如何更改標籤的背景色。

+0

[這裏是我的回答對自定義選項卡(HTTP更改標籤的背景顏色://stackoverflow.com/a/6992662/593709) – 2011-12-24 12:04:49

回答

0

也許你應該創建自定義選項卡。

  1. 讓說..創建另一個custom_tab.xml(LinearLayout中和的TextView)
  2. 在的LinearLayout
  3. ,設置機器人:背景在繪製另一個XML文件(@繪製/ tab_selector)
  4. 在上面提到的drawable中創建新的xml文件。恩。 tab_selector.xml。在這個文件中,您可以設置狀態(聚焦,按下,選擇等)

    好運
0

您可以通過

for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) { 
     tabHost.getTabWidget().getChildAt(i). 
            setBackgroundResource(R.drawable.hans_layout_nblue); 
    } 
相關問題