android加载页面切换
1.样例代码
protected void initTabHost()
{
tabHost=(TabHost)findViewById(R.id.tabhost);
tabHost.setup();
LayoutInflater.from(this).inflate(R.layout.tab1, tabHost.getTabContentView());
LayoutInflater.from(this).inflate(R.layout.tab3, tabHost.getTabContentView());
LayoutInflater.from(this).inflate(R.layout.tab4, tabHost.getTabContentView());
LayoutInflater.from(this).inflate(R.layout.tab5, tabHost.getTabContentView());
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("首页").setContent(R.id.tab1));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("购物车").setContent(R.id.tab3));
tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("聊天").setContent(R.id.tab4));
tabHost.addTab(tabHost.newTabSpec("tab5").setIndicator("个人中心").setContent(R.id.tab5));
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if (tabId.equals("tab1")) { //第一个标签
Toast.makeText(MainActivity.this, "点击标签页一", Toast.LENGTH_SHORT).show();
}
if (tabId.equals("tab2")) { //第二个标签
Toast.makeText(MainActivity.this, "点击标签页二", Toast.LENGTH_SHORT).show();
}
if (tabId.equals("tab3")) { //第三个标签
Toast.makeText(MainActivity.this, "点击标签页三", Toast.LENGTH_SHORT).show();
}
}
});
}
2.运行结果