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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3029074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:22:25  来源:igfitidea点击:

How to change tab style in Android?

androidcoding-styletabsthemes

提问by Stefan

I'd like to my Android tabs to look flat and simple like the ones in the official TWitter app. How can I override the default (light) theme and change the background images for the tabs using style/theme definitions?

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

回答by Mathias Conradt

You could adjust the tabs via code - here's an excerpt from my application, but you could also assign themes instead of the background image directly. (I haven't used a way via xml attributes yet, not sure if that's available as well somehow).

您可以通过代码调整选项卡 - 这是我的应用程序的摘录,但您也可以直接分配主题而不是背景图像。(我还没有使用通过 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);
   ...
}

回答by John Fowler

I used the following definition of my drawable, tab_background.xml

我使用了我的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>

Then iterate over the tabs to set them.

然后遍历选项卡以设置它们。

TabWidget tabWidget = tabHost.getTabWidget();

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

回答by Scott Ferguson

You could also do it via XML and create/define a global theme for all tabs in your app. There's more info on creating widget themes here: http://developer.android.com/guide/topics/ui/themes.html

您也可以通过 XML 来完成,并为应用程序中的所有选项卡创建/定义一个全局主题。这里有关于创建小部件主题的更多信息:http: //developer.android.com/guide/topics/ui/themes.html