我们在开发Android程序的时候,会经常使用Tab界面,但有些时候我们需要动态创建Tab页,这里就简单介绍创建Tab的方法。
第一步 界面格局设定
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="65px">
<LinearLayout
android:id="@+id/content1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="tab1"
android:id="@+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/content2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="tab2"
android:id="@+id/mail"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</FrameLayout>
</TabHost>
第二步 编写TabActivity的代码
package eoe.stu.ui.tabactivity;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class AndroidEggTabActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabs = getTabHost();
TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setIndicator("Android");
tab1.setContent(R.id.content1);
tabs.addTab(tab1);
TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setIndicator("Egg");
tab2.setContent(R.id.content2);
tabs.addTab(tab2);
tabs.setCurrentTab(0);
}
}
转载:http://www.adobex.com/android/source/details/00000427.htm