在线观看免费黄色网址,一区二区视频,亚洲国产综合网,欧美VA免费高清在线观看

Android之ActivityGroup實現Tab分頁標簽

來源:網絡

點擊:2020

A+ A-

所屬頻道:新聞中心

關鍵詞: Android,ActivityGroup,Tab分頁

      很多客戶端軟件和瀏覽器軟件都喜歡用Tab分頁標簽來管理內容,除了可以用TabHost控件,還可以用ImageButton + ActivityGroup實現Tab分頁標簽。使用ImageButton + ActivityGroup實現Tab分頁標簽,主要是把一個Sub Activity(子Activity)的Window作為View添加到ActivityGroup所指定的容器中,本文使用LinearLayout作為容器裝載Sub Activity。

      接下來貼出本例運行的效果圖:

    接下來貼出本例運行的效果圖

     

      以下是切換時Sub Activity的生存周期的狀態變化:

    切換時Sub Activity的生存周期的狀態變化

     

      從subActivity1切換到subActivity2的時候,會徹底釋放subActivity1的資源。

      主Activity的main.xml的源碼如下:

      view plaincopy to clipboardprint?

      《?xml version=“1.0” encoding=“utf-8”?》

      《LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

      android:orientation=“vertical” android:layout_width=“fill_parent”

      android:layout_height=“fill_parent”》

      《LinearLayout android:id=“@+id/LinearLayout01”

      android:layout_height=“wrap_content” android:layout_width=“fill_parent”》

      《ImageButton android:layout_width=“wrap_content”

      android:layout_height=“wrap_content” android:id=“@+id/ibtnTab1”

      android:background=“@drawable/png1298”》《/ImageButton》

      《ImageButton android:layout_width=“wrap_content”

      android:layout_height=“wrap_content” android:id=“@+id/ibtnTab2”

      android:background=“@drawable/png1292”》《/ImageButton》

      《/LinearLayout》

      《LinearLayout android:id=“@+id/LinearLayout02”

      android:layout_width=“fill_parent” android:layout_height=“fill_parent”》《/LinearLayout》

      《/LinearLayout》

      《?xml version=“1.0” encoding=“utf-8”?》

      《LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

      android:orientation=“vertical” android:layout_width=“fill_parent”

      android:layout_height=“fill_parent”》

      《LinearLayout android:id=“@+id/LinearLayout01”

      android:layout_height=“wrap_content” android:layout_width=“fill_parent”》

      《ImageButton android:layout_width=“wrap_content”

      android:layout_height=“wrap_content” android:id=“@+id/ibtnTab1”

      android:background=“@drawable/png1298”》《/ImageButton》

      《ImageButton android:layout_width=“wrap_content”

      android:layout_height=“wrap_content” android:id=“@+id/ibtnTab2”

      android:background=“@drawable/png1292”》《/ImageButton》

      《/LinearLayout》

      《LinearLayout android:id=“@+id/LinearLayout02”

      android:layout_width=“fill_parent” android:layout_height=“fill_parent”》《/LinearLayout》

      《/LinearLayout》

      Sub Activity的XML源碼(listview.xml)如下:

      view plaincopy to clipboardprint?

      《?xml version=“1.0” encoding=“utf-8”?》

      《LinearLayout android:id=“@+id/LinearLayout01”

      xmlns:android=“http://schemas.android.com/apk/res/android”

      android:layout_width=“fill_parent” android:layout_height=“fill_parent”》

      《ListView android:id=“@+id/MyListView” android:layout_width=“fill_parent”

      android:layout_height=“fill_parent”》

      《/ListView》

      《/LinearLayout》

      《?xml version=“1.0” encoding=“utf-8”?》

      《LinearLayout android:id=“@+id/LinearLayout01”

      xmlns:android=“http://schemas.android.com/apk/res/android”

      android:layout_width=“fill_parent” android:layout_height=“fill_parent”》

      《ListView android:id=“@+id/MyListView” android:layout_width=“fill_parent”

      android:layout_height=“fill_parent”》

      《/ListView》

      《/LinearLayout》

     

      testActivityGroup.java源碼如下:

      view plaincopy to clipboardprint?

      package com.testActivityGroup;

      import android.app.ActivityGroup;

      import android.content.Intent;

      import android.os.Bundle;

      import android.view.View;

      import android.view.Window;

      import android.widget.ImageButton;

      import android.widget.LinearLayout;

      import android.widget.ListView;

      public class testActivityGroup extends ActivityGroup {

      /** Called when the activity is first created. */

      LinearLayout container;//裝載sub Activity的容器

      ImageButton ibtnTab1,ibtnTab2;

      @Override

      public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);

      container = (LinearLayout) findViewById(R.id.LinearLayout02);

      ibtnTab1=(ImageButton)this.findViewById(R.id.ibtnTab1);

      ibtnTab1.setOnClickListener(new ClickEvent());

      ibtnTab2=(ImageButton)this.findViewById(R.id.ibtnTab2);

      ibtnTab2.setOnClickListener(new ClickEvent());

      }

      class ClickEvent implements View.OnClickListener{

      @Override

      public void onClick(View v) {

      container.removeAllViews();

      Intent intent=new Intent(testActivityGroup.this, subActivity.class);

      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

      String[] str=new String[12];

      if(v==ibtnTab1)

      {

      for(int i=0;i《str.length;i++)

      str[i]=“單選”+String.valueOf(i);

      intent.putExtra(“Name”, “subActivity1”);

      intent.putExtra(“Strings”, str);

      intent.putExtra(“ChoiceMode”, ListView.CHOICE_MODE_SINGLE);//通過參數設置列表式樣

      }

      else if(v==ibtnTab2)

      {

      for(int i=0;i《str.length;i++)

      str[i]=“復選”+String.valueOf(i);

      intent.putExtra(“Name”, “subActivity2”);

      intent.putExtra(“Strings”, str);

      intent.putExtra(“ChoiceMode”, ListView.CHOICE_MODE_MULTIPLE);//通過參數設置列表式樣

      }

      Window subActivity=getLocalActivityManager().startActivity(“subActivity”,intent);

      container.addView(subActivity.getDecorView());

      }

      }

      }

      package com.testActivityGroup;

      import android.app.ActivityGroup;

      import android.content.Intent;

      import android.os.Bundle;

      import android.view.View;

      import android.view.Window;

      import android.widget.ImageButton;

      import android.widget.LinearLayout;

      import android.widget.ListView;

      public class testActivityGroup extends ActivityGroup {

      /** Called when the activity is first created. */

      LinearLayout container;//裝載sub Activity的容器

      ImageButton ibtnTab1,ibtnTab2;

      @Override

      public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);

      container = (LinearLayout) findViewById(R.id.LinearLayout02);

      ibtnTab1=(ImageButton)this.findViewById(R.id.ibtnTab1);

      ibtnTab1.setOnClickListener(new ClickEvent());

      ibtnTab2=(ImageButton)this.findViewById(R.id.ibtnTab2);

      ibtnTab2.setOnClickListener(new ClickEvent());

      }

      class ClickEvent implements View.OnClickListener{

      @Override

      public void onClick(View v) {

      container.removeAllViews();

      Intent intent=new Intent(testActivityGroup.this, subActivity.class);

      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

      String[] str=new String[12];

      if(v==ibtnTab1)

      {

      for(int i=0;i《str.length;i++)

      str[i]=“單選”+String.valueOf(i);

      intent.putExtra(“Name”, “subActivity1”);

      intent.putExtra(“Strings”, str);

      intent.putExtra(“ChoiceMode”, ListView.CHOICE_MODE_SINGLE);//通過參數設置列表式樣

      }

      else if(v==ibtnTab2)

      {

      for(int i=0;i《str.length;i++)

      str[i]=“復選”+String.valueOf(i);

      intent.putExtra(“Name”, “subActivity2”);

      intent.putExtra(“Strings”, str);

      intent.putExtra(“ChoiceMode”, ListView.CHOICE_MODE_MULTIPLE);//通過參數設置列表式樣

      }

      Window subActivity=getLocalActivityManager().startActivity(“subActivity”,intent);

      container.addView(subActivity.getDecorView());

      }

      }

      }

     

      subActivity.java源碼如下:

      view plaincopy to clipboardprint?

      package com.testActivityGroup;

      import android.app.Activity;

      import android.os.Bundle;

      import android.util.Log;

      import android.widget.ArrayAdapter;

      import android.widget.ListView;

      public class subActivity extends Activity {

      String name;

      public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.listview);

      // 讀取列表內容

      name = this.getIntent().getStringExtra(“Name”);

      String[] str = this.getIntent().getStringArrayExtra(“Strings”);

      int choiceMode = this.getIntent().getIntExtra(“ChoiceMode”,

      ListView.CHOICE_MODE_NONE);

      ListView listView = (ListView) findViewById(R.id.MyListView);

      // 設置列表的式樣

      int itemID = android.R.layout.simple_list_item_1;

      if (choiceMode == ListView.CHOICE_MODE_MULTIPLE)// 主Activity要求多選

      itemID = android.R.layout.simple_list_item_multiple_choice;

      else if (choiceMode == ListView.CHOICE_MODE_SINGLE)// 主Activity要求單選

      itemID = android.R.layout.simple_list_item_single_choice;

      ArrayAdapter《String》 arrayAdapter = new ArrayAdapter《String》(this,

      itemID, str);

      listView.setAdapter(arrayAdapter);

      listView.setChoiceMode(choiceMode);

      Log.e(name, “onCreate”);// 顯示當前狀態,onCreate與onDestroy對應

      }

      @Override

      public void onDestroy() {

      super.onDestroy();

      Log.e(name, “onDestroy”);// 顯示當前狀態,onCreate與onDestroy對應

      }

      @Override

      public void onStart() {

      super.onStart();

      Log.e(name, “onStart”);// 顯示當前狀態,onStart與onStop對應

      }

      @Override

      public void onStop() {

      super.onStop();

      Log.e(name, “onStop”);// 顯示當前狀態,onStart與onStop對應

      }

      @Override

      public void onRestart() {

      super.onRestart();

      Log.e(name, “onRestart”);

      }

      @Override

      public void onResume() {

      super.onResume();

      Log.e(name, “onResume”);// 顯示當前狀態,onPause與onResume對應

      }

      @Override

      public void onPause() {

      super.onResume();

      Log.e(name, “onPause”);// 顯示當前狀態,onPause與onResume對應

      }

      }

      

    (審核編輯: 智匯小新)

    聲明:除特別說明之外,新聞內容及圖片均來自網絡及各大主流媒體。版權歸原作者所有。如認為內容侵權,請聯系我們刪除。

    李现又去公园打鸟了| 陈昊宇陈丽君四公帮唱| 黄霄雲方回应翻唱争议| newjeans是高层内斗的牺牲品吗| 陈昊宇陈丽君四公帮唱 | 站姐愚人节团建预告| 吴柳芳发博过三月三| 提高墙体楼板隔声性能 | 减肥上美团不瘦可获赔 | 李昀锐好标准的体育生下楼梯| 王曼昱小岱FocusStar四月刊预告 缅甸地震仍有270人失踪 | 难哄3月全网播放冠军| 河南一枯井发现近百名烈士遗骸 | 申请过国家助学贷款的同学注意了| 清明档预售前三名| 一直对月薪三万没概念直到换算成天| 4月一起加油| 女装啥时候能回归正常审美| 韩国庄仕洋| 日本特大地震若发生或致近30万人死亡| 清明档预售前三名| 王艳发了赤脚鬼| BLACKPINK未公开的物料| 王艳发了赤脚鬼| newjeans是高层内斗的牺牲品吗| 少吃水果少生很多病不具有普遍性| 一诺FMVP皮肤即将上线| 陈昊宇陈丽君四公帮唱| 站姐愚人节团建预告| 白敬亭 宋轶| 黄子韬徐艺洋睡觉前要对暗号| 李现又去公园打鸟了| 男子赠妻子闺蜜7万被判全额返还加利息 | 雁回时反转| BLACKPINK未公开的物料| 疑似一种新病毒在俄罗斯蔓延| 想和你去四月的春天里坐坐| 王艳发了赤脚鬼| 乘风2025四公帮唱组队征集| 甲亢哥成都行直播| 缅甸地震已致2056人死亡|