`

动态设置 TabHost选项卡属性及自定义TabHost选项Layout

 
阅读更多

TabHost生成后会向内添加TabSpec以形成选项栏:

如果是继承的TabActivity类:

  1. tabhost = getTabHost();  
  2. tabhost.addTab(tabhost.newTabSpec("1" ).setIndicator( "TAB 1" , getResources().getDrawable(R.drawable.icon)).setContent(R.id.text1));   
  3. tabhost.setCurrentTab(0 );  


如果是直接继承Activity类:

  1. tabhost = (TabHost) findViewById(R.id.tabhost);  
  2. tabhost.setup();  
  3. tabhost.addTab(tabhost.newTabSpec("1" ).setIndicator( "TAB 1" , getResources().getDrawable(R.drawable.icon)).setContent(R.id.text1));   
  4. tabhost.setCurrentTab(0 );  

注意加.setup(),否则会有NullPointer的异常


main.xml:

  1. < TabHost   android:id = "@+id/tabhost"   android:layout_width = "fill_parent"   android:layout_height = "wrap_content" >   
  2.     < LinearLayout   android:orientation = "vertical"   android:layout_width = "fill_parent"   android:layout_height = "fill_parent" >   
  3.         < TabWidget   android:id = "@android:id/tabs"   android:layout_width = "fill_parent"   android:layout_height = "fill_parent"   />   
  4.         < FrameLayout   android:id = "@android:id/tabcontent"   android:layout_width = "fill_parent"   android:layout_height = "fill_parent" >   
  5.             < TextView   android:id = "@+id/text1"   android:layout_width = "wrap_content"   android:layout_height = "wrap_content"   />   
  6.         </ FrameLayout >   
  7.     </ LinearLayout >   
  8. </ TabHost >    

注意:TabHost标签下的TabWidget和FrameLayout的id 是固定的,分别为"tabs"和"tabcontent"


---------------------------------------------------------------------------------- 分割线 ---------------------------------------------------------------------------------------------


动态更改TabHost的选项卡头显示,有两种方法:一种比较笨而且可控性差(为 什么还要讲一会再说),一种比较简单而且可控性高,我们先说简单的那一种:

TabHost本身不提供有效的函数支持对选项卡头的动态更改,但是我们可以通过 TabHost的getTabWidget()函数得到选项卡头的View从而"曲线"更改选项卡头的属性:

 

  1. ImageView iv = (ImageView) tabhost.getTabWidget().getChildAt(targetLocation).findViewById(android.R.id.icon);  
  2. TextView tv = (TextView) tabhost.getTabWidget().getChildAt(targetLocation).findViewById(android.R.id.title);  
  3. tv.setText("XXXX" );  
  4. iv.setImageDrawable(getResources().getDrawable(R.drawable.icon));             

其中获得的ImageView即是TabHost的选项卡 头在targetLocation位置的图片的View,TextView是对应的文字的View,这样就可以对它们进行任意的改动了,注意 targetLocation从0开始

还有一种比较笨的方法:

就是自定义TabHost选项卡头的Layout,如下:

 

  1. tabhost = getTabHost();  
  2. RelativeLayout tempLayout = (RelativeLayout) LayoutInflater.from(this ).inflate(R.layout.mytab,  null );  
  3. TextView targetTabLabel = (TextView) tempLayout.findViewById(R.id.tab_text);  
  4. targetTabLabel.setText("TAB 1" );  
  5. tempLayout.removeAllViews();  
  6. tabhost.addTab(tabhost.newTabSpec("1" ).setIndicator(targetTabLabel).setContent(R.id.text1));  

注意要对tempLayout使用removeAllViews(),否则在将 targetTabLabel加入TabHost中时会出现java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.的异常


其中R.layout.mytab来自mytab.xml:

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < RelativeLayout   xmlns:android = "http://schemas.android.com/apk/res/android"     
  3.     android:layout_width = "wrap_content"   
  4.     android:layout_height = "wrap_content" >   
  5. < TextView   android:id = "@+id/tab_text"     
  6.         android:layout_width = "wrap_content"   
  7.         android:layout_height = "wrap_content"   
  8.         android:gravity = "center"   
  9.         android:textStyle = "bold"    
  10.         android:background = "@drawable/selector_img" />   
  11. </ RelativeLayout >   


其中android:text="@drawable/selector_img"指向 selector_img.xml:

 

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < selector   xmlns:android = "http://schemas.android.com/apk/res/android" >   
  3. < item   android:state_selected = "false"   
  4.       android:drawable = "@drawable/img1" >   
  5. </ item >   
  6. < item   android:state_selected = "true"   
  7.       android:drawable = "@drawable/img2" >   
  8. </ item >   
  9. </ selector >   


点击时使用Selector置换背景图片,文字的更改尚未用此方法验证,这种方法非常繁琐而且可控性太差,只能响应选择事件,且只能更改2个选项,但是之 所以讲解这种方法,是因为这里顺便讲解了如何自定义TabHost选项卡头的Layout,如果你想在UI上做出更多的花样,这个技巧是很有帮助的。

分享到:
评论

相关推荐

    Android开发之TabHost选项卡及相关疑难解决方法

    本文实例分析了Android开发之TabHost选项卡及相关疑难解决方法。分享给大家供大家参考,具体如下: 前言: 虽然现在谷歌已经不推荐使用TabHost,但是初学者还是很有必要接触下这一成金的经典的,本文将介绍纤细介绍...

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

    TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计。 一、基础知识 TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡; TabSpec : 代表了选项卡界面, 添加...

    Android 创建手机界面中的选项卡切换效果.rar

    Android实现手机界面中的TAB,也就是大家熟悉的选项卡效果,可以单击或轻触选项卡,切换主体部分的内容,目前在各种手机应用软件中已得到普遍使用,这是一个创建基本TAB选项卡的例子,使用了TabHost实现,运行效果如...

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

    首先,定义TabHost的布局文件: 代码如下:&lt;?xml version=”1.0″ encoding=”utf-8″?&gt;&lt;TabHost xmlns:android=”http://schemas.android.com/apk/res/android” android:id android:layout_width=”fill_...

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

    &lt;TabHost xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@android:id/tabhost” android:layout_width=”fill_parent” android:layout_height=”fill_parent”&gt; &lt;LinearLayout ...

    Android控件之TabHost用法实例分析

    以下通过TabHost实现android选项卡。 main.xml布局文件: &lt;?xml version=1.0 encoding=utf-8?&gt; android:layout_width=fill_parent android:layout_height=fill_parent&gt; &lt;LinearLayout android:id=@+i

    详解Android应用中使用TabHost组件进行布局的基本方法

    TabHost布局文件 我们先来了解一下布局文件的基本内容: 1. 根标签及id 设置Android自带id : XML布局文件中, 可以使用 标签设置...tabhost android:id=”@android:id/tabhost” android:layout_height=”match_parent”

    Android开发之选项卡功能的实现方法示例

    选项卡(TabHost)方便的在窗口上设置多个标签页,每个标签页相当于获得一个与外部容器相同大小的组件摆放区域 通过这种方式,可以在一个容器中放置多组件。 创建4个java文件并对应layout 创建主java ,代码 ...

    Android实现底部导航栏功能(选项卡)

    现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能。 我们先看下该demo实例的框架图: 其中各个类的作用以及...

    Android编程实现在底端显示选项卡的方法

    本文实例讲述了Android编程实现在底端显示选项卡的方法。分享给大家供大家参考,具体如下: 1.layout 文件 &lt;?xml version=1.0 encoding=utf-8 ?&gt; android:orientation android:layout_width=fill_parent ...

    Android-实验报告-Sqlite-数据库操作.docx

    /*添加第一个选项卡*/ TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1"); tab1.setIndicator("login"); tab1.setContent(new Intent(this,LoginActivity.class)); tabHost.addTab(tab1); /*添加第二个选项卡*/ ...

    Android编程实现将tab选项卡放在屏幕底部的方法

    本文实例讲述了Android编程实现将tab选项卡放在屏幕底部的方法。分享给大家供大家参考,具体如下: 今天写Tab的时候由于TAB的跳转问题去查资料,倒反而发现更有趣的问题,就是如何将TAB放置在屏幕的底端。有点类似...

    疯狂Android讲义源码

     2.4.7 选项卡(TabHost)的功能和  用法 93  2.4.8 滚动视图(ScrollView)的  功能和用法 95  2.4.9 列表视图(ListView和  ListActivity) 95  2.4.10 可展开的列表组件(ExpandableListView) 101  ...

    Android简单实现屏幕下方Tab菜单的方法

    看到很多热门的Android程序(如:新浪微博、腾讯微博、京东商城、淘宝、当当等等)使用选项卡风格作为程序界面的主框架结构,而Android的选项卡控件默认是按钮在上方的。我在网上看到有多种实现方法,这里提供一种...

    疯狂Android讲义.part2

    2.4.7 选项卡(TabHost)的功能和 用法 93 2.4.8 滚动视图(ScrollView) 的 功能和用法 95 2.4.9 列表视图(ListView和 ListActivity) 95 2.4.10 可展开的列表组件(ExpandableListView) 101 2.4.11 网格视图...

    疯狂Android讲义.part1

    2.4.7 选项卡(TabHost)的功能和 用法 93 2.4.8 滚动视图(ScrollView) 的 功能和用法 95 2.4.9 列表视图(ListView和 ListActivity) 95 2.4.10 可展开的列表组件(ExpandableListView) 101 2.4.11 网格视图...

Global site tag (gtag.js) - Google Analytics