MainActivity.java
public class MainActivity extends TabActivity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//加载底部Tab布局
LinearLayout tab1=(LinearLayout)LayoutInflater.from(this).inflate(R.layout.item, null);
final ImageView icon1=(ImageView)tab1.findViewById(R.id.icon);
icon1.setBackgroundResource(R.drawable.icon11);
final TextView title1=(TextView)tab1.findViewById(R.id.title);
title1.setText("First");
LinearLayout tab2=(LinearLayout)LayoutInflater.from(this).inflate(R.layout.item, null);
ImageView icon2=(ImageView)tab2.findViewById(R.id.icon);
icon2.setBackgroundResource(R.drawable.icon12);
TextView title2=(TextView)tab2.findViewById(R.id.title);
title2.setText("Second");
// 加载TabSpec
/* tabHost = (TabHost) findViewById(R.id.view_tab_host);
tabHost.setup(getLocalActivityManager()); */
tabHost = getTabHost();
//tabHost.setup();
TabSpec ts1 = tabHost.newTabSpec("Activity1");
ts1.setIndicator(tab1);
ts1.setContent(new Intent(this, FirstActivity1.class));
tabHost.addTab(ts1);
TabSpec ts2 = tabHost.newTabSpec("Activity2");
ts2.setIndicator(tab2);
ts2.setContent(new Intent(this, SecondActivity2.class));
tabHost.addTab(ts2);
//设置标签栏背景图片
TabWidget tw = tabHost.getTabWidget();
tw.setBackgroundResource(R.drawable.tabbarbg);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
main.xml
<pre name="code" class="html"><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_vertical" >
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:layout_weight="0">
</TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout> </span>
item.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="8sp"
android:textColor="#00FF00"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal"
/>
</LinearLayout> </span>
运行结果: