d-m*_*man 47 tabs android widget
我的类扩展了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)
任何人都可以指导我如何更改所选标签的背景图像或颜色?
Blu*_*ell 93
这将设置标签颜色:
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
}
如果你把它放在onTabChangedListener()中,它将保持所选标签的正确颜色.
pet*_*tos 36
正如mbaird所提到的,更好的解决方案是使用带选择器的背景,因此您不必检查onTabChanged并进行手动更新.最小的代码在这里:
private void initTabsAppearance(TabWidget tabWidget) {
    // Change background
    for(int i=0; i < tabWidget.getChildCount(); i++)
        tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
}
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>
对于完整的Tab自定义,我将添加使用自定义主题更改选项卡文本样式的代码.将此添加到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>
要使用此主题,请在AndroidManifest.xml中定义它:
<application android:theme="@style/MyCustomTheme">
现在你有了自定义背景和自定义文本样式的标签小部件.
Ric*_*red 25
如果您注册TabHost.OnTabChanged事件并调用mTabHost.getCurrentTabView()以获取View,然后view.setBackgroundResource(),该怎么办?
| 归档时间: | 
 | 
| 查看次数: | 83401 次 | 
| 最近记录: |