智汇工业-智慧工业、智能制造及工业智能、工业互联门户网站,专业的工业“互联网+”传媒

Android之ActivityGroup實現Tab分頁標簽

來源:網絡

點擊:2037

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對應

      }

      }

      

    (審核編輯: 智匯小新)

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

    主站蜘蛛池模板: 呼吸家官网|肺功能检测仪生产厂家|国产肺功能仪知名品牌|肺功能检测仪|肺功能测试仪|婴幼儿肺功能仪|弥散残气肺功能仪|肺功能测试系统|广州红象医疗科技有限公司|便携式肺功能仪|大肺功能仪|呼吸康复一体机|儿童肺功能仪|肺活量计|医用简易肺功能仪|呼吸康复系统|肺功能仪|弥散肺功能仪(大肺)|便携式肺功能检测仪|肺康复|呼吸肌力测定肺功能仪|肺功能测定仪|呼吸神经肌肉刺激仪|便携式肺功能 | 潍坊卓瑞机械有限公司,输送设备,石灰消化设备,餐厨垃圾设备,化机浆设备,污泥脱水 | 企业宣传片制作公司-广告宣传片拍摄-专题片,tvc广告制作-拍摄微电影影视公司-艺虎文化 | 莫非传媒官网-江西知名的网络营销推广服务平台南昌网络公司,专业网络公关,品牌危机处理,网站SEO优化,微信朋友圈广告,网站建设,南昌莫非文化传媒有限公司 | 泊头市特种油泵阀制造有限公司 - 渣油泵,重油泵,沥青泵,高压齿轮泵,煤焦油泵,导热油泵,三螺杆泵,圆弧齿轮泵,不锈钢齿轮泵, | 橡皮艇_冲锋舟_充气钓鱼船_橡皮艇价格_海威龙橡皮艇生产厂家-首页 | 金华银焊条-金华银焊丝-银焊片制造商-银焊环批发商-浙江焊丝生产商-焊环厂家-浙江永旺焊材制造有限公司 | 真空干燥箱厂家-热风循环烘箱生产厂家-鼓风烘干箱价格-南京火燥机械科技有限公司 | 景德镇芳然装饰有限公司| 虚商通信-电销卡 电销助手| 上海钧尚电器有限公司 - Faulhaber电机 AMETEK pittman电机 AMETEK ROTRON军用航空风机 Exlar电动缸 MAE电机 MCG电机 CP电动工具 马头工具 AMCI驱动器 直流电机 减速箱 直流伺服电机,无刷电机,直线电机 直流防爆电机 防爆电机 汽车助力转向电机 EPS电机 faulhaber motor faulhaber gearbox NANOTEC电机 ELWOOD电机 PHYTRON电机 EXLAR伺服电动缸 高力矩、高性能直流电机,音圈电机,风机,直流风机,航空风机 | 气泡式洗菜机-气泡喷淋清洗机-喷淋清洗风干线-诸城市迪凯工业装备有限公司 | 手术示教系统-实训示教系统-数字化手术室-直播录播系统 - 深圳市视源视讯技术有限公司 | 生物除臭剂-养殖场垃圾除臭剂-垃圾填埋场除臭剂-成都微菌环境 | 思沃普智能会议预约管理系统-视频会议管理-信息发布-访客管理-会议运维-会议支持-工位管理系统 | 健身器材_健身器材厂_健身器材厂家-徐州兰士健身器材有限公司 | 山东金起起重机械有限公司[官网]-金桥银路悬臂吊,金起龙门吊,山东金起起重行吊,单梁起重机 | 自动烘干线,工业烘箱生产厂家-湖南耐美特智能装备有限公司官方网站 | 开水机-节能开水器-即热式开水器-上海捷水环保科技有限公司 | 自动烘干线,工业烘箱生产厂家-湖南耐美特智能装备有限公司官方网站 | 上海汽车音响_上海汽车隔音降噪_上海汽车音响改装店_上海音豪 | 徐州护栏,围栏,锌铁丝网围栏安全设施专家徐州铜山区威峰金属护栏厂 | 汽车衡,进口地磅,地磅厂家,无人值守称重系统丨青岛维特沃斯 | 生物除臭剂-养殖场垃圾除臭剂-垃圾填埋场除臭剂-成都微菌环境 | 徐州护栏,围栏,锌铁丝网围栏安全设施专家徐州铜山区威峰金属护栏厂 | 秦皇岛图成玻璃_横切机,琴键落板,堆垛机械手,玻璃钢化设备,掰边机,铺纸机,水平堆垛机+超大板堆垛机,纵掰纵分,下片机,冷端优化切割 | 云南昆明微信公众号小程序开发公司|抖音|网站建设APP制作-鸿翥网络 | 庭院大门,铝艺大门厂家,别墅庭院大门「免费设计」汉兰达庭院门厂家 | 智能仓储货架厂家 - 汇峰仓储| 重庆物流公司,重庆商贸货运,工厂物流,同城冷链物流配送,物流软件租售-重庆协通国际物流有限公司 重庆污水处理设备_废气处理设备_纯净水设备-山艺环保 | 山东净化车间_净化工程_净化公司-山东海蓝净化装饰工程有限公司 山东金起起重机械有限公司[官网]-金桥银路悬臂吊,金起龙门吊,山东金起起重行吊,单梁起重机 | 深圳万和制药有限公司_消化领域专业公司 万和香港(集团)成员 深圳同步带轮_东莞齿轮加工_东莞同步轮厂家-东莞东城精胜机械配件厂 | 三菱PLC,三菱变频器,三菱伺服,三菱电机--广州凌控 | 洛阳牡丹瓷工艺品生产厂家-特色旅游纪念品礼品定制网! | 园林绿化平台|园林绿化网|苗木网|苗圃网||苗木报价网|园林招标网|园林苗木网|园林工程网|景观设计网|园林机械网|绿化苗木网| | 无锡大型数控龙门铣,喷涂加工,回火抛丸加工,精密不锈钢焊接机床身机床底座制造加工-无锡美高帝机械有限公司 | 菏泽博捷电梯有限公司-菏泽博捷电梯有限公司 | 九江市东鸿气体有限公司| 重庆监控-监控系统-大型弱电工程-重庆万建电子工程有限责任公司是智能化一级工程公司 | 青岛网站建设_网站制作_品牌设计_网站设计_圭谷设计 | 上海品牌设计公司|品牌策划公司|包装设计公司|上海全案LOGO设计VI设计-木马品牌设计 |