如何更改Android选项卡窗口小部件的背景?
问题描述:
我的类扩展扩展TabActivity如何更改Android选项卡窗口小部件的背景?
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)
可有人指导我如何更改背景图片或所选标签的颜色?
答
如果您注册TabHost.OnTabChanged事件并调用mTabHost.getCurrentTabView()以获取View,然后view.setBackgroundResource(),该怎么办?
答
这将设置你的标签颜色:
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()内,它会保持正确的颜色选定的标签。
答
正如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绘制与选择:
<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>
对于整个标签定制,我会添加的代码使用自定义主题改变标签的文本样式。这种加入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">
现在你有自定义背景和自定义文本样式标签控件。
答
我在XML的TabWidget元素中设置了'android:background'参数,以提供所有选项卡的通用背景。
然后我在'.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);
我觉得这是一个更好的方法。
答
> 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);
使用.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>
非常感谢,这真的帮助了我。有没有什么办法在XML中实现这种方法? – teoREtik 2011-04-04 07:58:20
@teoREtik XML是静态内容,仅适用于您的活动首次启动时(布局初始化),因此不会。 – Blundell 2011-10-23 15:55:36
感谢您的帮助..这个答案是非常有用的.. +1为.. ..干杯.. !! – Aditya1510 2012-11-17 05:49:45