使用自定义的TabHost可以不用继承TabActicity,但是要注意的是如果使用Activity作为Content的话,有两处代码是一定要加的。不然就会出现RuntimeError,还有在XML布局中使用自定义的TabHost时除了TabHost的id自定义外,LinearLayout和TabWidget的id必须使用系统默认。具体可参考下面的Demo。
使用TabWidget的效果(左)和自定义View的效果图(右):
       
MainActivity加载的XML布局:

<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=".MainActivity" > <TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</TabHost> </RelativeLayout>

MainActivity的Java代码(注意红色代码):

package com.hitech.tabhostdemo;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec; @SuppressWarnings("deprecation")
public class MainActivity extends Activity { private LocalActivityManager lam;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); TabHost tabHost = (TabHost) findViewById(R.id.tabhost); // Intent对象
Intent intent1 = new Intent(this, TabSpecActivity1.class);
Intent intent2 = new Intent(this, TabSpecActivity2.class);
Intent intent3 = new Intent(this, TabSpecActivity3.class); // TabSpec对象
TabSpec tabSpec1 = tabHost.newTabSpec("first");
tabSpec1.setIndicator("第一页",
getResources().getDrawable(android.R.drawable.ic_dialog_email));
tabSpec1.setContent(intent1); TabSpec tabSpec2 = tabHost.newTabSpec("second");
tabSpec2.setIndicator("第二页",
getResources().getDrawable(android.R.drawable.ic_btn_speak_now));
tabSpec2.setContent(intent2); TabSpec tabSpec3 = tabHost.newTabSpec("third");
tabSpec3.setIndicator("第三页",
getResources().getDrawable(android.R.drawable.ic_dialog_dialer));
tabSpec3.setContent(intent3); lam = new LocalActivityManager(this, false);
lam.dispatchCreate(savedInstanceState);
tabHost.setup(lam);

tabHost.addTab(tabSpec1);
tabHost.addTab(tabSpec2);
tabHost.addTab(tabSpec3); // TabActivity
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} @Override
protected void onPause() {
super
.onPause();
lam.dispatchPause(isFinishing());
}
}

自定义View代替TabWidget效果的实现原理:隐藏TabWidget,然后在线性布局中加入自定义View,如上图中即为LinearLayout中添加三个TextView,再分别对TextView设置点击事件来模拟TabWidget的效果。
同样,MainActivity加载的XML布局如下:

<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=".MainActivity" > <TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"android:visibility="gone" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" /> <View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="1dp"
android:background="@android:color/background_light" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/tab_convst"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="-10dp"
android:drawableTop="@drawable/ic_widget_conversation"
android:gravity="center"
android:paddingBottom="5dp"
android:text="@string/conversation" /> <TextView
android:id="@+id/tab_folder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="-10dp"
android:drawableTop="@drawable/ic_widget_folder"
android:gravity="center"
android:paddingBottom="5dp"
android:text="@string/folder" /> <TextView
android:id="@+id/tab_group"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="-10dp"
android:drawableTop="@drawable/ic_widget_group"
android:gravity="center"
android:paddingBottom="5dp"
android:text="@string/group" />
</LinearLayout>
</LinearLayout>
</TabHost> </RelativeLayout>

注意和以往不同之处已做标记,分别是TabWidget和自定义的LinearLayout。
MainActivity中的处理:

package com.hitech.capacitymessage;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView; @SuppressWarnings("deprecation")
public class MainActivity extends Activity implements OnClickListener { private static final String TAG = "MainActivity";
private LocalActivityManager lam;
private TabHost tabHost;
private Bundle state;
private TabSpec tab1;
private TabSpec tab2;
private TabSpec tab3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.state = savedInstanceState;
activityInitializer();
} private void activityInitializer() {
tabHost = (TabHost) findViewById(R.id.tabhost);
tab1 = tabHost.newTabSpec("tab1");
tab1.setIndicator("会话",
getResources().getDrawable(R.drawable.ic_widget_conversation));
tab1.setContent(new Intent(this, TabActivity1.class)); tab2 = tabHost.newTabSpec("tab2");
tab2.setIndicator("文件夹",
getResources().getDrawable(R.drawable.ic_widget_folder));
tab2.setContent(new Intent(this, TabActivity2.class)); tab3 = tabHost.newTabSpec("tab3");
tab3.setIndicator("群组",
getResources().getDrawable(R.drawable.ic_widget_group));
tab3.setContent(new Intent(this, TabActivity3.class)); lam = new LocalActivityManager(this, true);
lam.dispatchCreate(state);
tabHost.setup(lam); tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3); TextView block1 = (TextView) findViewById(R.id.tab_convst);
TextView block2 = (TextView) findViewById(R.id.tab_folder);
TextView block3 = (TextView) findViewById(R.id.tab_group); block1.setOnClickListener(this);
block2.setOnClickListener(this);
block3.setOnClickListener(this);
} @Override
public void onClick(View v) {
String tabTag = tabHost.getCurrentTabTag();
switch (v.getId()) {
case R.id.tab_convst:
if (!tabTag.equals("tab1")) {
tabHost.setCurrentTabByTag("tab1");
}
Log.e(TAG, tabHost.getCurrentTabTag());
break;
case R.id.tab_folder:
if (!tabTag.equals("tab2")) {
tabHost.setCurrentTabByTag("tab2");
}
Log.e(TAG, tabHost.getCurrentTabTag());
break;
case R.id.tab_group:
if (!tabTag.equals("tab3")) {
tabHost.setCurrentTabByTag("tab3");
}
Log.e(TAG, tabHost.getCurrentTabTag());
break;
default:
break;
}
} @Override
protected void onPause() {
super.onPause();
lam.dispatchPause(isFinishing());
}
}

TabHost的自定义的更多相关文章

  1. 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)

    3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.htm ...

  2. TabHost自定义外观

    博客园:http://www.cnblogs.com 农民伯伯: http://www.cnblogs.com/over140 版本 新浪微博 weibo_10235010.apk 正文 一.效果图 ...

  3. 使用自定义RadioButton和ViewPager实现TabHost效果和带滑动的页卡效果

    在工作中又很多需求都不是android系统自带的控件可以达到效果的,内置的TabHost就是,只能达到简单的效果 ,所以这个时候就要自定义控件来达到效果:这个效果就是: 使用自定义RadioButto ...

  4. android自定义tabhost,tabcontent用intent获得

    地址:http://my.oschina.net/aowu/blog/36282 自己改的自定义tabhost组建,效果图如左.有更好的朋友可以相互交流一下,嘿嘿. 1.先上AndroidManife ...

  5. 自定义TabHost,TabWidget样式

    先看效果: 京东商城底部菜单栏 新浪微博底部菜单栏 本次学习效果图:

  6. 安卓自定义类似TabHost的导航栏

    有时候为了项目需要我们要自定义一些导航控件,类似下面这样. 下面给大家讲讲我是怎么实现的, 1.素材准备(这个都是美工的事情) 2.①资源文件共有五个 如下: activity_main_first. ...

  7. Android 自定义TabHost,TabWidget样式

    界面比较简单,要想做得漂亮换几张图片就可以了. 第一步:先在布局(这里用了main.xml创建时自动生成的)里面放上TabHost ,只要将TabHost控件托至屏幕中就可: <?xml ver ...

  8. Android 实现分页(使用TabWidget/TabHost)

    注:本文为转载,但该内容本人已亲身尝试,确认该方法可行,代码有点小的改动,转载用作保存与分享. 原作者地址:http://gundumw100.iteye.com/blog/853967 个人吐嘈:据 ...

  9. 利用TabHost制作QQ客户端标签栏效果(低版本QQ)

    学习一定要从基础学起,只有有一个好的基础,我们才会变得更加的perfect 下面小编将利用TabHost制作QQ客户端标签栏效果(这个版本的QQ是在前几年发布的)…. 首先我们看一下效果: 看到这个界 ...

随机推荐

  1. 【Android】Android程序自己主动更新

    App自己主动更新的步骤可分为三步: 检查更新(假设有更新进行第2步,否则返回) 下载新版的APK安装包 安装APK 以下对这三步进行解释.当中会穿插相应代码.App自己主动更新的这三步所有被封装到了 ...

  2. Hibernate中session回话的get方法和load方法的区别

    1.报错方式不同: 前提:获取的数据不存在 get方法会报异常:空指针异常 load方法会报异常:对象为找到异常,给定值没有行存在. 2.load方法 这种方式总是会返回一个代理而不是真正得去查询数据 ...

  3. Go语言Slice操作.

    1.基本使用方法: a = append(a, b...) 比如:list = appened(list,[]int{1,2,3,4}...) 能够用来合并两个列表. 不用这样了 :list := m ...

  4. android选择图片或拍照图片上传到server(包含上传參数)

    在9ria论坛看到的.还没測试,先Mark与大家分享一下. 近期要搞一个项目,须要上传相冊和拍照的图片.不负所望,最终完毕了! 只是须要说明一下,事实上网上非常多教程拍照的图片.都是缩略图不是非常清晰 ...

  5. spark集群体系结构

  6. [原创]微信小程序 实现 圆环 百分百效果

    1.最终效果 2.技术点:a. css3 clip-path , b.根据角度和直边计算另一个直边的长度 3.实现思路: a.3个层(灰色圆形层, 红色圆形层,白色圆形层)  ,其中灰色和红色层大小一 ...

  7. 新疆大学OJ(ACM) 1047: string 字符串排序

    1047: string 时间限制: 1 Sec  内存限制: 128 MB 题目描述 有n个字符串字符串n<=50000,把所有字符串串起来,得到一个字典序最小的字符串. 输入 输入第一行是一 ...

  8. Timer 的 schedule()方法

    1.timer.schedule(new MyTask(),long time1,long time2); 第一个参数是TimerTask类,使用者要继承该类,并实现run()方法,因为TimerTa ...

  9. 利用canvas做一个简单个gif停止和播放

    var ImagePlayer = function(options) { this.control = options.control; this.image = options.image; th ...

  10. ActiveMQ学习笔记(1)----初识ActiveMQ

    1. 什么是ActiveMQ? ActiveMQ是Apache推出的,一款开源的,完全支持JMS1.1和j2ee1.4规范的JMS Provider实现的消息中间件(Message Oriented ...