TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, NewUserActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
spec = tabHost.newTabSpec("New User").setIndicator("New User").setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ExistingUserActivity.class);
spec = tabHost.newTabSpec("Existing User").setIndicator("Existing User").setContent(intent);
tabHost.addTab(spec);
tabHost.getTabWidget()
.getChildAt(0)
.setLayoutParams(
new LinearLayout.LayoutParams((width / 2) - 2, 40));
tabHost.getTabWidget()
.getChildAt(1)
.setLayoutParams(
new LinearLayout.LayoutParams((width / 2) - 2, 40));
TabWidget tw = getTabWidget();
tw.getChildAt(0).setBackgroundColor(Color.parseColor("#800000"));
tw.getChildAt(1).setBackgroundColor(Color.parseColor("#FF6347"));
}
@Override
public void onTabChanged(String x) {
}
上面的代码初始化了 TabHost 。它使用两个选项卡。一个新用户和另一个现有用户。单击其中任何一个时,我希望该选项卡被聚焦并且颜色与另一个选项卡不同。我不知道如何使用 OnTabChanged 方法。