android tabHost布局之一 继承TabActivity并以布局文件进行布局

http://blog.csdn.net/chenzheng_java/article/details/6207839

上面的是最终效果图。

代码结构如下。

 

main.xml代码:

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 该布局文件定义了标签的内容部分,该布局文件一定要以FrameLayout为根元素 -->  
  3. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent" android:layout_height="fill_parent">  
  5.     <!-- 第一个标签内容 -->  
  6.     <LinearLayout android:id="@+id/widget_layout_Blue"  
  7.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  8.         android:orientation="vertical" >  
  9.         <EditText android:id="@+id/widget34" android:layout_width="fill_parent"  
  10.             android:layout_height="wrap_content" android:text="EditText"  
  11.             android:textSize="18sp">  
  12.         </EditText>  
  13.         <Button android:id="@+id/widget30" android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content" android:text="Button">  
  15.         </Button>  
  16.     </LinearLayout>  
  17.     <!-- 第二个标签内容 AnalogClock为钟表组件-->  
  18.     <LinearLayout android:id="@+id/widget_layout_red"  
  19.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  20.         android:orientation="vertical"  >  
  21.         <AnalogClock android:id="@+id/widget36"  
  22.             android:layout_width="wrap_content" android:layout_height="wrap_content">  
  23.         </AnalogClock>  
  24.     </LinearLayout>  
  25.     <!-- 第三个标签内容 RadioButton必须在RadioGroup中哦 -->  
  26.     <LinearLayout android:id="@+id/widget_layout_green"  
  27.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  28.         android:orientation="vertical">  
  29.         <RadioGroup android:id="@+id/widget43"  
  30.             android:layout_width="166px" android:layout_height="98px"  
  31.             android:orientation="vertical">  
  32.             <RadioButton android:id="@+id/widget44"  
  33.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  34.                 android:text="RadioButton">  
  35.             </RadioButton>  
  36.             <RadioButton android:id="@+id/widget45"  
  37.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  38.                 android:text="RadioButton">  
  39.             </RadioButton>  
  40.         </RadioGroup>  
  41.     </LinearLayout>  
  42. </FrameLayout>  

TagHostTest.java的代码:

[java]  view plain copy
  1. package cn.com.tagHost.test;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.ViewGroup;  
  8. import android.widget.TabHost;  
  9.   
  10. public class TagHostTest extends TabActivity {  
  11.   
  12.     private TabHost myTabhost;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         myTabhost = this.getTabHost();  
  18.   
  19.         /** 
  20.          * inflate(int resource, ViewGroup root, boolean attachToRoot) 
  21.          * resource 很显然是一个资源索引id 
  22.          * 当attachToRoot为true时,root代表一个可放置于容器中的组件 
  23.          * 当attachToRoot为false时,root仅代表一个存储值的对象 
  24.          * 该方法的意思是,将根据R.layout.main生成的标签View,添加到由myTabhost.getTabContentView()获得的父容器中 
  25.          * LayoutInflater类的inflate方法中有如下片段 
  26.          *  if (root != null && attachToRoot) { 
  27.                         root.addView(temp, params); 
  28.                     } 
  29.             其中temp是根据resource指定的资源生成的一个和标签有关的view 
  30.          */  
  31.         LayoutInflater.from(this).inflate(R.layout.main,  
  32.                 myTabhost.getTabContentView(), true);  
  33.         myTabhost.setBackgroundColor(Color.argb(1502270150));  
  34.   
  35.         myTabhost.addTab(myTabhost.newTabSpec("One")  
  36.                 .setIndicator("A").setContent(R.id.widget_layout_Blue));  
  37.   
  38.         myTabhost.addTab(myTabhost.newTabSpec("Two")  
  39.                 .setIndicator("B", getResources().getDrawable(R.drawable.icon))  
  40.                 .setContent(R.id.widget_layout_green));  
  41.   
  42.         myTabhost.addTab(myTabhost.newTabSpec("Three")  
  43.                 .setIndicator("C", getResources().getDrawable(R.drawable.icon))  
  44.                 .setContent(R.id.widget_layout_red));  
  45.     }  
  46. }  

 

这种方法实现起来比较简单,看看我们都做了些什么。

第一步:定义标签内容部分的布局文件,该布局文件必须以FrameLayout为根节点。

第二步:让activity继承TabActivity,然后实现自己的代码。

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值