android TabHost(选项卡)的使用方法
发布时间:2021-08-09 10:08:39
标签:TabHost,选项卡
首先,定义TabHost的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
其中,TabWidget即是选项卡上面的标签,FrameLayout是选项卡的内容。
在Java类文件中定义如下:
public class MainActivity extends TabActivity {
private TabHost my_tabhost;
private TabWidget my_tabwidget;
private int i,k;
private TextView tv;
private String[] tabMenu = { "系统", "硬件", "操作"};
private Intent intent0, intent1, intent2;
private Intent[] intents = { intent0, intent1, intent2};
private TabHost.TabSpec tabSpec0, tabSpec1, tabSpec2, tabSpec3;
private TabHost.TabSpec[] tabSpecs = { tabSpec0, tabSpec1, tabSpec2, tabSpec3};
public static Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 不要窗体标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
my_tabhost = getTabHost();
intent0 = new Intent(this, system.class);
intent1 = new Intent(this, hardware.class);
intent2 = new Intent(this, operation.class);
tabSpec0 = my_tabhost.newTabSpec("system").setIndicator(tabMenu[0],null).
setContent(intent0);
tabSpec1 = my_tabhost.newTabSpec("hardware").setIndicator(tabMenu[1],null).
setContent(intent1);
tabSpec2 = my_tabhost.newTabSpec("operation").setIndicator(tabMenu[2],null).
setContent(intent2);
my_tabhost.addTab(tabSpec1);
my_tabhost.addTab(tabSpec0);
my_tabhost.addTab(tabSpec2);
<br> // 设置默认选中的选项卡为第2个
my_tabhost.setCurrentTab(1);
}
}
每一个选项卡对应一个Intent,每一个Intent又对应一个类,选中这个选项卡时,就显示对应的类。
运行结果如下:


猜你喜欢
- 什么是apllo开源分布式配置中心?apllo开源分布式配置中心是携程框架部门研发的一个能够集中化管理应用的不同环境、不同集群的配置,并且具
- (未给Fragment的布局设置BackGound)之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文《详解An
- 使用ehcache-spring-annotations使得在工程中简单配置即可使用缓存下载地址:http://code.google.co
- 一.关于数组的特点1.在Java中,无论使用数组或集合,都有边界检查。如果越界操作就会得到一个RuntimeException异常。2.数组
- 在android 6.0开始,部分的权限需要我们动态申请,也就是说当我们的打开app的时候系统不会主动像您申请app所需要的部分权限,需要客
- java 对象的克隆一、对象的浅克隆(1)需要克隆类需要重写Object类的clone方法,并且实现Cloneable接口(标识接口,无需实
- 今天我们来编写一个缩放效果的ImageView ,网上有很多人都讲了这些。但有许多人都直接使用了库文件,那么我们今天做的是直接上代码编写一个
- RecyclerView显示Item布局不一致在自定义RecyclerAdapter的时候,在重写onCreateViewHolder方法是
- java模拟TCP通信实现客户端上传文件到服务器端,供大家参考,具体内容如下客户端package com.zr;import java.io
- 先说能用的究极解决方案,大家着急的直接复制走,以后想了解再过来看没有header,且所有Item的高度一致private fun getSc
- 本文实例讲述了Java swing框架实现的贪吃蛇游戏。分享给大家供大家参考,具体如下:java是门高级语言,做游戏时适合做后台,但是用它也
- 前端采用layui框架,讲解多文件上传的完整实现功能。前端html重点代码如下:<div class="layui-form
- 最近看到一道有点意思的逻辑算法题,便着手实现一下。题目是要求打印 出N*N顺时针螺旋数组,规律如下:// 1 2 &
- 自定义starter yaml提示失效问题问题场景在自定义starter后,必不可少会有properties配置参数需要指定,而在有时又不知
- 在讲使用path绘制多边形时,讲下Canvas的translate(),rotate()方法的使用,本博客中会使用这方面的知识,先单独讲下,
- Criteria的and和or进行联合查询DemoExample example=new DemoExample ();DemoExampl
- 笔者近2天在 Android Studio上玩了一下百度地图,碰到了常见的"230错误 APP Scode校验失败",下
- 本文实例讲述了Android开发实现查询远程服务器的工具类QueryUtils。分享给大家供大家参考,具体如下:/** * 查询远程服务器的
- 项目开发中,经常会遇到定时任务的场景,Spring提供了@Scheduled注解,方便进行定时任务的开发概述要使用@Scheduled注解,
- 今天来学习总结一下,Android 后添加的一些新的组件和UI效果,Material Dialog,SwipeRefreshLayout,L