如何更改 Android 选项卡小部件的背景?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2099959/
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 04:33:09  来源:igfitidea点击:

How do I change the background of an Android tab widget?

androidwidgettabs

提问by d-man

My class extends extends TabActivity

我的课程扩展了扩展 TabActivity

TabHost mTabHost =  getTabHost();

TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1");
TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2");

tab1 .setIndicator("title tab1");
tab2 .setIndicator("title tab2");
mTabHost.addTab(tab1);mTabHost.addTab(tab2);

TabHost.setCurrentTab(0 or 1)

Can anybody guide me how do I change the background image or color of selected tab?

任何人都可以指导我如何更改所选标签的背景图像或颜色?

采纳答案by RickNotFred

What if you register for TabHost.OnTabChanged events and call mTabHost.getCurrentTabView() to get the View, then view.setBackgroundResource()?

如果您注册 TabHost.OnTabChanged 事件并调用 mTab​​Host.getCurrentTabView() 获取视图,然后调用 view.setBackgroundResource() 会怎样?

回答by Blundell

This will set your tab colors:

这将设置您的标签颜色:

public static void setTabColor(TabHost tabhost) {
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
    }
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}

and if you put it within the onTabChangedListener(), it will keep the correct color for selected tabs.

如果您将它放在 onTabChangedListener() 中,它将为所选选项卡保留正确的颜色。

回答by peter.bartos

As mbaird mentioned, the better solution is to use backgroundwith selector, so you don't have to check onTabChangedand do manual update. The minimal code is here:

正如 maird 提到的,更好的解决方案是将背景与选择器一起使用,这样您就不必检查onTabChanged和手动更新。最小的代码在这里:

private void initTabsAppearance(TabWidget tabWidget) {
    // Change background
    for(int i=0; i < tabWidget.getChildCount(); i++)
        tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
}

Where tab_bgis an xml drawable with selector:

tab_bg带有选择器的 xml drawable在哪里:

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

For the full Tab customization I will add the code for changing tab text styleusing custom theme. Add this to styles.xml:

对于完整的选项卡自定义,我将添加使用自定义主题更改选项卡文本样式的代码。将此添加到styles.xml

<resources>

    <style name="MyCustomTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
    </style>

    <style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
        <item name="android:textAppearance">@style/CustomTabWidgetText</item>
    </style>

    <style name="CustomTabWidgetText" parent="@android:style/TextAppearance.Widget.TabWidget">
        <item name="android:textSize">12sp</item>
        <item name="android:textStyle">bold</item>
    </style>

</resources>

To use this theme, define it in AndroidManifest.xml:

要使用这个主题,请在 AndroidManifest.xml 中定义它:

<application android:theme="@style/MyCustomTheme">

And now you have tab widgets with custom backgroundand custom text style.

现在您拥有带有自定义背景自定义文本样式的选项卡小部件。

回答by Mark B

Does thissolve your problem? Basically calling setBackgroundDrawable on each tab view with a selector?

请问这个解决问题了吗?基本上使用选择器在每个选项卡视图上调用 setBackgroundDrawable ?

回答by Jamal

>     TabHost mTabHost =  getTabHost();
>     
>     TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1");
>     TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2");
>     
>     tab1.setIndicator("title tab1");
>     tab2.setIndicator("title tab2");
>     mTabHost.addTab(tab1) ;mTabHost.addTab(tab2);
>     
>     TabHost.setCurrentTab(0 or 1);


mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1selector); 

mTabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2selector);    

mTabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3selector);    

mTabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.tab4selector);

Use .setBackgroundResourceand tabNselector is an XML - tabNselector.xml

使用.setBackgroundResource和 tabNselector 是一个 XML - tabNselector.xml

    <?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_selected="false" android:drawable="@drawable/tabN"/>
   <item android:state_selected="true" android:drawable="@drawable/tabNsel"  />
</selector>

回答by Saad Farooq

I set the 'android:background' parameter in the TabWidget element of the XML to give the generic background of all the tabs.

我在 XML 的 TabWidget 元素中设置了“android:background”参数,以提供所有选项卡的通用背景。

Then I passed views inflated from another XML in the '.setIndicator' method.

然后我在“.setIndicator”方法中传递了从另一个 XML 膨胀的视图。

 View v = LayoutInflater.from(this).inflate(R.layout.tab_widget, null);
    TextView label = (TextView) v.findViewById(R.id.tabLabel);
    label.setText("Whatever");
 tab1 .setContent(v);

I feel that's a nicer way of doing this.

我觉得这是一个更好的方法。