android底部选项卡(一) TableHost

如上图:在开发中我们会经常遇到使用底部选项卡实现不同界面的切换,实现方法是多种多样:
(1).TableHost
(2).RadioGroup 、RadioButton
(3).自定义布局 实现原理和RadioButton一致
(4).FragmentTabHost

下面附上代码:演示了TableHost的实现,但是应该注意,TableHost目前已过时,不推荐使用
 


1.定义布局文件

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <!--TabHost布局定义:需要使用指定id="@android:id/tabhost"-->  
  8.     <TabHost  
  9.         android:id="@android:id/tabhost"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent" >  
  12.         <TabWidget  
  13.             android:id="@android:id/tabs"  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="60px"  
  16.             android:layout_gravity="bottom" >  
  17.         </TabWidget>  
  18.         <FrameLayout  
  19.             android:id="@android:id/tabcontent"  
  20.             android:layout_width="match_parent"  
  21.             android:layout_height="match_parent"  
  22.             android:layout_marginBottom="60px" >  
  23.         </FrameLayout>  
  24.     </TabHost>  
  25.   
  26. </RelativeLayout>  


2.要在TabHost中显示的两个Activity的布局没有再次写出,其中的内容完全由自己设置

 

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
    1. package com.example.android_tablehost;  
    2.   
    3. import android.app.TabActivity;  
    4. import android.content.Intent;  
    5. import android.os.Bundle;  
    6. import android.view.LayoutInflater;  
    7. import android.view.View;  
    8. import android.view.Window;  
    9. import android.widget.ImageView;  
    10. import android.widget.TabHost;  
    11. import android.widget.TabHost.TabSpec;  
    12. import android.widget.TextView;  
    13.   
    14. public class MainActivity extends TabActivity {  
    15.     private TabHost mTabHost;  
    16.     private LayoutInflater inflater;  
    17.   
    18.     @SuppressWarnings("deprecation")  
    19.     @Override  
    20.     protected void onCreate(Bundle savedInstanceState) {  
    21.         super.onCreate(savedInstanceState);  
    22.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
    23.         setContentView(R.layout.activity_main);  
    24.         inflater = LayoutInflater.from(this);  
    25.         // 直接调用TabActivity.getTabHost()方法获得xml文件中的TabHost对象  
    26.         mTabHost = getTabHost();  
    27.         // 添加底部标签  
    28.         mTabHost.addTab(getFirstTab());  
    29.         mTabHost.addTab(getSecondTab());  
    30.         mTabHost.setCurrentTabByTag("first");// 设置默认选中  
    31.     }  
    32.   
    33.     /** 
    34.      * 内容1 
    35.      *  
    36.      * @return 
    37.      */  
    38.     private TabSpec getFirstTab() {  
    39.         // 添加AG  
    40.         TabSpec spec = mTabHost.newTabSpec("first");  
    41.         // 指定标签显示的内容 , 激活的activity对应的intent对象  
    42.         Intent intent = new Intent(this, FirsActivity.class);  
    43.         spec.setContent(intent);  
    44.         // 设置标签的文字和样式  
    45.         spec.setIndicator(getIndicatorView("标签一", R.drawable.tab_main_nav_me));  
    46.         return spec;  
    47.     }  
    48.   
    49.     /** 
    50.      * 内容2 
    51.      *  
    52.      * @return 
    53.      */  
    54.     private TabSpec getSecondTab() {  
    55.         // 为标签指定tag  
    56.         TabSpec spec = mTabHost.newTabSpec("second");  
    57.         // 指定标签显示的内容 , 激活的activity对应的intent对象  
    58.         Intent intent = new Intent(this, SecondActivity.class);  
    59.         spec.setContent(intent);  
    60.         // 设置标签的文字和样式  
    61.         spec.setIndicator(getIndicatorView("标签2", R.drawable.tab_main_nav_book));  
    62.         return spec;  
    63.     }  
    64.   
    65.     /** 
    66.      * 设置底部选项卡 
    67.      */  
    68.     private View getIndicatorView(String name, int iconid) {  
    69.         View view = inflater.inflate(R.layout.nav_tab, null);  
    70.         ImageView ivicon = (ImageView) view.findViewById(R.id.ivIcon);  
    71.         TextView tvtitle = (TextView) view.findViewById(R.id.tvTitle);  
    72.         ivicon.setImageResource(iconid);  
    73.         tvtitle.setText(name);  
    74.         return view;  
    75.     }  
    76. }  

posted on 2015-10-18 17:01  杭州糊涂虫  阅读(568)  评论(0)    收藏  举报

导航