TabHost选项卡
TabHost标签,TabWidget标签,FragmentLayout标签,这三个标签的id都是固定的系统定义的。
TabHost由标签(tabs)和标签体(tabcontent)组成。
是一个容器,可以包含并且运行多个嵌入的activity。
已过时,推荐使用Fragment替代。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<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" >
</FrameLayout>
</TabHost>
</RelativeLayout>
创建TabHost步骤:
1.获取当前activity中的TabHost,getTabHost。
2.创建一个标签TabSpec对象,它里面封装了标签指示符(卡片)和标签体(显示内容),指定标签的详细说明TabSpec ,TabSpec用来指定如何创建标签指示符和标签卡内容。tabHost.addTab(tabspec); TabSpec = TabHost.newTabSpace(tabname);
3.设置标签指示符和标签内容,标签内容是一个意图对象,用来开启一个activity。
4.将标签TabSpec添加到TabHost选项卡中。
---------------------------代码示例-----------------
1.标签卡xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TabHost
android:id="@android:id/tabhost"
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" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</TabHost>
</RelativeLayout>
2.TabActivity类
package com.itheima.tabhost;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
TabSpec tab1 = tabHost.newTabSpec("标签1");
TabSpec tab2 = tabHost.newTabSpec("标签2");
TabSpec tab3 = tabHost.newTabSpec("标签3");
tab1.setIndicator("功能1");
tab1.setContent(new Intent(this,activity01.class));
tab2.setIndicator("功能2");
tab2.setContent(new Intent(this,activity02.class));
tab3.setIndicator("功能3");
tab3.setContent(new Intent(this,activity03.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
}
3.activity01类
package com.itheima.tabhost;
import android.app.Activity;
import android.os.Bundle;
public class activity01 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1_layout);
}
}
4.activity01的xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:text="我是标签1" />
</RelativeLayout>
141

被折叠的 条评论
为什么被折叠?



