zoukankan      html  css  js  c++  java
  • 使用Intent创建Tab页面

         前面已经介绍了如何使用TabActivity来创建Activity布局,前面添加Tab页面使用了TabHost.TabSpec如下方法。

    • setContent(int viewId):直接将指定View组件设置成Tab页的Content,实际上TabHost.TabSpec还提供了一个如下方法。
    • setContent(Intent intent):直接将指定Intent对应的Activity设置成Tab页的Content。    

         下面是该Activity的布局文件:

    <TabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <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="match_parent">
            </FrameLayout>
        </LinearLayout>
    </TabHost>

    对应Activity的代码如下:

    package com.example.studyintent;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.TabActivity;
    import android.content.Intent;
    import android.view.Menu;
    import android.widget.TabHost;
    
    public class IntentTab extends TabActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_intent_tab);
            //获取该Activity里面的TabHost组件
            TabHost tabHost=getTabHost();
            //使用Intent添加第一个Tab页面
            tabHost.addTab(tabHost.newTabSpec("tab1")
                    .setIndicator("已接电话",getResources().getDrawable(R.drawable.ic_launcher))
                    .setContent(new Intent(this,BeCalledActivity.class)));
            //使用Intent添加第二个Tab页面
            tabHost.addTab(tabHost.newTabSpec("tab1")
                    .setIndicator("呼出电话")
                    .setContent(new Intent(this,CalledActivity.class)));
            //使用Intent添加第三个Tab页面
            tabHost.addTab(tabHost.newTabSpec("tab1")
                    .setIndicator("未接电话")
                    .setContent(new Intent(this,NoCallActivity.class)));
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.intent_tab, menu);
            return true;
        }
    
    }

    上面的程序中三行粗体字代码用于为TabHost.TabSpec设置Content,三行粗体字代码分别传入了三个Intent对象,这意味着该TabHost将直接以三个Activity类作为Tab页。

    上面的程序还需要BeCalledActivity、CalledActivity、NoCalledActivity三个Activity类,不过这三个Activity都很简单,故此处不再给出。运行上面的程序将看到如图5.16所示的界面。

         

  • 相关阅读:
    Bacula Plugins
    getopt、getopt_long命令参数
    Notepad++ 快捷键
    make命令
    Linux目录结构
    rhel安装输入法
    libtool编译
    install和cp
    dlopen动态链接库操作
    结构体赋值
  • 原文地址:https://www.cnblogs.com/wolipengbo/p/3428318.html
Copyright © 2011-2022 走看看