注:本文为转载,但该内容本人已亲身尝试,确认该方法可行,代码有点小的改动,转载用作保存与分享。

原作者地址:http://gundumw100.iteye.com/blog/853967

个人吐嘈:据说TabWidget已经过时了,取而代之的是Fragment,但关于这个Fragment暂时还没时间研讨,现使用的是TabWidget方法,个人感觉实现出来的效果还是很不错的。

自定义TabHost:不用继承TabActivity,具体代码如下:

定义布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TabHost01" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical" android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs"
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 android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="two"
android:id="@+id/TextView02" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ScrollView
android:id="@+id/Scroll01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:background="@android:color/white" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="2dp">
<TextView
android:id="@+id/title01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="20sp"
android:text="员工信息查询测试"
android:padding="5dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:textSize="20sp"
android:text="测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n测试\n"
android:layout_weight="1"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="three"
android:id="@+id/TextView03" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>

  

编写Java代码:

package com.example.test2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.TabHost; public class MainActivity extends Activity {
/** Called when the activity is first created. */
private TabHost tabHost; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
tabHost = (TabHost) this.findViewById(R.id.TabHost01);
tabHost.setup(); tabHost.addTab(tabHost.newTabSpec("tab_1")
.setContent(R.id.LinearLayout1)
//.setIndicator("TAB1",this.getResources().getDrawable(R.drawable.img01)));
.setIndicator("TAB1"));
tabHost.addTab(tabHost.newTabSpec("tab_2")
.setContent(R.id.LinearLayout2)
//.setIndicator("TAB2",this.getResources().getDrawable(R.drawable.img01)));
.setIndicator("TAB2"));
tabHost.addTab(tabHost.newTabSpec("tab_3")
.setContent(R.id.LinearLayout3)
//.setIndicator("TAB3",this.getResources().getDrawable(R.drawable.img01)));
.setIndicator("TAB3"));
tabHost.setCurrentTab(1);
}catch(Exception ex){
ex.printStackTrace();
Log.d("EXCEPTION", ex.getMessage());
} }
}

  

运行图:

本人提供Demo下载:点击此处下载Demo

-全文完-

Android 实现分页(使用TabWidget/TabHost)的更多相关文章

  1. Android:简单实现ViewPager+TabHost+TabWidget实现导航栏导航和滑动切换

    viewPager是v4包里的一个组件,可以实现滑动显示多个界面. android也为viewPager提供了一个adapter,此adapter最少要重写4个方法: public int getCo ...

  2. Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3360938.html 如新浪微博下面的标签切换功能,我以前也写过一篇博文(http:/ ...

  3. Android --ListView分页

    参考博客:Android ListView分页加载(服务端+android端)Demo 监听OnScrollListener事件 class OnListScrollListener implemen ...

  4. Android ListView分页载入(服务端+android端)Demo

    Android ListView分页载入功能 在实际开发中经经常使用到,是每一个开发人员必须掌握的内容,本Demo给出了服务端+Android端的两者的代码,并成功通过了測试. 服务端使用MyEcli ...

  5. android开发之如何使TabHost的TabWidget位于屏幕下方

    更改TabHost里的第一个LinearLayout为RelativeLayout.并在TabWidget中添加android:layout_alignParentBottom="true& ...

  6. 【Android基础篇】TabWidget设置背景和字体

    在使用TabHost实现底部导航栏时,底部导航栏的三个导航button无法在布局文件中进行定制.比方设置点击时的颜色.字体的大小及颜色等,这里提供了一个解决的方法.就是在代码里进行定制. 思路是在Ac ...

  7. Android studio 中的TabWidget

    1.TabWidget 的 layout文件 <?xml version="1.0" encoding="utf-8"?> <TabHost ...

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

    使用TabHost 能够在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该怎样进行自己定义改动优化呢 MainActivity的源码 package com.dream. ...

  9. Android之分页加载数据

    基本的原理和我的上一篇随笔“Android之下拉刷新ListView”差不多,代码里面有注释,这里就不废话了,直接上代码. 自定义的分页显示ListView——PagedListView.java代码 ...

随机推荐

  1. [转]不优雅的方式处理 xlrd 中 int/float 的问题

    原址:http://blog.chedushi.com/archives/7258 最近在用 xlrd 写一个题库自动导出的程序,但碰到一个比较 ugly 的问题. 程序要求是将 xls 文件中的数据 ...

  2. MongoDB学习笔记(二:入门环境配置及与关系型数据库区别总结)

    一.下载及安装MongoDB MongoDB下载官网链接:http://www.mongodb.org/downloads 具体安装步骤教程:http://www.shouce.ren/api/vie ...

  3. J2EE基础之Web服务简介

    J2EE基础之Web服务简介 1.什么是Web服务? 在人们的日常生活中,经常会查询网页上某城市的天气信息,这些信息都是动态的.实时的,它是专业的气象站提供的一种服务.例如,在网上购物时,通常采用网上 ...

  4. Redis学习总结

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API,其实当前最热门的NoSQL数据库之一,NoSQL还包括了Mem ...

  5. java进程占用CPU资源过高分析脚本

    #!/bin/bash #输入占用CPU较高的进程号 pid=$ if [ -z $pid ] then echo "PID is NULL" exit fi #找到该进程中占用较 ...

  6. WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

    http://dl.bintray.com/sequenceiq/sequenceiq-bin/ http://www.secdoctor.com/html/yyjs/31101.html

  7. Log4J日志配置详解

    一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...

  8. Python-09-线程、进程、协程、异步IO

    0. 什么是线程(thread)? 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆 ...

  9. GO语言总结(1)——基本知识

    1.注释(与C++一样) 行注释:// 块注释:/* ... */ 2.标识符 可以这么说,除了数字开头的不允许,符号开头的不允许,关键字不允许,其他的Unicode字符组合都可以.“_33”也可以是 ...

  10. 【笔记4】用pandas实现条目数据格式的推荐算法 (基于用户的协同)

    ''' 基于用户的协同推荐 条目数据 ''' import pandas as pd from io import StringIO import json #数据类型一:条目(用户.商品.打分)(避 ...