就是常见这个界面,可以选择的页卡:
public class MyTabHostAndGridViewActivity extends TabActivity {
private ImageView mImageTabHost;
private TextView mTextTabHost;
private LayoutInflater mLayoutInflater;
private TabHost mTabHost;
final private int one = 1;
final private int two = 2;
final private int three = 3;
final private int four = 4;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_tabhost_layout);
mLayoutInflater = LayoutInflater.from(this);
//实例化TabHost
mTabHost = getTabHost();
//创建第一个页卡
TabSpec spe1 = mTabHost.newTabSpec("One");
spe1.setIndicator(getListView(one));
spe1.setContent(new Intent(this, MyTabOneGostActivity.class));
//创建第二个页卡
TabSpec spe2 = mTabHost.newTabSpec("Two");
spe2.setIndicator(getListView(two));
spe2.setContent(new Intent(this, MainActivity.class));
//创建第三个页卡
TabSpec spe3 = mTabHost.newTabSpec("Three");
spe3.setIndicator(getListView(three));
spe3.setContent(new Intent(this, MyTabWidgetActivity.class));
//创建第四个页卡
TabSpec spe4 = mTabHost.newTabSpec("Four");
spe4.setIndicator(getListView(four));
spe4.setContent(new Intent(this, MyMoreActivity.class));
然后在把创建的页卡加入到TabWidget中
mTabHost.addTab(spe1);
mTabHost.addTab(spe2);
mTabHost.addTab(spe3);
mTabHost.addTab(spe4);
}
//自定义页卡的名字
public View getListView(int n) {
View viewTab = mLayoutInflater.inflate(
R.layout.activity_main_tabhost_public_layout, null);
mImageTabHost = (ImageView) viewTab
.findViewById(R.id.main_tab_host_iamge);
mTextTabHost = (TextView) viewTab
.findViewById(R.id.main_tab_host_text_content);
switch (n) {
case one:
mImageTabHost.setBackgroundResource(R.drawable.select_my);
mTextTabHost.setText("团购");
break;
case two:
mImageTabHost.setBackgroundResource(R.drawable.selcet_business);
mTextTabHost.setText("商家");
break;
case three:
mImageTabHost.setBackgroundResource(R.drawable.select_me);
mTextTabHost.setText("我的");
break;
case four:
mImageTabHost.setBackgroundResource(R.drawable.select_more);
mTextTabHost.setText("更多");
break;
}
return viewTab;
}
}
代码的布局文件:均需要引用系统的ID来处理
<?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="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/bung"
android:paddingBottom="5dp"
android:paddingTop="5dp" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_above="@android:id/tabs">
</FrameLayout>
</RelativeLayout>
</TabHost>