Android之TabActivity的使用
TabActivity实现多页显示效果
由于手机屏幕有限,所以我们要尽量充分利用屏幕资源。在我们的应用程序中通常有多个Activity,而且会经常切换显示,这样我们就可以用TabActivity来显示。先看一下效果:

下面我先带领大家实现一下最简单的一种实现:
首先我们的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activitytwo" > <TabHost
android:id="@+id/bookTabHost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/doneBook"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:text="边城"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="围城"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="追风筝的人"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout> <LinearLayout
android:id="@+id/doingBook"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:text="倾城之恋"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="灿烂千阳"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="活着"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout> <LinearLayout
android:id="@+id/willBook"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:text="百年孤独"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="房子里的大象"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="忏悔"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</TabHost> </RelativeLayout>
我们的主Activity代码:
public class MainActivity extends TabActivity{
public Button button_two;
public TabHost bookth = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bookth = getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_two, bookth.getTabContentView(), true);
bookth.addTab(bookth.newTabSpec("done").setIndicator("已读").setContent(R.id.doneBook));
bookth.addTab(bookth.newTabSpec("doing").setIndicator("正读").setContent(R.id.doingBook));
bookth.addTab(bookth.newTabSpec("will").setIndicator("未读").setContent(R.id.willBook));
}
}
ok我们的上图效果就已经完成了,代码很简单,就不再多做解释。下面我们来一起看一下另一种实现方式:
我们的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
/>
<TabWidget
android:layout_alignParentBottom="true"
android:id="@+id/tabwidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</TabHost>
</RelativeLayout>
我们的主Activity代码:
public class Activityone extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
LayoutInflater.from(this).inflate(R.layout.activity_one, tabHost.getTabContentView(), true);
intent = new Intent().setClass(Activityone.this, Activitytwo.class);
tabHost.addTab(tabHost.newTabSpec("拨号").setIndicator("拨号", res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent));
// intent = new Intent().setClass(Activityone.this,Activitytwo.class);
//
// spec = tabHost.newTabSpec("拨号").setIndicator("拨号", res.getDrawable(R.drawable.ic_tab_artists))
// .setContent(intent);
// tabHost.addTab(spec);
intent = new Intent().setClass(Activityone.this, Activitythree.class);
spec = tabHost.newTabSpec("联系人").setIndicator("联系人", res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(Activityone.this, Activityfour.class);
spec = tabHost.newTabSpec("通话记录").setIndicator("通话记录", res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}
}
请注意红色字体部分,这里使用了一个图片的配置文件(ic_tab_albums.xml):
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/ic_tab_albums_grey"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/ic_tab_albums_white"
android:state_selected="false" />
</selector>
好了到这里我们关于TabActivity的介绍内容完成了,这部分知识并不难,相信大家一定已经掌握了。新手学习,高手交流。
Android之TabActivity的使用的更多相关文章
- Android 底部TabActivity(0)——开篇(界面分析|系列文章文件夹)
当下主流的软件没有一个统一明白的风格,App框架什么样的都有,但个人钟情于页面底部Tab分签架构,移动设备的屏幕尽管越来越大,可是显示的内容还是有限,为了能展示很多其它的内容,方便简洁的操作习惯中Ta ...
- Android 底部TabActivity(1)——FragmentActivity
先看看效果图: 第一篇Tab系列的文章首先实现这样的风格的底部Tab:背景条颜色不变,我们是用了深灰的颜色,图标会发生对应的变化.当选中某个标签后该标签的背板会由正常的颜色变为不正常,哈哈,是变为加深 ...
- 学习Android之第六个小程序新浪微博(二)(ListView和TabActivity)
效果图例如以下: 选项卡的使用: 1.继承TabActivity 2.声明TabHost变量,通过方法getTabHost()获取并赋值. (TabHost tabHost =getTabHost( ...
- Android 轮换页面+TabHost 实例
最终效果展示: 首先我们需要一个ViewPager控件,不过可以发现在左侧的控件列表中并没有这个控件 这时我们要去升级包中查看 然后在厘米找到 ViewPager.class 这时我们双击这个发现不能 ...
- Android应用底部导航栏(选项卡)实例
现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其 ...
- Android Listview
方法一: xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...
- TabActivity 切换Activity界面
TAB切换先上图,tab标题没有添加样式,因为setIndicator可以直接接收View,所以可以自己编辑样式: 也可以实现OnTabChangeListener监听tab的点击,改变tab点击后的 ...
- Android工作学习第5天之TabHost实现菜单栏底部显示
TabHost是一个装载选项卡窗口的容器,实现分模块显示的效果.像新浪微博客户端.微信客户端都是使用tabehost组件来开发的. TabHost的组成: |---TabWidget:实现标签栏,可供 ...
- 【Android UI】Android开发之View的几种布局方式及实践
引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...
随机推荐
- Yii框架CURD方法
在YII框架中,CURD有2种方式: 1.AR模式:2. DAO模式 AR模式下 查全部 MODEL $model->find()->asArray()->all()查单 个 ...
- 20155315庄艺霖--对做中学的理解及对c语言和Java的看法
关于做中学的理解及技能训练的思考 在写这篇博客之前,我首先阅读了娄老师的博客,对做中学的概念很感兴趣.我们常说知识要学以致用,做中学强调的是在用的过程中有新的收获和体会来进一步巩固学习.细数我学过的课 ...
- ajax的一些笔试面试题
1. 什么是ajax,为什么要使用Ajax(请谈一下你对Ajax的认识) 什么是ajax: AJAX是“Asynchronous JavaScript and XML”的缩写.他是指一种创建交互式网页 ...
- Android 网络状态检测
package com.example.administrator.yunstore.net; import android.app.AlertDialog; import android.conte ...
- PHP定界符使用技巧
为什么要使用定界符 : 因为PHP是一个Web编程语言,在编程过程中难免会遇到用echo来输出大段的html和javascript脚本的情况,如果用传统的输出方法——按字符串输出的话,肯定要有大量的转 ...
- android的EditText重新获取焦点问题
在android开发过程中关于EditText在setFocusable(false);后,当需要再次获得焦点输入时设置setFocusable(true);后还是无法获得焦点的问题: 解决办法: 对 ...
- sscanf_强大的数据读取-转换
function <cstdio> sscanf int sscanf ( const char * s, const char * format, ...); Read formatte ...
- Hbuilder开发HTML5 APP之WebView
WebView就是原生的WebView,HBuilder在其上封装了一层,便于Javascript的调用,结构如图: 也可以实现这样的结构: 注意:WebView的使用属性HTML5+规范,所以必须等 ...
- ASP.Net请求处理机制初步探索之旅 - Part 4 WebForm页面生命周期
开篇:上一篇我们了解了所谓的请求处理管道,在众多的事件中微软开放了19个重要的事件给我们,我们可以注入一些自定义的业务逻辑实现应用的个性化设计.本篇,我们来看看WebForm模式下的页面生命周期. ( ...
- 前端编码规范之CSS
"字是门面书是屋",我们不会去手写代码,但是敲出来的代码要好看.有条理,这还必须得有一点约束~ 团队开发中,每个人的编码风格都不尽相同,有时候可能存在很大的差异,为了便于压缩组件对 ...