Android中的假TabHost-只是在上层覆盖了一层TabWidget

TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容。

实现方法:继承TabActivity

1.界面布局

1)、TabHost    必须设置android:id为@android:id/tabhost
2)、TabWidget   必须设置android:id为@android:id/tabs
3)、FrameLayout   必须设置android:id为@android:id/tabcontent

子activity必须是线性布局LinearLayout,否则的话可以显示tabs但是无法显示其它activity

2.实现方法

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.maintabs);

tabHost = getTabHost();

tabHost.addTab(tabHost.newTabSpec(TAB_SALE)
.setIndicator(TAB_SALE)
.setContent(new Intent(this, SaleActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));//每次点击都会刷新
tabHost.addTab(tabHost.newTabSpec(TAB_CART)
.setIndicator(TAB_CART)
.setContent(new Intent(this, CartActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.addTab(tabHost.newTabSpec(TAB_REPORT)
.setIndicator(TAB_REPORT)
.setContent(new Intent(this, ReportActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.addTab(tabHost.newTabSpec(TAB_SETUP)
.setIndicator(TAB_SETUP)
.setContent(new Intent(this, SetupActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

linearLayout1 = (LinearLayout)findViewById(R.id.linearLayout1);
linearLayout1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tabHost.setCurrentTabByTag(TAB_SALE);
}
});
linearLayout2 = (LinearLayout)findViewById(R.id.linearLayout2);
linearLayout2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tabHost.setCurrentTabByTag(TAB_CART);
}
});
linearLayout3 = (LinearLayout)findViewById(R.id.linearLayout3);
linearLayout3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tabHost.setCurrentTabByTag(TAB_REPORT);
}
});
linearLayout4 = (LinearLayout)findViewById(R.id.linearLayout4);
linearLayout4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tabHost.setCurrentTabByTag(TAB_SETUP);
}
});

}
posted on 2016-12-13 15:41  晨小主  阅读(204)  评论(0)    收藏  举报