tabhost使用

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("榜眼"));

}

}

时间: 2024-08-07 10:58:22

tabhost使用的相关文章

Android -- TabHost、Fragment、状态保存、通信

工程结构                                                                                       TabAFm到TabEFm都是Fragment,并且每个Fragment对应一个布局文件. TabAFm.java                                                                             package com.yydcdut.tabho

Android ——TabHost使用

在Android中,通常可以使用切换卡(选项卡)实现切换显示不同页面内容的功能.这一功能可以通过TabHost控件来实现. 下面我们就通过一个简单的实例演示如何使用TabHost控件完成切换卡功能,完成后的运行效果如图1所示. 图1 主页显示效果 可以看出,在该实例中,总共设置了四个TabHost标签,分别为主页.时间.联系人和搜索.在点击这些标签时,便可以完成相应页面内容的显示. 1.界面布局 TabHost是整个Tab的容器,是由TabWidget和FrameLayout 两部分组成的.其中

Android学习笔记(30):选项卡TabHost

TabHost组件是可以在界面中存放多个选项卡的容器,选项卡大小与外部容器相同. TabWidget 组件就是TabHost的标题条,单击可以切换选项卡. TabSpec组件就是一个选项卡(Tab页面). TabHost的使用步骤如下: 1.在XML文件中定义一个TabHost组件,并在其中定义一个FrameLayout.在其中添加内容. 2.Activity应该继承自TabActivity. 3.调用TabActivity的getTabHost()方法获取TabHost对象. 4.通过TabH

[转]Android学习笔记:TabHost 和 FragmentTabHost

TabHost 命名空间: android.widget.TabHost 初始化函数(必须在addTab之前调用): setup(); 包含两个子元素: 1.Tab标签容器TabWidget(@android:id/tabs) 2.Tab内容容器FrameLayout(@android:id/tabcontent) FragmentTabHost 命名空间: android.support.v4.app.FragmentTabHost android.support.v13.app.Fragme

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

viewPager是v4包里的一个组件,可以实现滑动显示多个界面. android也为viewPager提供了一个adapter,此adapter最少要重写4个方法: public int getCount() public boolean isViewFromObject(View view, Object o) public void destroyItem(ViewGroup container, int position, Object object)  public Object in

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

1 package com.myhost; 2 3 import android.os.Bundle; 4 import android.view.LayoutInflater; 5 import android.widget.TabHost; 6 import android.app.TabActivity; 7 8 public class TabhostActivity extends TabActivity { // Extend TabActivity class 9 10 /** C

Fragment + TabHost + RadioGroup

1. 使用FragMent是因为 4.0.3之后 ,摒弃了TabActivity这种用法, Demo 效果图: 先上布局XML R.layout.activity_main [html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"

创建TabHost的两种方式的简单分析

最近做了一个TabHost的界面,在做的过程中发现了一些问题,故和大家分享一下. 首先我的界面如下: 目前就我所知,创建TabHost有两种方式,第一种是继承TabActivity类,然后用getTabHost方法来得到一个TabHost的实例,然后就可以给这个TabHost添加Tab了.示例代码如下: [java] view plaincopy public class PlotHost extends TabActivity  { @Override protected void onCre

【原创】android——Tabhost 自定义tab+底部实现+intent切换内容

1,实现tabhost自定义格式,再此仅仅显示背景和文字,效果图预览:(底边栏所示) (图片变形) 2,xml配置 activity_user的XML配置  1 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/tabhost&qu

如何在Fragment中使用tabhost

最近在做一个仿电商的APP,由于前面使用了Fragment技术,现在想要在一个Fragment中做出TabHost的界面效果,经过查找资料找到了解决办法,特分享出来!(新人勿喷!) 首先要使用的控件是Support V4里面的控件,XML如图 <android.support.v4.app.FragmentTabHost         xmlns:android="http://schemas.android.com/apk/res/android"         andro