• Android TabHost的使用


      android 实现tab视图有2种方法,一种是在布局页面中定义<tabhost>标签,另一种就是继承tabactivity.但是我比较喜欢第二种方式,应为如果页面比较复杂的话你的XML文件会写得比较庞大,用第二种方式XML页面相对要简洁得多。

    Tab标签页的使用
      首先要设计所有的分页的界面布局
      在分页设计完成后,使用代码建立Tab标签页,并给每个分页添加标识和标题
      最后确定每个分页所显示的界面布局
     
    建立一个“TabDemo”程序,包含三个XML文件,分别为tab1.xml、tab2.xml和tab3.xml,这3个文件分别使用线性布局、相对布局和绝对布局示例中的main.xml的代码,并将布局的ID分别定义为layout01、layout02和layout03
    tab1.xml文件代码   
                           <?xml version="1.0" encoding="utf-8"?>
                           <LinearLayout android:id = "@+id/layout01"
                            ……
                            ……
                          </LinearLayout>
     
    tab2.xml文件代码
            <?xml version="1.0" encoding="utf-8"?>
            <AbsoluteLayout android:id="@+id/layout02"
              ……
              ……
            </AbsoluteLayout>
     
    tab3.xml文件代码                       
            <?xml version="1.0" encoding="utf-8"?>
            <RelativeLayout android:id="@+id/layout03"
             ……
              ……
            </RelativeLayout>
     
    import android.app.TabActivity;
    import android.os.Bundle;
    import android.widget.TabHost;
    import android.view.LayoutInflater;
     
    public class TabDemo extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);       
      TabHost tabHost = getTabHost();
       LayoutInflater.from(this).inflate(R.layout.tab1, tabHost.getTabContentView(),true);   //将布局文件与TabHost关联在一起
       LayoutInflater.from(this).inflate(R.layout.tab2, tabHost.getTabContentView(),true);
       LayoutInflater.from(this).inflate(R.layout.tab3, tabHost.getTabContentView(),true);
     
     
       tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("线性布局").setContent(R.id.layout01));  //setContent()指定每个Tab包含的View
       tabHost.addTab(tabHost.newTabSpec("TAB2").setIndicator("绝对布局").setContent(R.id.layout02));
       tabHost.addTab(tabHost.newTabSpec("TAB3").setIndicator("相对布局").setContent(R.id.layout03));
                }
     }
     
    8行代码的声明TabDemo类继承与TabActivity,与以往继承Activity不同,TabActivity支持内嵌多个ActivityView
    12行代码通过getTabHost()函数获得了Tab标签页的容器,用以承载可以点击的Tab标签和分页的界面布局。
    13行代码通过LayoutInflatertab1.xml文件中的布局转换为Tab标签页可以使用的View对象
    16行代码使用addTab()函数添加了第1个分页,tabHost.newTabSpec("TAB1")表明在第12行代码中建立的tabHost上,添加一个标识为TAB1Tab分页
    17行代码使用setIndicator()函数设定分页显示的标题,使用setContent()函数设定分页所关联的界面布局
     

     第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    3.     android:id="@+id/hometabs"  
    4.     android:orientation="vertical"  
    5.     android:layout_width="fill_parent"    
    6.     android:layout_height="fill_parent">   
    7.     <TabHost android:id="@+id/tabhost"         //继承自FrameLayout,下面只能显示一个节点,TabWidget 和FrameLayout 放在LinearLayout  下面
    8.          android:layout_width="fill_parent"  
    9.          android:layout_height="wrap_content">  
    10.          <LinearLayout  
    11.             android:orientation="vertical"  
    12.             android:layout_width="fill_parent"  
    13.             android:layout_height="fill_parent">  
    14.               
    15.              <TabWidget android:id="@android:id/tabs"   
    16.               android:orientation="horizontal"  
    17.               android:layout_width="fill_parent"  
    18.               android:layout_height="wrap_content">  
    19.             </TabWidget>  
    20.            
    21.              <FrameLayout android:id="@android:id/tabcontent"  
    22.                   android:layout_width="wrap_content"  
    23.                   android:layout_height="wrap_content">  
      1. <LinearLayout android:id="@+id/ll01" android:layout_width="fill_parent"  
      2.         android:layout_height="fill_parent" android:gravity="center_horizontal"  
      3.         android:orientation="vertical">  
      4.         <EditText android:id="@+id/widget34" android:layout_width="fill_parent"  
      5.             android:layout_height="wrap_content" android:text="EditText"  
      6.             android:textSize="18sp">  
      7.         </EditText>  
      8.         <Button android:id="@+id/widget30" android:layout_width="wrap_content"  
      9.             android:layout_height="wrap_content" android:text="Button">  
      10.         </Button>  
      11.     </LinearLayout>  
      12.     <LinearLayout android:id="@+id/ll02" android:layout_width="fill_parent"  
      13.         android:layout_height="fill_parent" android:gravity="center_horizontal"  
      14.         android:orientation="vertical">  
      15.         <AnalogClock android:id="@+id/widget36"  
      16.             android:layout_width="wrap_content" android:layout_height="wrap_content">  
      17.         </AnalogClock>  
      18.     </LinearLayout>  
      19.     <LinearLayout android:id="@+id/ll03" android:layout_width="fill_parent"  
      20.         android:layout_height="fill_parent" android:gravity="center_horizontal"  
      21.         android:orientation="vertical">  
      22.         <RadioGroup android:id="@+id/widget43"  
      23.             android:layout_width="166px" android:layout_height="98px"  
      24.             android:orientation="vertical">  
      25.             <RadioButton android:id="@+id/widget44"  
      26.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
      27.                 android:text="RadioButton">  
      28.             </RadioButton>  
      29.             <RadioButton android:id="@+id/widget45"  
      30.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
      31.                 android:text="RadioButton">  
      32.             </RadioButton>  
      33.         </RadioGroup>  
      34.     </LinearLayout> 
      35.  
    24.              </FrameLayout>  
    25.            
    26.          </LinearLayout>  
    27.     </TabHost>  
    28. </LinearLayout>  

     

    1. TabHost tabHost = (TabHost) findViewById(R.id.tabhost);  
    2.         tabHost.setup();                                     //完成初始化
    3.         TabWidget tabWidget = tabHost.getTabWidget();  
    4.         tabHost.addTab(tabHost.newTabSpec("tab1")  .setIndicator("tab1",  getResources().getDrawable(R.drawable.img01))  .setContent(R.id.1101));  
    5.         tabHost.addTab(tabHost   .newTabSpec("tab2").setIndicator("tab2",   getResources().getDrawable(R.drawable.img02))  
    6.                                   .setContent(R.id.1102));  
    7.         tabHost.addTab(tabHost   .newTabSpec("tab3").setIndicator("tab3",   getResources().getDrawable(R.drawable.img03))  
    8.                                    .setContent(R.id.1103));  

     

  • 相关阅读:
    Magento入门开发教程
    Magento文件系统目录结构
    正则匹配获取HTML图片地址,正则匹配获取HTML内容
    php+phpspreadsheet读取Excel数据存入mysql
    PHP遍历一个文件夹下所有文件和子文件夹的函数
    前端实现在线预览pdf、word、xls、ppt等文件
    微服务架构的基础框架选择
    Linux性能优化实战学习笔记:第五十八讲
    Linux性能优化实战学习笔记:第五十六讲
    Linux性能优化实战学习笔记:第五十五讲
  • 原文地址:https://www.cnblogs.com/renyuan/p/2588740.html
Copyright © 2020-2023  润新知