我正在使用TabHost合成器进行应用,标签显示默认颜色,是否有可能将默认颜色更改为我们自己的颜色。我从谷歌那里得到了一些想法,
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(R.color.transparent); //unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#000011")); // selected
}
每当我这样做时,我都会收到强制关闭错误。如果有人想改变背景颜色,请引导我。
答案 0 :(得分:7)
首先需要更改默认外观
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
if (i == 0) tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF"));
else tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
}
在处理onTabChanged
事件之前
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parsecolor("#7392B5")); //unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#FFFFFF")); // selected
}
});
答案 1 :(得分:1)
使用视图创建标签:
final TabHost tabs = getTabHost();
tabs.setup();
final TabHost.TabSpec spec = tabs.newTabSpec("tag");
spec.setIndicator(View.inflate(this, R.layout.tab_view, null));
tabs.addTab(spec);
定义你的选择器
<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="true" >
<shape>
<gradient
android:endColor="#bbb"
android:centerColor="#999"
android:startColor="#ddd"
android:angle="270" />
<stroke
android:width="3dp" />
<corners
android:topLeftRadius="10dp"
android:bottomRightRadius="0.1dp"
android:bottomLeftRadius="0.1dp"
android:topRightRadius="10dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_selected="true" >
<shape>
<gradient
android:endColor="#888"
android:centerColor="#777"
android:startColor="#999"
android:angle="270" />
<stroke
android:width="3dp" />
<corners
android:topLeftRadius="10dp"
android:bottomRightRadius="0.1dp"
android:bottomLeftRadius="0.1dp"
android:topRightRadius="10dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#eee"
android:centerColor="#aaa"
android:endColor="#ccc"
android:angle="270" />
<stroke
android:width="3dp" />
<corners
android:topLeftRadius="10dp"
android:bottomRightRadius="0.1dp"
android:bottomLeftRadius="0.1dp"
android:topRightRadius="10dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
将您的选择器添加为选项卡可用于选项卡视图
<View
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/tab_width"
android:layout_height="fill_parent"
android:background="@xml/tabs_background"/>
"@dimen/tab_width"
是您的标签宽度,以dp,像素或smthn为单位。