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

原作者地址: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. log4net不同logger输出日志

    4步曲 1.引用log4net.dll(nuget) 2.任意位置的命名空间头部加入下面的代码,web.config可修改为自己定义的.xml [assembly: log4net.Config.Xm ...

  2. 观察者模式(Observer和Observable实现)

    package com.wzy.java8.thread; import java.util.Observable; import java.util.Observer; public class D ...

  3. jquery的curCSS方法

    核心思想是用getComputedStyle获取样式,如果没有获取到就判断是不是动态创建的元素,如果是则用style获取行内样式.看重点(注释部分)代码吧! curCSS = function( el ...

  4. mysql重置密码

    1.首先停止正在运行的MySQL进程 复制代码代码如下: >net stop mysql  如未加载为服务,可直接在进程管理器或者服务中进行关闭. 2.以安全模式启动MySQL 进入mysql目 ...

  5. mysql: Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '= 的解决

    昨天把mysql里所有table的varchar字段的字符集,批量换成了utf8mb4/utf8mb4_unicode_ci ,以便能保存一些emoji火星文 , 结果有一个sql语句执行时,报错如下 ...

  6. Webpack学习笔记一:What is webpack

      #,Loaders干嘛的,webpack can only process JavaScript natively, but loaders are used to transform other ...

  7. Tp缓存

    系统默认的缓存方式是采用File方式缓存,我们可以在项目配置文件里面定义其他的缓存方式,例如,修改默认的缓存方式为Xcache(当然,你的环境需要支持Xcache) 对于File方式缓存下的缓存目录下 ...

  8. 我的第一篇blog

    加入博客园两年多了,学习.从事编程也两年多了,一直是在网上找资料,都没有认真写写博客. 博客园里面好多功能都还不会用,今天起我也要在博客园写自己的blog了.感觉很高大上的样纸!!

  9. 架构师养成记--4.volatile关键字

    volatile修饰的变量可在多个线程间可见. 如下代码,在子线程运行期间主线程修改属性值并不对子线程产生影响,原因是子线程有自己独立的内存空间,其中有主内存中的变量副本. public class ...

  10. C#安全性记录

    安全性一直是开发中,重中之重的问题.不过平时用的不算特别多,基本上用个MD5,SSL也就到这了.再次记录一下,以免忘记. MD5多次加密 MD5算法是不可逆算法.应用于密码验证,完整性验证这种特征.这 ...