Android开发之TabActivity用法实例详解
作者:炫_愛羊 发布时间:2022-08-09 00:09:03
本文实例讲述了Android开发之TabActivity用法。分享给大家供大家参考,具体如下:
一.简介
TabActivity继承自Activity,目的是让同一界面容纳更多的内容。TabActivity实现标签页的功能,通过导航栏对各个页面进行管理。
二.XML布局文件
注意:
1.TabActivity的布局文件要求以TabHost作为XML布局文件的根。
2.通常我们采用线性布局,所以<TabHost> 的子元素是 <LinearLayout>。
3.<TabWidget>对应Tab
<FrameLayout>则用于包含Tab需要展示的内容
需要注意的是<TabWidget> 和<FrameLayout>的Id 必须使用系统id,分别为android:id/tabs 和 android:id/tabcontent 。
因为系统会使用者两个id来初始化TabHost的两个实例变量(mTabWidget 和 mTabContent)。
4.代码示例
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
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>
</LinearLayout>
</TabHost>
三.TabActivity
1.TabHost:TabHost是Tab的载体,用来管理Tab。
2.TabHost的一些函数
(1)获取
TabHost tabHost=this.getTabHost();
(2) 创建TabHost.TabSpec
public TabHost.TabSpec newTabSpec (String tag)
(3)添加tab
public void addTab (TabHost.TabSpec tabSpec)
(4)remove所有的Tabs
public void clearAllTabs ()
public int getCurrentTab ()
(5) 设置当前的Tab (by index)
public void setCurrentTab (int index)
(6) 设置当前的(Tab by tag)
public void setCurrentTabByTag (String tag)
(7)设置TabChanged事件的响应处理
public void setOnTabChangedListener (TabHost.OnTabChangeListener l)
3.TabHost.TabSpec要设置tab的label和content,需要设置TabHost.TabSpec类。TabHost.TabSpec管理:
public String getTag ()
public TabHost.TabSpec setContent
public TabHost.TabSpec setIndicator
(1)Indicator这里的Indicator 就是Tab上的label,它可以
设置label :
setIndicator (CharSequence label)
设置label和icon :
setIndicator (CharSequence label, Drawable icon)
指定某个view :
setIndicator (View view)
(2)Content对于Content ,就是Tab里面的内容,可以
设置View的id :
setContent(int viewId)
用new Intent 来引入其他Activity的内容:setContent(Intent intent)
package com.zhanglong.music;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TabHost;
public class MainActivity extends TabActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, ListActivity.class);
spec = tabHost.newTabSpec("音乐").setIndicator("音乐",
res.getDrawable(R.drawable.item))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ArtistsActivity.class);
spec = tabHost.newTabSpec("艺术家").setIndicator("艺术家",
res.getDrawable(R.drawable.artist))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("专辑").setIndicator("专辑",
res.getDrawable(R.drawable.album))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("最近播放").setIndicator("最近播放",
res.getDrawable(R.drawable.album))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
希望本文所述对大家Android程序设计有所帮助。
猜你喜欢
- 刚开始我以为熔断和降级是一体的,以为他们必须配合使用; 只不过名字不一样而已,但是当我经过思考过后,发现他们其实不是一个东西;降级什么是服务
- 具体详细介绍请看下文:在使用文件进行交互数据的应用来说,使用FTP服务器是一个很好的选择。本文使用Apache Jakarta Common
- mybatis一直加载xml,找到错误我们在写springmvc+mybatis项目,启动项目的时候,mapper配置文件一直刷,一直加载。
- 先给大家展示下效果图,如果大家感觉效果不错,请参考实例代码, act2是Main2Activity,act3是Main3Activ
- 在网上学习了一种继承系统AlertDialog然后用一统一方法控制dialog显示的方法,效果还不错,但按钮栏那里的分隔线并不是想要的。于是
- 1.例题题目描述迷宫由 n 行 m 列的单元格组成,每个单元格要么是空地,要么是障碍物。其中1表示空地,可以走通,2表示障碍物。给定起点坐标
- 目录一、SpringBoot 使用 Nacos Config 实现多环境切换1. 现象2. 引入依赖3. 添加bootstrap.yaml配
- 1.最近的项目中,有一个Activity用到Fragment+ViewPager,其中一个fragment中实现了视频播放的功能,包含有Su
- 之前在工作中,需要实现Word打印功能,并且插入图片。当时采取的方式则是使用书签进行操作。首先在word内插入书签,完成后,存为模板。程序加
- 如果你还不是很了解restful,或者认为restful只是一种规范不具有实际意义,推荐一篇osc两年前的文章:RESTful API 设计
- 运用Java编写代码将一个大文件切割成指定大小的小文件思路:对已知文件进行切割操作 –> 得到多个碎片文件使用:1、 1个字节输入流
- 近日工程中,逐渐感觉到原来复制粘贴代码的笨重,突然想起以前有人和我说起过Git和SVN之类的版本管理工具。由于平时主要是写Java代码,所以
- 在java中的整数类型有四种,分别是 byte short int long 其中byte只有一个字节 0或1,在此不详细讲解。
- 1 pom.xml文件注:热部署功能spring-boot-1.3开始有的<!--添加依赖--><dependency&g
- 一、日志工具功能封装Debug类,需要实现功能:1.控制所有日志是否打印;2.除了Log,Warning,Error外,给更多日志种类(不同
- 希尔排序是插入排序的一种,又称"缩小增量排序”,是插入排序算法的一种更高效的改进版本。希尔排序原理1.选定一个增长量h,按照增长量
- Spring Boot读取配置文件1)通过注入ApplicationContext 或者 Environment对象来读取配置文件里的配置信
- 在java中用到的最多的时间类莫过于 java.util.Date了, 由于Date类中将getYear(),getMonth()等获取年、
- HttpClient模拟浏览器登录后发起请求浏览器实现这个效果需要如下几个步骤: 1请求一个需要登录的页
- 在我们编写客户端应用程序时,经常要用到鼠标当前的位置。在C#winform中,可以用Control.MousePosition获得当前鼠标的