2012-06-16 280 views
1

我的这两个选项卡都有这些代码。我想改变颜色,但我不知道如何去做。应该在我的java文件中完成,还是在我的xml中完成?谢谢更改Android中的选项卡颜色

这里是我的代码

import android.app.Activity; 
import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Window; 
import android.widget.TabHost; 

// This is now the first activity that you see when you enter the app, it derives from  TabActivity 
public class TabsActivity extends TabActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState); 



    // The activity displays a TabHost layout with a TabWidget below the actual tab contents 
    setContentView(R.layout.tabs); 

    // Two tabs are added one displays the main list activity, the other an  info activity 
    addNewTab("Kalender", MainActivity.class); 
    addNewTab("Info", InfoActivity.class); 
} 


// This function defines and adds a tab to the interface 
private void addNewTab(String name, Class<? extends Activity> activityClass) 
{ 
    TabHost tabHost = getTabHost(); 

    // The new tab will display a separate activity, so it needs an intent for it 
    Intent activityIntent = new Intent().setClass(this, activityClass); 

    // The TabSpec sets the internal name and the visible name of the newly created tab 
    TabHost.TabSpec spec =  tabHost.newTabSpec(name).setIndicator(name).setContent(activityIntent); 

    // Finally, the new tab is added to the TabHost 
    tabHost.addTab(spec); 
} 
} 

回答

2

更改文本颜色和TAB的背景颜色

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
    { 

     tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE); //Changing background color of tab 

     TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); /*for Selected Tab changing text color*/ 
     tv.setTextColor(Color.BLACK); 
    } 
+1

非常感谢 – Chrfugl

0

如果您想自定义标签的外观,你应该使用自己的标签控件。事情是,大多数的Android小部件是使用位图主题,所以你不能简单地改变渐变颜色。

有人建议简单地更改标准小部件的backgroundColor,但它看起来相当平坦。

使用自己的小部件去something like this

// Initialize a TabSpec for each tab and add it to the TabHost 
spec = tabHost.newTabSpec("artists").setIndicator("Artists", 
        res.getDrawable(R.drawable.ic_tab_artists)) 
       .setContent(intent); 
tabHost.addTab(spec); 

也看看the Android style guide's tab section

0

这是一种让背景单色标签的一种颜色,也可以设置一种。

tabHost.getTabWidget().getChildAt(tabIndex).setBackgroundColor(color); 
+0

我应该在哪里插入这条线? – Chrfugl

+0

@Chrfugl - 您的选择。你的'addNewTab'结尾是很自然的。 –

+0

好的,谢谢。最后一个问题。设置颜色只会是(#000000)等等。 – Chrfugl

相关问题