我已经有了一个选项卡小部件,当它被选中时,它可以更改它的子选项卡颜色。但这是有缺陷的,我的android选项卡小部件中的边界线并不总是工作得很好。有时,当我选择另一个选项卡以使其清晰时,边界线会变成白色,这是我的活动代码。如果我不能提供图像,我很抱歉,因为我在堆栈溢出方面没有赢得很多声誉
@SuppressWarnings("deprecation")
public class TabHostActivity extends TabActivity {
private TabHost tabHost;
private int currentTab = 0;
private int lastTab = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_tabhost);
UserAccount userAccount = new UserAccount();
tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("First Tab");
tab1.setContent(new Intent(this, HomeActivity.class));
tab1.setIndicator("",getResources().getDrawable(R.drawable.home_icon));
tabHost.addTab(tab1);
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
tab2.setContent(new Intent(this, About.class));
tab2.setIndicator("",getResources().getDrawable(R.drawable.about_icon));
tabHost.addTab(tab2);
TabSpec tab3 = tabHost.newTabSpec("Third Tab");
tab3.setContent(new Intent(this, GridViewActivity.class));
tab3.setIndicator("",getResources().getDrawable(R.drawable.gallery_icon));
tabHost.addTab(tab3);
getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId)
{
currentTab = getTabHost().getCurrentTab();
setCurrentTabColor();
lastTab =currentTab;
}
});
getTabWidget().getChildAt(lastTab).setBackgroundColor(Color.GRAY);
}
public void setCurrentTabColor(){
getTabWidget().getChildAt(currentTab).setBackgroundColor(Color.GRAY);
getTabWidget().getChildAt(lastTab).setBackgroundColor(Color.parseColor("#FCAFA6"));
}
}
这是我的xml for tabhost,它将我的选项卡小部件的背景色设置为粉红色
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#FCAFA6" >
</TabWidget>
</LinearLayout>
</TabHost>
首先,在我的活动中,我实例化了我的选项卡主机
tabHost = (FragmentTabHost) findViewById(R.id.tabContainer);
tabHost.setup(this, getSupportFragmentManager(), R.id.tabContent);
tabHost.addTab(tabHost.newTabSpec("1").setIndicator("New Tab"),
PageFragment.class, null);
我写了一个方法,根据是否选择来更新标签的颜色
protected void updateTabs() {
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
if (tabHost.getTabWidget().getChildAt(i).isSelected()) {
SessionState.getSelectedIndex().setSelectedPage(i);
tabHost.getTabWidget()
.getChildAt(i)
.setBackgroundResource(
R.drawable.tab_selected_background);
} else {
tabHost.getTabWidget()
.getChildAt(i)
.setBackgroundResource(
R.drawable.tab_unselected_background);
}
}
}
然后我每次选项卡更改时都调用该方法
tabhost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
updateTabs();
}
});
我希望这能帮助