简体   繁体   English

如何更改Android标签小部件的背景?

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

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? 任何人都可以指导我如何更改所选标签的背景图像或颜色?

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()中,它将保持所选标签的正确颜色。

As mbaird mentioned, the better solution is to use background with selector, so you don't have to check onTabChanged and do manual update. 正如mbaird所提到的,更好的解决方案是使用带选择器的背景 ,因此您不必检查onTabChanged并进行手动更新。 The minimal code is here: 最小的代码在这里:

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_bg is 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 style using custom theme. 对于完整的Tab自定义,我将添加使用自定义主题更改选项卡文本样式的代码。 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 background and custom text style . 现在你有了自定义背景自定义文本样式的标签小部件。

如果您注册TabHost.OnTabChanged事件并调用mTabHost.getCurrentTabView()以获取View,然后view.setBackgroundResource(),该怎么办?

>     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 .setBackgroundResource and 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>

Does this solve your problem? 这会解决您的问题吗? Basically calling setBackgroundDrawable on each tab view with a selector? 基本上使用选择器在每个选项卡视图上调用setBackgroundDrawable?

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. 我觉得这是一个更好的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM