如何在安卓系统中更改选项卡样式?

8

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


我到目前为止看到的最优雅的解决方案是:https://dev59.com/kXA75IYBdhLWcg3w1sz2#3166865。 - Italo Borssatto
3个回答

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);
   ...
}

不允许使用 spec.setIndicator(txtTabInfo)。编译器会在此行报错。 - Renuka

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);

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接