如何在Android中更改标签样式?

时间:2010-06-12 15:03:00

标签: android coding-style tabs themes

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

3 个答案:

答案 0 :(得分: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);
   ...
}

答案 1 :(得分:3)

我使用了以下对drawable 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);

答案 2 :(得分:0)

您也可以通过XML完成此操作,并为应用中的所有标签创建/定义全局主题。这里有关于创建窗口小部件主题的更多信息:http://developer.android.com/guide/topics/ui/themes.html