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应用程序运行原理 ...
随机推荐
- SVN出现Invalid authz configuration解决方案
思路: 1.检查是否为不存在的用户组或用户设置了权限(大部分为此问题,调整用户权限或删除账号后,但忘了去掉某个文件夹的权限) 2.检查authz文件的编码: 3.更改权限后是否未重启svn. 4.按照 ...
- Python3.5 Day2作业:购物车程序
需求: 1. 启动程序后,用户通过账号密码登录,然后打印商品列表. 2. 允许用户根据商品编号购买商品. 3. 用户选择商品后,检测余额是否足够,够就直接扣款,不够就提醒充值. 4. 可随时退出,退出 ...
- OpenSSL 1.0.2e 3 Dec 2015
目录: 1,交叉编译openssl 2,win32 vc9 编译 openssl 1,交叉编译openssl [原]交叉编译openssl不修改Makefile的方法 http://blog.chi ...
- Mongodb常用命令介绍
查看命令的方式: 1.在shell中运行db.listCommands() 2.在浏览器中访问管理员接口:http://ipaddress:28017/_commands 下面介绍在Mongodb中最 ...
- *HDU 1115 计算几何
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- html5、canvas绘制本地时钟
效果图: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- svg格式的中国地图轮廓图
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C ...
- 解读ASP.NET 5 & MVC6系列(5):Configuration配置信息管理
在前面的章节中,我们知道新版的MVC程序抛弃了原来的web.config文件机制,取而代替的是config.json,今天我们就来深入研究一下配置文件的相关内容. 基本用法 新版的配置信息机制在Mic ...
- ABP理论学习之EntityFramework集成
返回总目录 本篇目录 Nuget包 创建DbContext 仓储 仓储基类 实现仓储 自定义仓储方法 阅读其他 ABP可以使用任何ORM框架工作,并且已经内置了EntityFramework集成.这篇 ...
- 【大型网站技术实践】初级篇:搭建MySQL主从复制经典架构
一.业务发展驱动数据发展 随着网站业务的不断发展,用户量的不断增加,数据量成倍地增长,数据库的访问量也呈线性地增长.特别是在用户访问高峰期间,并发访问量突然增大,数据库的负载压力也会增大,如果架构方案 ...