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

1 回答 1

0

你可以试试这个:

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

i :指要设置颜色的选项卡。例如:下面的片段将改变第一个选项卡的颜色。

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

将此代码放在活动文件中。

于 2013-02-18T09:09:22.103 回答