zoukankan      html  css  js  c++  java
  • android学习---TabHost

    TabHost

    一个简单的示例:

    布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    
    </LinearLayout>

    实现代码:

    package com.leaf.android;
    
    import java.util.ArrayList;
    import java.util.List;
    import android.app.TabActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TabHost;
    
    //TabContentFactory的意思是当某一选项卡被选中时生成选项卡的内容.
    //如果你的选项卡的内容按某些条件来生成, 请使用该接口.例如:不显示既存的视图而是启动活动.
    public class Main extends TabActivity implements TabHost.TabContentFactory {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            TabHost th = getTabHost();
            // newTabSpecd的作用是获取一个新的 TabHost.TabSpec,并关联到当前 TabHost
            // setIndicator的作用是指定标签和图标作为选项卡的指示符.
            // setContent的作用是指定用于显示选项卡内容的视图 ID.
            th.addTab(th.newTabSpec("all").setIndicator("所有通话记录").setContent(this));
            th.addTab(th.newTabSpec("ok").setIndicator("已接来电").setContent(this));
            th.addTab(th.newTabSpec("cancel").setIndicator("未接来电").setContent(this));
        }
    
        // 创建选项卡内容的回调函数.
        public View createTabContent(String tag) {
            ListView lv = new ListView(this);
            List<String> list = new ArrayList<String>();
            list.add(tag);
            if (tag.equals("all")) {
                list.add("leaf");
                list.add("android");
                list.add("Lilei");
            } else if (tag.equals("ok")) {
                list.add("leaf");
                list.add("Lilei");
            } else {
                list.add("leaf");
            }
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_checked, list);
            lv.setAdapter(adapter);
            return lv;
        }
    }

    效果:

  • 相关阅读:
    第五章 项目范围管理
    一位华为IT总监的12大职场经验谈
    接到面试通知后该做什么
    经验借鉴:外包失败三条血泪经验分享
    项目范围管理收集需求
    绝对不能对老板说的十句傻话
    项目整合实施整体变更控制
    项目整合结束项目或阶段
    9招助你夺取更高职位
    IT人写好简历的原则与方法
  • 原文地址:https://www.cnblogs.com/lea-fu/p/3299465.html
Copyright © 2011-2022 走看看