android.app包中含有一個ActivityGroup類,該類是Activity的容器,可以包含多個嵌套進來的Activitys,這篇文章就是借助ActivityGroup可以嵌套Activity的功能來實現(xiàn)Tab功能。tab這種UI在很多的移動應用中可以看到,包括android、iphone、window phone7等移動終端上都有這樣的應用,Tab這種UI方式具有小視圖大容量的特點。
首先,從SDK中doc文檔中都可以獲知,ActivityGroup類的父類是Activity(見下圖),也就是說二者具有相同的接口和生命周期,同Activity一樣,也有onCreate()、onPause()等函數(shù)可供我們重載。
ActivityGroup中有兩個public方法(下圖):ActivityGroup中可以調用getLocalActivityManage()方法獲取LocalActityManage來管理Activity。
ActivityGroup實現(xiàn)的tab功能的效果圖如下。
先看一下java代碼:
- public class MainView extends ActivityGroup {
- @SuppressWarnings ( “unused” )
- private LinearLayout bodyView , headview ;
- private LinearLayout one , two , three , four ;
- private int flag = 0 ; // 通過標記跳轉不同的頁面,顯示不同的菜單項
- /** Called when the activity is first created. */
- @Override
- public void onCreate ( Bundle savedInstanceState ) {
- super . onCreate ( savedInstanceState );
- setContentView ( R . layout . view_main );
- initMainView ();
- // 顯示標記頁面
- showView ( flag );
- one . setOnClickListener ( new OnClickListener () {
- public void onClick ( View v ) {
- // TODO Auto-generated method stub
- flag = 0 ;
- showView ( flag );
- }
- });
- two . setOnClickListener ( new OnClickListener () {
- public void onClick ( View v ) {
- // TODO Auto-generated method stub
- flag = 1 ;
- showView ( flag );
- }
- });
- three . setOnClickListener ( new OnClickListener () {
- public void onClick ( View v ) {
- // TODO Auto-generated method stub
- flag = 2 ;
- showView ( flag );
- }
- });
- four . setOnClickListener ( new OnClickListener () {
- public void onClick ( View v ) {
- // TODO Auto-generated method stub
- flag = 3 ;
- showView ( flag );
- }
- });
- }
- /*
- * 初始化主界面
- */
- public void initMainView () {
- headview =( LinearLayout ) findViewById ( R . id . head );
- bodyView =( LinearLayout ) findViewById ( R . id . body );
- one =( LinearLayout ) findViewById ( R . id . one );
- two =( LinearLayout ) findViewById ( R . id . two );
- three =( LinearLayout ) findViewById ( R . id . three );
- four =( LinearLayout ) findViewById ( R . id . four );
- }
- // 在主界面中顯示其他界面
- public void showView ( int flag ) {
- switch ( flag ) {
- case 0 :
- bodyView . removeAllViews ();
- View v = getLocalActivityManager (). startActivity ( “one” ,
- new Intent ( MainView . this , OneView . class )). getDecorView ();
- one . setBackgroundResource ( R . drawable . frame_button_background );
- two . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- three . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- four . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- bodyView . addView ( v );
- break ;
- case 1 :
- bodyView . removeAllViews ();
- bodyView . addView ( getLocalActivityManager (). startActivity ( “two” ,
- new Intent ( MainView . this , TwoView . class ))
- . getDecorView ());
- one . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- two . setBackgroundResource ( R . drawable . frame_button_background );
- three . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- four . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- break ;
- case 2 :
- bodyView . removeAllViews ();
- bodyView . addView ( getLocalActivityManager (). startActivity (
- “three” , new Intent ( MainView . this , ThreeView . class ))
- . getDecorView ());
- one . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- two . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- three . setBackgroundResource ( R . drawable . frame_button_background );
- four . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- break ;
- case 3 :
- bodyView . removeAllViews ();
- bodyView . addView ( getLocalActivityManager (). startActivity (
- “four” , new Intent ( MainView . this , FourView . class ))
- . getDecorView ());
- one . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- two . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- three . setBackgroundResource ( R . drawable . frame_button_nopressbg );
- four . setBackgroundResource ( R . drawable . frame_button_background );
- break ;
- default :
- break ;
- }
- }
- }
程序中重要的是如下的方法:
- bodyView . removeAllViews ();
- bodyView . addView ( getLocalActivityManager (). startActivity ( “two” ,
- new Intent ( MainView . this , TwoView . class ))
- . getDecorView ());
使用view的removeAllViews()方法清除不需要的view,使用addView(View v)方法添加需要的view。
getLocalActivityManager().startActivity(“two”,new Intent(MainView.this, TwoView.class))得到一個window對象,window對象調用
getDecorView()獲取view。關于window的方法可以參考android.app.Window。
通過tab的效果圖可以看到這個效果使用了上、中、下三種布局,layout就可以這樣做了。實現(xiàn)layout就可以實現(xiàn)tab功能了。
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
