使用tab有两种方式
1、不继承tabActivity
这种方式首先在布局中TabHost的id必须是android:id="@android:id/tabhost",TabWidget的id必须是 android:id="@android:id/tabs"
TabHost还包含FrameLayout,id也必须是android:id="@android:id/tabcontent",FrameLayout里面可以放不同的布局
在activity中,通过findviewbyid找到TabHost,在通过tabhost的setup()方法找到TabWidget 和 FrameLayot,这个方法底层实现了这种功能
最后通过addTab()方法就能实现了
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二页" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三页" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到选项卡
TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);
tabhost.setup();
TabSpec tab1 = tabhost.newTabSpec("tab1");
tab1.setIndicator("首页", getResources().getDrawable(R.drawable.ic_launcher));
tab1.setContent(R.id.ll1);
tabhost.addTab(tab1);
TabSpec tab2 = tabhost.newTabSpec("tab2");
tab2.setIndicator("第二页", getResources().getDrawable(R.drawable.ic_launcher));//指定头
tab2.setContent(R.id.ll2);//指定内容
tabhost.addTab(tab2);
TabSpec tab3 = tabhost.newTabSpec("tab3");
tab3.setIndicator("第三页", getResources().getDrawable(R.drawable.ic_launcher));
tab3.setContent(R.id.ll3);
tabhost.addTab(tab3);
}
}
2、继承TabActivity
这种方式就不用setcontentview找到布局,也就是不用findviewbyid找到tabhost,只需要通过TabHost tabhost = getTabHost();就可以找打tabhost
注意这连个方法的参数
// tab1.setContent(viewId); 通过id找到tabhost的内容
// tab1.setContent(intent);通过intent找到tabhost的内容,这时候就需要新建activity作为内容,这种方式方便,可以重新用布局来定义tabhost的内容,开发中常见
// tab1.setIndicator(view); 这种方式通过一个view来指定头显示,可以自定义view
// tab1.setIndicator(label, icon); 这种方式是通过文字和图片来显示头
public class MainActivity2 extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabhost = getTabHost();
TabSpec tab1 = tabhost.newTabSpec("tab1");
tab1.setIndicator(createView("首页"));//这里传递一个view,可自定义
tab1.setContent(new Intent(this,Activity1.class));
tabhost.addTab(tab1);
TabSpec tab2 = tabhost.newTabSpec("tab2");
tab2.setIndicator(createView("第二页"));
tab2.setContent(new Intent(this,Activity2.class));
tabhost.addTab(tab2);
TabSpec tab3 = tabhost.newTabSpec("tab3");
tab3.setIndicator(createView("第三页"));
tab3.setContent(new Intent(this,Activity3.class));
tabhost.addTab(tab3);
}
public View createView(String name){
View view = getLayoutInflater().inflate(R.layout.header, null);
TextView tv = (TextView) view.findViewById(R.id.tv_info);
tv.setText(name);
return view;
}
}