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"><TextViewandroid:text="边城"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:text="围城"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid: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"><TextViewandroid:text="倾城之恋"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:text="灿烂千阳"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid: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"><TextViewandroid:text="百年孤独"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:text="房子里的大象"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid: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;@Overrideprotected 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}" ><TabHostandroid: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"/><TabWidgetandroid: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 {@Overrideprotected 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的介绍内容完成了,这部分知识并不难,相信大家一定已经掌握了。新手学习,高手交流。

转载于:https://www.cnblogs.com/AndroidJotting/p/4943624.html

Android之TabActivity的使用相关推荐

  1. Android自定义TabActivity(实现仿新浪微博底部菜单更新UI)

    如今Android上很多应用都采用底部菜单控制更新的UI这种框架,例如新浪微博 点击底部菜单的选项可以更新界面.底部菜单可以使用TabHost来实现,不过用过TabHost的人都知道自定义TabHos ...

  2. 【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏

    转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/8989063       从这一篇文章开始,我们将进入到一个应用程序主界面UI ...

  3. 微博动态来自Android,Android新浪微博开发(二)主UI的实现:动态建立TabActivity

    仿照官网微博,设立5个窗口,这就需要TabActivity.动态建立TabActivity的步骤如下: 1 建立所需的Activity 跟官方的一样,也建5个,里面的先空着,别忘了最后要在配置文件中加 ...

  4. android Tabhost部件

    本文结合源代码和实例来说明TabHost的用法. 使用TabHost 可以在一个屏幕间进行不同版面的切换,例如android自带的拨号应用,截图: 查看tabhost的源代码,主要实例变量有: pri ...

  5. android 中使用TabHost控件实现微信界面的底部菜单效果

    首先,在布局文件中的代码如下:(菜单位于底部,需要在代码中设置) <TabHostandroid:id="@android:id/tabhost"android:layout ...

  6. Android入门之TabHost,TabWidget

    为什么80%的码农都做不了架构师?>>>    这回要介绍的是Android的Tab控件,Tab控件可以达到分页的效果,让一个屏幕的内容尽量丰富,当然也会增加开发的复杂程度,在有必要 ...

  7. Android底部菜单栏 仿微博效果

    实现方式一:通过TabWidget实现 这种方式主要是在布局中将TabWidget标签嵌套在RelativeLayout中,并且在TabWidget标签中中设置 android:layout_alig ...

  8. Android开发之自定义TabHost文字及背景(源代码分享)

    使用TabHost 可以在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该如何进行自定义修改优化呢 MainActivity的源代码 package com.dream. ...

  9. 一个Demo让你掌握所有的android控件

    注:这个例子来自"安卓巴士",仅在此学习,阅读      下面给出实现各个组件的源代码: 1.下拉框实现--Spinner package com.cellcom;import j ...

最新文章

  1. 神策数据助力海尔落地 6 大智慧厨房在线场景
  2. 苹果的工作官方检索地址
  3. 要玩转这个星际争霸II开源AI,你只需要i5+GTX1050
  4. mysql 授权用户_MySQL创建用户与授权
  5. 【swift学习笔记】三.使用xib自定义UITableViewCell
  6. Java GregorianCalendar getActualMinimum()方法与示例
  7. phpMyAdmin链接MySQL拒接_phpmyadmin连接MySQL服务器被拒绝
  8. Python模拟智能开关设备MQTT接入阿里云物联网平台 - PyCharm paho.mqtt
  9. ffmpeg time_base详解
  10. surfer 8 scripter 学习笔记(9)surfer与VB结合的VB源代码
  11. python调用数据集mnist_使用MNIST数据集进行分类
  12. Sketch项目安装缺失字体
  13. 设计模式解密(17)- 备忘录模式
  14. 统计之均值中位数众数全距四分位数以及箱线图展示
  15. Graylog日志简介
  16. 模型是如何训练出来的
  17. 记录一次H3C交换机的配置
  18. 极兔快递单号查询API
  19. 测试游戏战地1配置软件,战地1测试版
  20. SpringBoot集成shiro框架

热门文章

  1. 国家加强网络数据安全管控 中信国安鸿联九五助力企业保障数据安全
  2. mac打开桌面与屏幕保护程序卡死(解决方案)
  3. 一些个人总结(2008)
  4. react 微信公众平台实现支付功能
  5. 联合国基金会 广告投放 策略
  6. 33、Python第三方库安装和使用
  7. 祝朋友生日前程似锦的句子:愿未来锦上添花
  8. C ++ Primer Plus 第六版 第九章编程练习答案
  9. 向NS2中添加协议PING[转载]
  10. GAIN: Missing Data Imputation using Generative Adversarial Nets(基于生成对抗网络的缺失数据填补)论文详解