欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Tabhost使用方法详解

程序员文章站 2024-03-01 23:22:16
android 实现tab视图有2种方法,一种是在布局页面中定义标签,另一种就是继承tabactivity.但是我比较喜欢第二种方式,应为如果...

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,后面的步骤则和上面一种一样,就不在说明。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇: MySql查询时间段的方法

下一篇: