Tabhost用法

使用方法一:使用同一个布局文件

在xml中如此定义tabhost:

<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"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.hello.MainActivity" >

<TabHost

android:id="@+id/mytabhost"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true" >

<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" >

</TabWidget>

<FrameLayout

android:id="@android:id/tabcontent"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<LinearLayout

android:id="@+id/tab1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<TextView

android:id="@+id/text1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="hellllllll"></TextView>

</LinearLayout>

<LinearLayout

android:id="@+id/tab2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<TextView

android:id="@+id/text2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="hellllllll"></TextView>

</LinearLayout>

<LinearLayout

android:id="@+id/tab3"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<ImageView

android:id="@+id/img1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher"/>

</LinearLayout>

</FrameLayout>

</LinearLayout>

</TabHost>

</RelativeLayout>

对应的activity中这么写:

public class Tabs extends ActionBarActivity{

TabHost tabHost=null;

TabSpec spec=null;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.tabs);

tabHost=(TabHost)findViewById(R.id.mytabhost);

tabHost.setup();//这句必须写,不然崩溃

//实现标签、id、设置content,三者缺一不可,最后将其添加到tabhost

spec=tabHost.newTabSpec("tag1");

spec.setIndicator("课程");

spec.setContent(R.id.tab1);

tabHost.addTab(spec);

spec=tabHost.newTabSpec("tag2");

spec.setIndicator("论坛");

spec.setContent(R.id.tab2);

tabHost.addTab(spec);

spec=tabHost.newTabSpec("tag3");

spec.setIndicator("我的");

spec.setContent(R.id.tab3);

tabHost.addTab(spec);

}

}

也可以通过以下java代码创建一个新的tab

spec=tabHost.newTabSpec("tag4");

spec.setContent(new TabHost.TabContentFactory() {

@Override

public View createTabContent(String tag) {

// TODO Auto-generated method stub

TextView textView=new TextView(Tabs.this);

textView.setText("123456");

return textView;

}

});

spec.setIndicator("new");

tabHost.addTab(spec);

设置content的三种方式:

1)  使用布局文件

2)  用TabHost.TabContentFactory(如上面的java代码)

3)  用启动另一个布局的intent对象

以上三者均可以作为setContent的参数

设置indicator

可以是字符串、布局文件、图片。

注意:

1)  xml中FrameLayout中定义的布局,要都使用,如果不是用的话就会造成,未使用的布局和其他标签重合的现象。如下:

使用方法二:每个tab用不同的布局文件,使用LayoutInflater动态加载

Xml:

Tab1:

<LinearLayout 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"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.tabhost.MainActivity"

android:orientation="vertical"

android:id="@+id/tab1">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/app_name" />

</LinearLayout>

Tab2:

<LinearLayout 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"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.tabhost.MainActivity"

android:orientation="vertical"

android:id="@+id/tab2">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="第二个tab" />

</LinearLayout>

activity_main.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"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.tabhost.MainActivity" >

<TabHost

android:id="@+id/tabhost"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_marginTop="22dp" >

<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" >

</TabWidget>

<FrameLayout

android:id="@android:id/tabcontent"

android:layout_width="match_parent"

android:layout_height="match_parent" >

</FrameLayout>

</LinearLayout>

</TabHost>

</RelativeLayout>

Activity:

MainActivity:

package com.example.tabhost;

import android.support.v7.app.ActionBarActivity;

import android.text.Layout;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.ViewGroup;

import android.widget.TabHost;

public class MainActivity extends ActionBarActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TabHost tabHost=(TabHost)findViewById(R.id.tabhost);

tabHost.setup();

LayoutInflater inflater=LayoutInflater.from(this);

inflater.inflate(R.layout.tab1, tabHost.getTabContentView());//getTabContentView返回framelayout对应的view

inflater.inflate(R.layout.tab2, tabHost.getTabContentView());

tabHost.addTab(tabHost.newTabSpec("状元").setContent(R.id.tab1).setIndicator("状元"));

tabHost.addTab(tabHost.newTabSpec("榜眼").setContent(R.id.tab2).setIndicator("榜眼"));

}

}

使用方法三:继承TabActivity

Xml:

继承tabActivity后只需要把用到的各个tab布局写好,跟标签一般是FrameLayout也试过LinearLayout也可以

<FrameLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@android:id/tabcontent"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<LinearLayout

android:id="@+id/tab1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<TextView

android:id="@+id/text1"

android:layout_width="match_parent"

android:layout_height="match_parent"

          android:text="hellllllll"></TextView>

</LinearLayout>

<LinearLayout

android:id="@+id/tab2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<TextView

android:id="@+id/text2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="hemmmmmmm">

</TextView>

</LinearLayout>

<LinearLayout

android:id="@+id/tab3"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<ImageView

android:id="@+id/img1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher"/>

</LinearLayout>

</FrameLayout>

Activity:

package com.example.tab;

import android.support.v7.app.ActionBarActivity;

import android.app.TabActivity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.ImageView;

import android.widget.TabHost;

import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

TabHost tabHost=null;

TabSpec spec=null;

ImageView imageView=null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

tabHost=getTabHost();//和第一种方法不同在于直接用getTabHost获得tabhost

//且不用调用setup()函数

LayoutInflater.from(this).inflate(R.layout.activity_main, tabHost.getTabContentView());

spec=tabHost.newTabSpec("tag1");

spec.setIndicator("课程",getResources().getDrawable(R.drawable.picture));

spec.setContent(R.id.tab1);

tabHost.addTab(spec);

spec=tabHost.newTabSpec("tag2");

spec.setIndicator("论坛");

spec.setContent(R.id.tab2);

tabHost.addTab(spec);

spec=tabHost.newTabSpec("tag3");

spec.setIndicator("我的");

spec.setContent(R.id.tab3);

tabHost.addTab(spec);

imageView=(ImageView)findViewById(R.id.img1);

}

}

监听事件

标签切换监听:

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

@Override

public void onTabChanged(String tabId) {

// TODO Auto-generated method stub

//tabid对应于newTabSpec("tag2")设置的这个标签可

//用”tag2”.equals(tabid)判读按并处理

}

});

参考:http://blog.csdn.net/harvic880925/article/details/17120325

tabhost使用的更多相关文章

  1. android 使用Tabhost 发生could not create tab content because could not find view with id 错误

    使用Tabhost的时候经常报:could not create tab content because could not find view with id 错误. 总结一下发生错误的原因,一般的 ...

  2. Android 轮换页面+TabHost 实例

    最终效果展示: 首先我们需要一个ViewPager控件,不过可以发现在左侧的控件列表中并没有这个控件 这时我们要去升级包中查看 然后在厘米找到 ViewPager.class 这时我们双击这个发现不能 ...

  3. tabhost 下 setOnItemClickListener失效的问题

    分析了一下代码,应该是tabhost 的ontabchangedListener接管了下面应该由setOnItemClickListener接管的部分,导致不能相应setOnItemClickList ...

  4. 安卓初級教程(5):TabHost的思考

    package com.myhost; import android.os.Bundle; import android.view.LayoutInflater; import android.wid ...

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

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

  6. 安卓TabHost页面

    <?xml version="1.0" encoding="UTF-8"?> <!-- TabHost组件id值不可变--> <T ...

  7. Android TabHost使用

    TabHost是Android中自带的选项卡控件,效果图如下: 主布局文件 <RelativeLayout xmlns:android="http://schemas.android. ...

  8. tab使用 TabActivity TabHost Tabspec常用方法

    本文是参考Android官方提供的sample里面的ApiDemos的学习总结.   TabActivity   首先Android里面有个名为TabActivity来给我们方便使用.其中有以下可以关 ...

  9. Android工作学习第5天之TabHost实现菜单栏底部显示

    TabHost是一个装载选项卡窗口的容器,实现分模块显示的效果.像新浪微博客户端.微信客户端都是使用tabehost组件来开发的. TabHost的组成: |---TabWidget:实现标签栏,可供 ...

随机推荐

  1. Maven中的DependencyManagement和Dependencies

    Maven 使用dependencyManagement 元素来提供了一种管理依赖版本号的方式.通常会在一个组织或者项目的最顶层的父POM 中看到dependencyManagement 元素.使用p ...

  2. java substring

    str=str.substring(int beginIndex,int endIndex);中最终得到的值: beginIndex =< str的值 < endIndex

  3. 最近在做外贸网站的时候,需要大量的字体来充实页面,就学习了怎么引用Google Fonts

    第一步,FQ进入谷歌官方字体网站:https://fonts.google.com  妥妥的. 第二步,点击你所选择字体演示块的右上角的加号,然后你所选择的字体会形成引用链接以及你所要写的css样式. ...

  4. Android-小tips

    1.只保留float类型的一位小数,  String.format("%.1f", float值)   2.android  edittext 限制输入内容:  android:d ...

  5. Ubuntu 16.04 安装 .NET Core[转]

    upir@upir-Rev-1-0:~$ sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos ...

  6. ASP.NET MVC为字段设置多语言显示 [转]

    这段时间一直在忙.NET的项目,今天就写一下关于.NET的文章吧,也很长时间没写过.NET的啦  在使用ASP.NET MVC3 的时候,使用元数据模式可以非常方便地设置每个 字段(或者说属性)以减少 ...

  7. 罗永浩Vs王自如:浮躁的世界该如何降温?!

    这段时间智能手机业界有一件事情炒得纷纷扬扬,可谓是波澜起伏,想必大家都多少略有耳闻.昨天中午在群里看到“罗永浩舌战王自如视频”分享,就有些‘凑热闹’的好奇点开看看.一开场的几分钟正如老罗所说的那样:两 ...

  8. pthreads 2.0.10 test

    CentOS 6.3 cd /root mkdir pthreads //get php-5.6 and install zts version wget cn2.php.net/get/php-5. ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  10. java JVM

    1.ClassLoader(类加载器) 1.加载:查找并加载类的二进制数据 2.连接 —验证:确保被加载的类的正确性(防止不是通过java命令生成的class文件被加载) —准备:为类的静态变量分配内 ...