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的介绍内容完成了,这部分知识并不难,相信大家一定已经掌握了。新手学习,高手交流。

最新文章

  1. CentOS7下Apache及Tomcat开启SSL
  2. -A 解决数据库表太多,预读表时间很长
  3. 转:Android Webview 加载外部html时选择加载本地的js,css等资源文件
  4. pwd命令(转)
  5. HDU 1698 Just a Hook(线段树成段更新)
  6. Ubuntu 12.04下PostgreSQL-9.1安装与配置详解(在线安装)
  7. 世纪互联运营的Microsoft Azure正式支持FreeBSD虚拟机镜像
  8. Annotation介绍@interface
  9. CSS实现圆角,三角,五角星,五边形,爱心,12角星,8角星,圆,椭圆,圆圈,八卦
  10. Java DES 加密和解密源码(转)
  11. 利用Eclipse中的Maven构建Web项目报错(二)
  12. Shell按行读取文件的3种方法
  13. alexnet- tensorflow
  14. spring BeanFactory及ApplicationContext中Bean的生命周期
  15. ROS中.launch文件的remap标签详解
  16. Android - 内存泄漏 + 垃圾回收(GC)概念
  17. 设置VMWARE通过桥接方式使用主机无线网卡上网(转载)
  18. 在github fork的项目中推送与抓取
  19. ionic新手教程第八课-(加更)从无到有说Ionic、绘图说明MVC-U-S
  20. bazel-编译多目标

热门文章

  1. Ubuntu 14.04 更换阿里云源
  2. asp.net,cookie,写cookie,取cookie
  3. java cookie 工具类
  4. druid sql黑名单 报异常 sql injection violation, part alway true condition not allow
  5. 从Erlang进程看协程思想
  6. Centos7 编译安装 Nginx PHP Mariadb Memcached 扩展 ZendOpcache扩展 (实测 笔记 Centos 7.3 + Mariadb 10.1.20 + Nginx 1.10.2 + PHP 7.1.0 + Laravel 5.3 )
  7. 【hihoCoder】1121:二分图一&#183;二分图判定
  8. 实现滑动条与表单中的input中的value交互
  9. 工作总结_JS_1
  10. ecma6 yield