当前位置: 首页 > 编程笔记 >

Android Tabhost使用方法详解

嵇丰
2023-03-14
本文向大家介绍Android Tabhost使用方法详解,包括了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,后面的步骤则和上面一种一样,就不在说明。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍FragmentTabHost使用方法详解,包括了FragmentTabHost使用方法详解的使用技巧和注意事项,需要的朋友参考一下 FragmentTabHost是support-v包下提供的用于集成和管理Fragment页面的组件. 今天要实现的效果图如下: 整体结构是MainActivity+5个模块的Fragment. MainActivity的布局如下: 每个tab的布局如

  • 本文向大家介绍ToolBar使用方法详解,包括了ToolBar使用方法详解的使用技巧和注意事项,需要的朋友参考一下 ToolBar的出现是为了替换之前的ActionBar的各种不灵活使用方式,相反,ToolBar的使用变得非常灵活,因为它可以让我们自由往里面添加子控件.低版本要使用的话,可以添加support-v7包. 今天要实现的效果如下: 由上图可以看到,toolBar的布局还是相对丰富的.要

  • 本文向大家介绍TabLayout使用方法详解,包括了TabLayout使用方法详解的使用技巧和注意事项,需要的朋友参考一下 TabLayout是design库提供的控件,可以方便的使用指示器,功能类似ViewPagerIndicator. 使用非常方便,Android Studio只需要在gradle中引入即可使用 . TabLayout即可以单独使用,也可以配合ViewPager来使用. 先来看

  • 本文向大家介绍jQuery:unbind方法的使用详解,包括了jQuery:unbind方法的使用详解的使用技巧和注意事项,需要的朋友参考一下 jQuery:unbind方法的使用详解 一、前言 unbind方法只能解绑用jQuery的bind方法以及用jquery方法注册的事件处理程序。比如:$(‘a').click(function(){})可以通过unbind解绑。用原生addEventLi

  • 本文向大家介绍Java HttpURLConnection使用方法详解,包括了Java HttpURLConnection使用方法详解的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Java HttpURLConnection使用,供大家参考,具体内容如下 包括使用HttpURLConnection执行get/post请求 GitHub:https://github.com/taz3

  • 本文向大家介绍php使用PDO方法详解,包括了php使用PDO方法详解的使用技巧和注意事项,需要的朋友参考一下 本文详细分析了php使用PDO方法。分享给大家供大家参考。具体分析如下: PDO::exec:返回的是int类型,表示影响结果的条数. 返回的是boolean型,true表示执行成功,false表示执行失败,这两个通常出现在如下代码: 一般情况下可以用$rs0的值判断SQL执行成功与否,