广告
返回顶部
首页 > 资讯 > 移动开发 >Android Tabhost使用方法详解
  • 453
分享到

Android Tabhost使用方法详解

方法tabhostAndroid 2022-06-06 08:06:09 453人浏览 八月长安
摘要

Android 实现tab视图有2种方法,一种是在布局页面中定义<tabhost>标签,另一种就是继承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>>(); 
 
@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,后面的步骤则和上面一种一样,就不在说明。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

您可能感兴趣的文章:Android组件TabHost实现页面中多个选项卡切换效果android TabHost(选项卡)的使用方法android 选项卡(TabHost)如何放置在屏幕的底部Android组件必学之TabHost使用方法详解Android控件之TabHost用法实例分析Android 使用FragmentTabhost代替Tabhost详解Android应用中使用TabHost组件进行布局的基本方法Android编程实现设置TabHost当中字体的方法详解Android TabHost的多种实现方法 附源码下载Android TabHost组件使用方法详解Android TabHost选项卡标签图标始终不出现的解决方法


--结束END--

本文标题: Android Tabhost使用方法详解

本文链接: https://www.lsjlt.com/news/24898.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作