Android TabHost的使用(Tab为Layout)

本文介绍如何使用AndroidTabHost实现选项卡界面,通过继承TabActivity并定制american.xml、chinese.xml和japanese.xml布局文件,创建对应的Activity来展示不同内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android TabHost的使用,这里采用继承TabActivity的方法。

这里分别定制三个Tab,分别为american.xml, chinese.xml, japanese.xml三个Layout。

american.xml文件

<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:orientation= "vertical"  >
 
     <Button
         android:id= "@+id/button1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "American1"  />
 
     <Button
         android:id= "@+id/button2"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "American2"  />
 
</LinearLayout>

 chinese.xml文件

<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:orientation= "vertical"  >
         <Button
         android:id= "@+id/button1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "Chinese button 1"  />
 
     <Button
         android:id= "@+id/button2"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "Chinese button 2"  />
 
</LinearLayout>

  japanese.xml文件

<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:orientation= "vertical"  >
     
         <Button
         android:id= "@+id/button1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "Japanese button 1"  />
 
         <Button
             android:id= "@+id/button2"
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:text= "Japanese button 2"  />
 
</LinearLayout>

 

三个Layout对应的java文件为AmericanActivity.java, ChinaActivity.java, JapanActivity.java

AmericanActivity.java文件

public  class  AmericanActivity extends Activity {
 
      @Override
         protected  void  onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.american);
          
         }
}

  ChinaActivity.java文件

public  class  ChinaActivity extends Activity {
 
      @Override
         protected  void  onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.chinese);
            
         }
}

  JapanActivity.java文件

public  class  JapanActivity extends Activity {
 
      @Override
         protected  void  onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.japanese);
           
         }
}

  

MainActivity.java 继承TabActivity。

public  class  MainActivity extends TabActivity {
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         //setContentView(R.layout.activity_main);
         Resources resources = getResources();
         TabHost tabHost = getTabHost();
         
         TabHost.TabSpec spec;
         
         Intent intent = new  Intent( this ,AmericanActivity. class  );
         spec = tabHost.newTabSpec( "American" );
         spec.setIndicator( "Ameican Tab" );
         spec.setContent(intent);
         tabHost.addTab(spec);
         
         Intent intent2 = new  Intent( this ,ChinaActivity. class  );
         spec = tabHost.newTabSpec( "China" );
         spec.setIndicator( "China Tab" )
         spec.setContent(intent2);
         tabHost.addTab(spec);
         
         Intent intent3 = new  Intent( this ,JapanActivity. class  );
         spec = tabHost.newTabSpec( "Japanese" );
         spec.setIndicator( "Japanese Tab" );
         spec.setContent(intent3);
         tabHost.addTab(spec);
         
         tabHost.setCurrentTab(1);
     }
 
     
}

  注意:在AndroidManifest文件中加入下面三行代码。

<activity android:name= "com.example.app1.AmericanActivity"  android:label= "@string/app_name" ></activity>
<activity android:name= "com.example.app1.JapanActivity"  android:label= "@string/app_name" ></activity>
<activity android:name= "com.example.app1.ChinaActivity"  android:label= "@string/app_name" ></activity>

 效果图:



本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2013/03/16/2963592.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值