初学者。

选项卡是安卓常用的一种功能。每个选项对应一个活动。

一般由TabHost实现。 常用的实现方法有两种:

1.继承TabActivity

一般过程:

(1) 在主布局文件中定义框架布局,并在里面加入若干个子布局。每个子布局对应TabHost容器里的一个选项的布局。

(2) 主活动继承TabActivity。用getTabHost()获取TabHost对象。

通过创建对象引用LayoutInflate类中Inflate函数,找到(1)中定义的布局文件,设置为Tab视图。

创建选项,添加选项。

主布局文件activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/tabcontent"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/tab1"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="This is tab1"

/>

android:id="@+id/tab2"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="This is tab2"

/>

"

主Java文件MainActivity.java

package com.example.tabhost1;

import android.os.Bundle;

import android.app.Activity;

import android.app.TabActivity; //android3.0后声明TabActivity过期,不过仍能使用

import android.view.LayoutInflater;

import android.view.Menu;

import android.widget.TabHost;

import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

TabHost tab;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

tab = this.getTabHost(); //获得TabHost对象

LayoutInflater inf = LayoutInflater.from(this);

inf.inflate(R.layout.activity_main, tab.getTabContentView()); //找到布局文件,设置为Tab视图

TabSpec spc1 = tab.newTabSpec("tab1") //创建一个Tab项

.setIndicator("tab1", null) //设置Tab标题

.setContent(R.id.tab1); //设置Tab资源Id

TabSpec spc2 = tab.newTabSpec("tab2").setIndicator("tab2", null).setContent(R.id.tab2);

//把Tab项加如到Tab中

tab.addTab(spc1);

tab.addTab(spc2);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

效果图:

2.不继承TabActivity,自定义TabHost容器

一般过程:

(1) 主布局里自定义TabHost容器,初始化容器

(2) 主Activity直接创建TabHost容器的对象

为这个对象创建选项,添加选项

主布局文件activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/tabhost"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@android:id/tabs"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

android:id="@android:id/tabcontent"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/tab1"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="This is tab1" />

android:id="@+id/tab2"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="This is tab2" />

android:id="@+id/tab3"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="This is tab3" />

主Java文件MainActivity.java

package com.example.tabhost2;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.TabHost;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TabHost tab=(TabHost)findViewById(R.id.tabhost);

tab.setup(); //初始化TabHost容器

//匿名对象方式,添加Tab项

tab.addTab(tab.newTabSpec("tab1").setIndicator("标签1",null).setContent(R.id.tab1));

tab.addTab(tab.newTabSpec("tab2").setIndicator("标签2",null).setContent(R.id.tab2));

tab.addTab(tab.newTabSpec("tab3").setIndicator("标签3",null).setContent(R.id.tab3));

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

效果图:

其实,可以把主布局里的两个子布局独立出来,成为两个独立的布局xml文件。

主布局文件activity_main.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/tabhost"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@android:id/tabs"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

android:id="@android:id/tabcontent"

android:layout_width="match_parent"

android:layout_height="match_parent" >

两个独立出来的子布局tab1.xml和tab2.xml

tab1.xml

android:id="@+id/tab1"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:text="This is tab1"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

tab2.xml

android:id="@+id/tab1"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:text="This is tab1"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

主Java文件MainActivity.java

package com.example.tabhost3;

import android.os.Bundle;

import android.app.Activity;

import android.view.LayoutInflater;

import android.view.Menu;

import android.widget.TabHost;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TabHost tab = (TabHost)findViewById(R.id.tabhost);

tab.setup(); //初始化TabHost容器

LayoutInflater i=LayoutInflater.from(this);

i.inflate(R.layout.tab1, tab.getTabContentView()); //找到tab1布局,设置为Tab视图

i.inflate(R.layout.tab2, tab.getTabContentView()); //找到tab2布局,设置为Tab视图

tab.addTab(tab.newTabSpec("tab1").setIndicator("选项1").setContent(R.id.tab1));

tab.addTab(tab.newTabSpec("tab2").setIndicator("选项2").setContent(R.id.tab2));

}

}

效果图

android 自定义tabhost,安卓选项卡的实现方法(TabActivity),自定义TabHost容器相关推荐

  1. android手机电池温度,安卓手机电池温度查看方法图文教程

    智能手机性能如今越来越强,功耗也相对较高,长时间用手机玩游戏,经常也能感受到机身比较烫.本文主要分享一个安卓手机小技巧,教大家如何查看安卓手机电池温度,让大家更加安全的使用手机. 安卓手机怎么看电池温 ...

  2. java类中自定义函数的调用_关于方法:自定义类中对函数的未解析引用

    我无法解决看似微不足道的问题.我的问题可能是缺乏对Kotlin语言的经验(和理解).不过,我将需要帮助. 我做了一个自定义类,其中包含一个自定义函数.看起来很简单,但是当我尝试使用此功能时,我一直收到 ...

  3. android tabhost的使用方法,android TabHost(选项卡)的使用方法

    首先,定义TabHost的布局文件: android:id="@android:id/tabhost" android:layout_width="fill_parent ...

  4. android 设置系统铃声设置在哪里,安卓手机自定义设置的系统铃声通用方法

    安卓手机自定义设置的系统铃声通用方法 安卓手机自定义设置的系统铃声通用方法 很多使用安卓系统手机的新手,不知道怎么设置自己下载的铃声,以前发过一篇老方法,但是不怎么完美.现在给大家分享一种新方法.如下 ...

  5. 安卓 android:windowsoftinputmode,Android:windowSoftInputMode="adjustResize"无效解决方法

    Android:windowSoftInputMode="adjustResize"无效解决方法 时间:2018-08-16     来源:未知 Android开发中用到软键盘时会出现设置Activi ...

  6. android 自定义 theme,Android使用Theme自定义Activity进入退出动画的方法

    本文实例讲述了Android使用Theme自定义Activity进入退出动画的方法.分享给大家供大家参考,具体如下: 有没有觉得Activity的默认动画太快了或者太难看了.. 我原来使用Activi ...

  7. android手机刷ios6,2017安卓手机刷机方法

    刷机可以全面清理手机内部软件系统,可以不受限制的在各版本中互刷,无需改CODE,也可实现降级.以下是学习啦小编为你精心整理的2017安卓手机刷机方法,希望你喜欢. 2017安卓手机刷机方法 1.在re ...

  8. iphone与android传文件,安卓与苹果手机之间互传文件的方法教程

    一直以来,很多的app都是分为安卓和IOS两个版本,在使用上,两个平台之间很多功能都不能跨平台使用.比如大家在玩游戏时充值的东西,在苹果充值之后,同一个账号转到安卓手机上玩,那些东西都是不能迁移进来的 ...

  9. android 绘画笔迹回放_一种基于可缩放矢量图形的安卓平台笔迹回放方法及装置与流程...

    本发明涉及笔迹显示领域,更具体地,涉及一种基于可缩放矢量图形的安卓平台笔迹回放方法及装置. 背景技术: 智能移动设备平台上的回放技术已遍布许多安卓(Android)软件中,比如:字帖类软件,画图类软件 ...

  10. Android手机静态ip地址网关,安卓手机Android 4.0系统静态ip设置方法【详解】

    安卓系统Android 4.0推出之后,很多网友都进行了版本的更新,但是,一些更新了Android 4.0系统的用户,表示,自从版本升级之后,就不能使用静态IP,这是怎么回事呢?小编经过研究后发现,并 ...

最新文章

  1. hook情况下,解决内联没有:hover的方案
  2. 2020年 第11届 蓝桥杯 第2次模拟赛真题详解及小结【Java版】
  3. 看动画学算法之:doublyLinkedList
  4. python练习笔记——利用信号signal处理僵尸进程
  5. 递归——数的计算(洛谷 P1028)
  6. 怎么判断一个字符串的最长回文子串是否在头尾_回文自动机入门
  7. document.addEventListener的使用介绍
  8. CentOS 7 安装OpenOffice并实现WordToPDF(Java调用)
  9. Atitit usrQC27模块化的规范模块化法 v4 t77 目录 1. 模块化层级(软件项目 1 1.1. 子项目》命名空间package机制》类》类文件》方法函数级别》语句 1 2. 常见的
  10. MongoDB学习(黑马教程)-3-数据库MongoDB的删除文档操作
  11. 同济大学c语言程序设计答案,2020年同济大学道路与铁道工程考研真题试卷及试题答案,汽车理论及设计考研试题下载...
  12. idea导入项目出现时钟标志
  13. python 问卷调查系统_GitHub - JukieChen/surveySystem-1: 问卷调查系统
  14. 计算机如何重新连接打印机,打印机脱机怎么处理 打印机重新连接方法教程
  15. linux更新opengl驱动下载,支持OpenGL 3.2 NVIDIA全新Linux驱动发布
  16. 01-快速入门webpack模块化打包工具
  17. word转html linux java,Java-linux下如何代码实现word转换成html
  18. arduino点阵声音频谱_Arduino实现32分频音频频谱显示器
  19. MySQL (4) 第一范式 第二范式 第三范式 BC范式
  20. 即使你毕业非名校,也能找到月入10k的工作

热门文章

  1. 将RGB图像和depth深度图像存入同一个HDF5文件中
  2. LR(1)项目集族的构造:如何确定前向搜索符
  3. VDO-SLAM 配置 2022 Ubuntu20.04
  4. 关于http协议详解(摘)
  5. MyBatis实现模糊查询的几种方式
  6. 如何让js在浏览器宽度改变的时候执行一个函数?浏览器宽度变化
  7. ASP.Net Mvc 发布网站 (样式+图片问题)
  8. HOJ 2739 The Chinese Postman Problem
  9. 第二十三模板 9对像数组模板
  10. 关于c#中的string