Android 底部TabActivity(2)——ActivityGroup|顶部底部均有Tab标签之一

来源:互联网 发布:淘宝助手官方网站 编辑:程序博客网 时间:2024/05/16 11:39

今天这篇文章记述一下页面顶部底部上下均有Tab标签页的特殊需求!使用了过时的ActivityGroup。


再看一下整个Project的结构,如下

下面逐一介绍一下实现过程,一贯风格,具体实现还是看注释吧,代码也不是很多,就不啰嗦了。

step1:首先是主界面MainActivity.java

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package sun.geoffery.tabtopbottom;  
  2.   
  3. import android.app.ActivityGroup;  
  4. import android.content.Intent;  
  5. import android.graphics.Color;  
  6. import android.os.Bundle;  
  7. import android.view.Window;  
  8. import android.widget.TabHost;  
  9. import android.widget.TabHost.TabSpec;  
  10. import android.widget.TabWidget;  
  11. import android.widget.TextView;  
  12.   
  13. /** 
  14.  * All rights Reserved, Designed By GeofferySun 
  15.  *  
  16.  * @Title: MainActivity.java 
  17.  * @Package sun.geoffery.tabtopbottom 
  18.  * @Description:上下都有Tab的界面 
  19.  * @author: GeofferySun 
  20.  * @date: 2014-12-9 下午3:41:04 
  21.  * @version V1.0 
  22.  */  
  23. public class MainActivity extends ActivityGroup {  
  24.     // 定义一个TabHost控件  
  25.     private TabHost mTabHost;  
  26.   
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.           
  29.         // 设置隐藏标题栏  
  30.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  31.           
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.activity_main);  
  34.           
  35.         // 获取TabHost布局  
  36.         mTabHost = (TabHost) findViewById(R.id.tabhost);  
  37.         mTabHost.setup(this.getLocalActivityManager());  
  38.   
  39.         TabSpec _tab;  
  40.           
  41.         _tab = mTabHost.newTabSpec("home");  
  42.         _tab.setIndicator("首页", getResources().getDrawable(R.drawable.ic_launcher));  
  43.         _tab.setContent(new Intent(this, HomeActivity.class));  
  44.         mTabHost.addTab(_tab);  
  45.   
  46.         _tab = mTabHost.newTabSpec("order");  
  47.         _tab.setIndicator("订单", getResources().getDrawable(R.drawable.ic_launcher));  
  48.         _tab.setContent(new Intent(this, OrderActivity.class));  
  49.         mTabHost.addTab(_tab);  
  50.   
  51.         _tab = mTabHost.newTabSpec("wallet");  
  52.         _tab.setIndicator("钱包", getResources().getDrawable(R.drawable.ic_launcher));  
  53.         _tab.setContent(new Intent(this, WalletActivity.class));  
  54.         mTabHost.addTab(_tab);  
  55.   
  56.         // 设置第一个标签页被选中  
  57.         mTabHost.setCurrentTab(0);  
  58.   
  59.         TabWidget tabWidget = mTabHost.getTabWidget();  
  60.           
  61.         for (int i = 0; i < tabWidget.getChildCount(); i++) {  
  62.             TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);  
  63.             tv.setTextColor(Color.LTGRAY);// 设置Tab栏字体的颜色  
  64.         }  
  65.     }  
  66. }  

step2:主页面对应的布局文件

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <!-- 选项卡布局必须要用TabHost -->  
  8.     <TabHost  
  9.         android:id="@+id/tabhost"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent" >  
  12.   
  13.         <!-- 镶嵌线性布局 -->  
  14.         <LinearLayout  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="fill_parent"  
  17.             android:orientation="vertical" >  
  18.   
  19.             <!-- 帧布局 -->  
  20.             <FrameLayout  
  21.                 android:id="@android:id/tabcontent"  
  22.                 android:layout_width="fill_parent"  
  23.                 android:layout_height="wrap_content"  
  24.                 android:layout_weight="1"/>  
  25.               
  26.             <!-- TabWidget是Tab标签的使用 -->  
  27.             <TabWidget  
  28.                 android:id="@android:id/tabs"  
  29.                 android:layout_width="fill_parent"  
  30.                 android:layout_height="wrap_content"  
  31.                 android:layout_weight="0"   
  32.                 android:background="#404040"/>  
  33.         </LinearLayout>  
  34.     </TabHost>  
  35.   
  36. </LinearLayout>  

step3:普通的单独页面HomeActivity.java(和WalletActivity.java),什么也没做,只加载了个简单的布局!

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package sun.geoffery.tabtopbottom;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class HomeActivity extends Activity {  
  7.   
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_simple);  
  11.     }  
  12. }  

step4:普通的单独页面HomeActivity.java(和WalletActivity.java)的布局文件activity_simple.xml,什么也也没有!

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#FFCC00"  
  6.     android:orientation="vertical" >  
  7.       
  8. </LinearLayout>  

step5:重点来了,顶部也要加上Tab标签的页面OrderActivity.java。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package sun.geoffery.tabtopbottom;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6. import android.widget.TabHost;  
  7. import android.widget.TabWidget;  
  8. import android.widget.TextView;  
  9.   
  10. /** 
  11.  * All rights Reserved, Designed By GeofferySun  
  12.  * @Title:  OrderActivity.java  
  13.  * @Package sun.geoffery.tabtopbottom  
  14.  * @Description:顶部Tab页面 
  15.  * @author: GeofferySun    
  16.  * @date:   2014-12-9 下午5:31:00  
  17.  * @version V1.0 
  18.  */  
  19. public class OrderActivity extends Activity {  
  20.     private TabHost mTabHost;  
  21.   
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_tab);  
  25.           
  26.         mTabHost = (TabHost) findViewById(R.id.mytabhost);  
  27.         mTabHost.setup();  
  28.           
  29.         TabWidget tabWidget = mTabHost.getTabWidget();  
  30.         mTabHost.addTab(mTabHost.newTabSpec("待服务").setIndicator("待服务").setContent(R.id.page0));  
  31.         mTabHost.addTab(mTabHost.newTabSpec("已完成").setIndicator("已完成").setContent(R.id.page1));  
  32.         mTabHost.addTab(mTabHost.newTabSpec("已取消").setIndicator("已取消").setContent(R.id.page2));  
  33.           
  34.         int height =120;   
  35. //      int width =45;    
  36.           
  37.         for(int i=0;i<tabWidget.getChildCount();i++){  
  38.             // 设置高度、宽度,由于宽度设置为fill_parent,在此对它没效果  
  39.             tabWidget.getChildAt(i).getLayoutParams().height=height;  
  40.             // 设置tab中标题文字的颜色,不然默认为黑色   
  41.             final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);  
  42.             tv.setTextColor(Color.LTGRAY);  
  43.         }  
  44.     }  
  45. }  

step6:顶部Tab页面对应的布局文件activity_tab.xml。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/mytabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:orientation="vertical" >  
  11.   
  12.         <TabWidget  
  13.             android:id="@android:id/tabs"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:background="#404040" />  
  17.   
  18.         <!-- 注意FrameLayout/TabWidget标签的位置 -->  
  19.         <FrameLayout  
  20.             android:id="@android:id/tabcontent"  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="0dp"  
  23.             android:layout_weight="1" >  
  24.   
  25.             <TextView  
  26.                 android:id="@+id/page0"  
  27.                 android:layout_width="match_parent"  
  28.                 android:layout_height="match_parent"  
  29.                 android:background="#6699FF"  
  30.                 android:text="待服务页面" >  
  31.             </TextView>  
  32.   
  33.             <TextView  
  34.                 android:id="@+id/page1"  
  35.                 android:layout_width="match_parent"  
  36.                 android:layout_height="match_parent"  
  37.                 android:background="#FF9900"  
  38.                 android:text="已完成页面" >  
  39.             </TextView>  
  40.   
  41.             <TextView  
  42.                 android:id="@+id/page2"  
  43.                 android:layout_width="match_parent"  
  44.                 android:layout_height="match_parent"  
  45.                 android:background="#99cc33"  
  46.                 android:text="已取消页面" >  
  47.             </TextView>  
  48.         </FrameLayout>  
  49.     </LinearLayout>  
  50.   
  51. </TabHost>  

step7:最后是清单文件Manifest.xml。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="sun.geoffery.tabtopbottom"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="14"  
  9.         android:targetSdkVersion="21" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name=".MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.         <activity android:name=".OrderActivity"></activity>  
  25.         <activity android:name=".HomeActivity"></activity>  
  26.         <activity android:name=".WalletActivity"></activity>  
  27.     </application>  
  28.   
  29. </manifest>  


到此为止,全部代码就可以玩了!看一下最终效果。


虽然代码很简单,但还是提供个连接吧!


0 0