使用自定义的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. jquery 函数的定义

    var ss_login = { ptjy : function(method) { CloseAlert(); if( getLocalData("ActivePTJYUser" ...

  2. zzulioj--1791-- 旋转矩阵(模拟水题)

     旋转矩阵 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 268  Solved: 116 SubmitStatusWeb Board Descr ...

  3. swoole-tcp-server

    swoole-tcp-server 标签(空格分隔): php,linux 执行程序 php swoole_server.php 查看端口:netstat -antpl 连接服务器:telnet ip ...

  4. xBIM 基础09 WeXplorer 基本应用

    系列目录    [已更新最新开发文章,点击查看详细]  在本教程中,将学习如何创建最基本和最直接的查看器. 除了展示建筑模型外,不做其他任何操作.它将只使用内置导航,但是不会对按钮做出事件响应. &l ...

  5. Ubuntu16.04下Mongodb官网安装部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 在这篇博客里,我采用了非官网的安装步骤,来进行安装.走了弯路,同时,也是不建议.因为在大数据领域和实际生产里,还是要走正规的为好. Ubuntu16.04下Mongodb(离线安 ...

  6. Java7里try-with-resources分析--转

    原文地址:http://blog.csdn.net/hengyunabc/article/details/18459463 这个所谓的try-with-resources,是个语法糖.实际上就是自动调 ...

  7. mybatis使用注解替代xml配置,动态生成Sql

    mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...

  8. JavaScript学习——JS事件总结

    回顾之前已经使用过的事件 (onsubmit.onclick.onload.onfocus.onblur.onmouseover.onmouseout) onfocus/onblur:聚焦离焦事件,用 ...

  9. 在sql server数据库的一个表中如何查询共有多少字段

    select a.* from sys.columns a,sys.tables bwhere a.object_id = b.object_id and b.name = '要查的表名'

  10. HttpWebRequest WebExcepton: The remote server returned an error: (407) Proxy Authentication Required.

    1. Supply the credentials of the Currently Logged on User to the Proxy object similar to this: // Be ...