Android TahHost的使用2 不继承TabActivity

简介:

Layout的设计,其实有我这里直接拖了一个TabHost控件,然后在每个Tab中放一个Button。

文件名为activity_tab_host.xml.

<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     xmlns:tools= "http://schemas.android.com/tools"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:paddingBottom= "@dimen/activity_vertical_margin"
     android:paddingLeft= "@dimen/activity_horizontal_margin"
     android:paddingRight= "@dimen/activity_horizontal_margin"
     android:paddingTop= "@dimen/activity_vertical_margin"
     tools:context= ".TabHostActivity"  >
 
     <TabHost
         android:id= "@+id/tabhost"
         android:layout_width= "match_parent"
         android:layout_height= "match_parent"
         android:layout_alignParentLeft= "true"
         android:layout_alignParentTop= "true"  >
 
         <LinearLayout
             android:layout_width= "match_parent"
             android:layout_height= "match_parent"
             android:orientation= "vertical"  >
 
             <TabWidget
                 android:id= "@android:id/tabs"
                 android:layout_width= "match_parent"
                 android:layout_height= "wrap_content"  >
             </TabWidget>
 
             <FrameLayout
                 android:id= "@android:id/tabcontent"
                 android:layout_width= "match_parent"
                 android:layout_height= "match_parent"  >
 
                 <LinearLayout
                     android:id= "@+id/tab1"
                     android:layout_width= "match_parent"
                     android:layout_height= "match_parent"  >
 
                     <Button
                         android:id= "@+id/button1"
                         android:layout_width= "wrap_content"
                         android:layout_height= "wrap_content"
                         android:text= "Fist Tab Content"  />
 
                 </LinearLayout>
 
                 <LinearLayout
                     android:id= "@+id/tab2"
                     android:layout_width= "match_parent"
                     android:layout_height= "match_parent"  >
                     <Button
                         android:id= "@+id/button1"
                         android:layout_width= "wrap_content"
                         android:layout_height= "wrap_content"
                         android:text= "Second Tab Content"  />
                 </LinearLayout>
 
                 <LinearLayout
                     android:id= "@+id/tab3"
                     android:layout_width= "match_parent"
                     android:layout_height= "match_parent"  >
                     <Button
                         android:id= "@+id/button1"
                         android:layout_width= "wrap_content"
                         android:layout_height= "wrap_content"
                         android:text= "Third Tab Content"  />
                 </LinearLayout>
             </FrameLayout>
         </LinearLayout>
     </TabHost>
 
</RelativeLayout>

 

需要注意的是TabHost必须包含一个 TabWidget和一个FrameLayout。而且TabWidget的id属性必须为 @android:id/tabs,FrameLayout的id属性必须为 @android:id/tabcontent。

下面TabHostActivity.java文件为:

public  class  TabHostActivity extends Activity {
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_tab_host);
         
         TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
         tabHost.setup();
         
         tabHost.addTab(tabHost.newTabSpec( "tab1" )
                 .setIndicator( "First Tab" )
                 .setContent(R.id.tab1));
         
         tabHost.addTab(tabHost.newTabSpec( "tab2" )
                 .setIndicator( "Second Tab" )
                 .setContent(R.id.tab2));
         
         tabHost.addTab(tabHost.newTabSpec( "tab3" )
                 .setIndicator( "Third Tab" )
                 .setContent(R.id.tab3));
     }
 
}

 效果图:

 


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


目录
相关文章
|
Java 数据安全/隐私保护 Android开发
Android C++系列:C++最佳实践3继承与访问控制
整个结构还是比较简单的,从类内部到本包到子类到外部包权限越来越小,比较好理解也比较好记忆。但是在C++中访问控制要复杂很多,因为不仅有属性和方法的访问控制,还有继承时的派生列表访问说明符。今天我们着重了解访问控制。
71 0
|
XML JSON Java
android 继承popupWindow实现时间、地址选择器
日期选择、地址选择,都是开发中少不了的功能,下面通过自定义的形式,同一套代码实现时间选择与地址选择,通过构造方法的不同来实现。
445 0
android 继承popupWindow实现时间、地址选择器
|
Java Android开发 图形学
Android修行手册之Kotlin-【Get和Set】、【继承】、【抽象类/嵌套类/内部类】篇
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣!!!
716 0
|
Android开发
Android中主要类的继承关系梳理汇总
版权声明:本文为sydMobile原创文章,转载请务必注明出处! https://blog.csdn.net/sydMobile/article/details/78904316 文章最早发布于我的微信公众号 Android_De_Home 中,欢迎大家扫描下面二维码关注微信公众及时获取更新和我交流互动。
1015 0
|
Android开发 开发工具 git
android build system中product的继承(inherit-product),加载(import-products)和选择(lunch)
一、前言 android源码中有很多product,进行配置时,会将源码中所有product的信息都读进来(不用的product的信息也会被读进来),其中每个product,可以包含如下信息 # # Functions for including ...
3017 0