Android学习——TabHost的使用一

发布时间:2020-06-04 09:59:41 作者:uncom2005
来源:网络 阅读:1070

TabHost是一种非常使用的组件,TabHost可以方便的在窗口上放置多个标签页,每个标签页相当于或得了一个与外部容器相同大小的组件摆放区域。

与TabHost 结合使用的组件

 TabWidget:代表选项卡的标签条。

 TabSpec:代表选项卡的一个Tab页面。

创建添加选项卡的方法:

 newTabSpec():创建选项卡

 addTab():添加选项卡

使用TabHost的步骤:

  1. 在界面布局中定义TabHost组件

  2. Activity继承TabActivity

  3. 调用TabActivity的getTabHost()方法获取TabHost

  4. 通过TabHost对象的方法来创建添加选项卡。

     

    如果程序需要监控TabHost里当前标签页的改变,可以为他设置TabHost.onTabChangeListener 的监听器。




实例:1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
   android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="毓璜顶小学"
        android:textSize="20sp"
        />
    </LinearLayout>
<TabHost
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@android:id/tabhost"
    android:layout_weight="1"
    ><!--引用android系统已有的id-->
    <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"></TabWidget>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!--定义第一个标签页的内容-->
            <LinearLayout
                android:id="@+id/tab01"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="消息页面"
                    android:textSize="30sp"
                    />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/tab02"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="班级页面"
                    android:textSize="30sp"
                    />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/tab03"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="我的页面"
                    android:textSize="30sp"
                    />
            </LinearLayout>
        </FrameLayout>

    </LinearLayout>
</TabHost>
</LinearLayout>

注意:

 android:id="@android:id/tabhost"
 android:id="@android:id/tabs"
 android:id="@android:id/tabcontent"
 引用android系统已有的id
public class MainActivity extends TabActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        //获取tabhost组件
        TabHost tabHost=getTabHost();
        //创建第一个Tab页
        TabHost.TabSpec tab1=tabHost.newTabSpec("tab1")
                .setIndicator("班级")//设置标题
                .setContent(R.id.tab01);
        //添加第一个tab页
        tabHost.addTab(tab1);
        TabHost.TabSpec tab2=tabHost.newTabSpec("tab2")
                .setIndicator("消息")//设置标题
                .setContent(R.id.tab02);
        //添加第一个tab页
        tabHost.addTab(tab2);

        TabHost.TabSpec tab3=tabHost.newTabSpec("tab3")
                .setIndicator("我")//设置标题
                .setContent(R.id.tab03);
        //添加第一个tab页
        tabHost.addTab(tab3);
    }
}

注意:最新版本的Android推荐使用Fragment 代替TabActivity

Android学习——TabHost的使用一

推荐阅读:
  1. android基础之TabSpec和TabHost
  2. Android ARM 汇编学习(一)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

程序 监控 标签

上一篇:go语言中bytes包的使用方法

下一篇:ansible中的幂等性是什么?怎么用?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》