Android Tabhost使用方法详解

Android 实现tab视图有2种方法,一种是在布局页面中定义<tabhost>标签,另一种就是继承tabactivity.但是我比较喜欢第二种方式,应为如果页面比较复杂的话你的XML文件会写得比较庞大,用第二种方式XML页面相对要简洁得多。

下面是我的XML源码:

<FrameLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <ListView
   android:id="@+id/journals_list_one"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:cacheColorHint="#FFFFFFFF"
   android:scrollbars="vertical"
   android:paddingTop="5dip"
   android:paddingBottom="5dip"
   android:paddingRight="5dip"
   android:background="#FFFFFFFF"
   android:listSelector="@drawable/list_item_selecter"
   />
 <ListView
   android:id="@+id/journals_list_two"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:cacheColorHint="#FFFFFFFF"
   android:scrollbars="vertical"
   android:paddingTop="5dip"
   android:paddingBottom="5dip"
   android:paddingRight="5dip"
   android:background="#FFFFFFFF"
   />
 <ListView
   android:id="@+id/journals_list_three"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:cacheColorHint="#FFFFFFFF"
   android:scrollbars="vertical"
   android:paddingTop="5dip"
   android:paddingBottom="5dip"
   android:paddingRight="5dip"
   android:background="#FFFFFFFF"
   />
 <ListView
   android:id="@+id/journals_list_end"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:cacheColorHint="#FFFFFFFF"
   android:scrollbars="vertical"
   android:paddingTop="5dip"
   android:paddingBottom="5dip"
   android:paddingRight="5dip"
   android:background="#FFFFFFFF"
   />
</FrameLayout>

这是JAVA源码:

private TabHost tabHost;
private ListView listView;
private MyListAdapter adapter;
private View footerView;
private List<Map<String, String>> data = new ArrayList<Map<String, String>>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 tabHost = this.getTabHost(); 

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

 tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("",
   getResources().getDrawable(R.drawable.home)).setContent(
   R.id.journals_list_one));
 tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("",
   getResources().getDrawable(R.drawable.activity)).setContent(
   R.id.journals_list_two));
 tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("",
   getResources().getDrawable(R.drawable.community)).setContent(
   R.id.journals_list_three));
 tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("",
   getResources().getDrawable(R.drawable.shop)).setContent(
   R.id.journals_list_end)); 

 tabHost.setCurrentTab(0);
 setContentView(tabHost);
 tabHost.setOnTabChangedListener(tabChangeListener); 

 showContent(); 

} 

让自己的类继承TabActivity,然后通过调用getTabHost()方法得到tabhost对象,然后把自己写好的数据展示的布局文件加载到tabhost中,就可以实现了。最后是通过调用addTab()方法添加标签的相关属性(如:标签名称,标签图片,标签内容布局)。

而如果通过XML文件配置tabHost则需要注意的是,framelayout,tabwidge标签的id都必须引用系统的id(@android:id/tabcontent,@android:id/tabs),不然会报异常.在程序用使用findViewById()加载tabhost,然后调用tabhost.setup()方法初始化tabhost,后面的步骤则和上面一种一样,就不在说明。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • android TabHost(选项卡)的使用方法

    首先,定义TabHost的布局文件: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost" android:layout_width="fill_p

  • 详解Android TabHost的多种实现方法 附源码下载

    最近仔细研究了下TabHost,主要是为了实现微信底部导航栏的功能,最后也给出一个文章链接,大家不要着急 正文: TabHost的实现分为两种,一个是不继承TabActivity,一个是继承自TabActivity:当然了选用继承自TabActivity的话就相对容易一些,下面来看看分别是怎样来实现的吧. 方法一.定义tabhost:不用继承TabActivity  1.布局文件:activity_main.xml <LinearLayout xmlns:android="http://s

  • Android程序开发之自定义设置TabHost,TabWidget样式

    先看效果: 京东商城底部菜单栏 新浪微博底部菜单栏 本次学习效果图: 第一,主布局文件(启动页main.xml,位于res/layout目录下)代码 <?xml version="." encoding="utf-"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_paren

  • Android入门之TabHost与TabWidget实例解析

    本文实例介绍的是Android的Tab控件,Tab控件可以达到分页的效果,让一个屏幕的内容尽量丰富,当然也会增加开发的复杂程度,在有必要的时候再使用.Android的Tab控件使用起来有点奇怪,必须包含和按照以下的顺序: TabHost控件->TabWidget(必须命名为tabs)->FrameLayout(必须命名为tabcontent). 先来贴出本例运行的截图: main.xml的源码如下: <?xml version="1.0" encoding="

  • Android组件TabHost实现页面中多个选项卡切换效果

    TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计. 一.基础知识 TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡; TabSpec : 代表了选项卡界面, 添加一个TabSpec即可添加到TabHost中; -- 创建选项卡 : newTabSpec(String tag), 创建一个选项卡; -- 添加选项卡 : addTab(tabSpec); 二.实例讲解 TabHost的基本使用,主要是layout的

  • Android中使用TabHost 与 Fragment 制作页面切换效果

    三个标签页置于顶端 效果图: 在文件BoardTabHost.java中定义页面切换的效果:切换页面时,当前页面滑出,目标页面滑入.这是2个不同的动画设定动画时要区分对待 import android.content.Context; import android.util.AttributeSet; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import

  • Android组件必学之TabHost使用方法详解

    一.TabHost用法 通常情况下我们会通过继承TabActivity,调用getTabHost()获取TabHost实例,下面是具体过程. TabHostActivity.java public class TabHostActivity extends TabActivity { private TabHost tabHost; private Intent certificateIntent; private Intent feeIntent; private Intent scoreIn

  • Android控件之TabHost用法实例分析

    本文实例讲述了Android控件之TabHost用法.分享给大家供大家参考.具体如下: 以下通过TabHost实现android选项卡. main.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=

  • android 选项卡(TabHost)如何放置在屏幕的底部

    今天写Tab的时候由于TAB的跳转问题去查资料,倒反而发现更有趣的问题,就是如何将TAB放置在屏幕的底端. 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" a

  • android中TabHost的图标(48×48)和文字叠加解决方法

    开发过程中,有时候图标稍微大点,比如48×48的时候,文字就会和图标叠加起来,解决方法如下: 复制代码 代码如下: TabWidget tw = tabHost.getTabWidget(); for (int i = 0; i < tw.getChildCount(); i++) {     TextView tv=(TextView)tw.getChildAt(i).findViewById(android.R.id.title);     ImageView iv=(ImageView)t

随机推荐