2010-06-12 42 views
8

我想让我的Android标签看起来像官方TWitter应用程序中的标签一样平坦和简单。如何使用样式/主题定义覆盖默认(浅色)主题并更改标签的背景图像?如何更改Android中的标签样式?

+0

最优雅的解决方案,我看到到现在为止是:http://stackoverflow.com/questions/3078081/setting-a-global-style-for-views-in-android/3166865#3166865 – 2011-09-02 17:43:52

回答

16

您可以通过代码调整标签 - 这里是我的应用程序摘录,但您也可以直接指定主题而不是背景图片。 (我还没有通过xml属性使用过一种方式,但不确定是否可用)。

private void initTabs() { 
    tabs = (TabHost) findViewById(R.id.tabhost); 
    tabs.setup(); 
    tabs.setBackgroundResource(R.drawable.bg_midgray); 

    TabHost.TabSpec spec; 

    // Location info 
    txtTabInfo = new TextView(this); 
    txtTabInfo.setText("INFO"); 
    txtTabInfo.setPadding(0, 5, 0, 0); 
    txtTabInfo.setTextSize(11); 

    txtTabInfo.setBackgroundResource(R.drawable.bg_tab_left_active_right_inactive); 
    txtTabInfo.setTextColor(Color.DKGRAY); 
    txtTabInfo.setGravity(Gravity.CENTER_HORIZONTAL); 
    txtTabInfo.setHeight(39); 
    spec = tabs.newTabSpec("tabInfo"); 
    spec.setContent(R.id.tabInfo); 
    spec.setIndicator(txtTabInfo); 
    tabs.addTab(spec); 
    ... 
} 
+0

规范。 setIndicator(txtTabInfo);这是不允许的。编译器在此行发生错误。 – sachin 2010-09-03 08:44:02

3

我用我的绘制如下定义,tab_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
     <item android:state_selected="true" android:drawable="@drawable/tab_bg_selected" /> 
     <item android:drawable="@drawable/tab_bg_normal" /> 
    </selector> 

然后在标签迭代进行设置。

TabWidget tabWidget = tabHost.getTabWidget(); 

for(int i=0; i<tabWidget.getChildCount(); i++) 
    tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_background);