Android之ActivityGroup實現Tab功能 )

來源:互聯網
上載者:User

android.app包中含有一個ActivityGroup類,該類是Activity的容器,可以包含多個嵌套進來的Activitys,這篇文章就是藉助ActivityGroup可以嵌套Activity的功能來實現Tab功能。tab這種UI在很多的行動裝置 App中可以看到,包括android、iphone、window phone7等移動終端上都有這樣的應用,Tab這種UI方式具有小視圖大容量的特點。

首先,從SDK中doc文檔中都可以獲知,ActivityGroup類的父類是Activity(見),也就是說二者具有相同的介面和生命週期,同Activity一樣,也有onCreate()、onPause()等函數可供我們重載。

ActivityGroup中有兩個public方法():ActivityGroup中可以調用getLocalActivityManage()方法擷取LocalActityManage來管理Activity。

ActivityGroup實現的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 stubflag = 0;showView(flag);}});two.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubflag = 1;showView(flag);}});three.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubflag = 2;showView(flag);}});four.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubflag = 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;}}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.