使用ActivityGroup類顯示多個Activity

Phenixyf發表於2016-07-29

有些情況下需要把一個Activity分割成幾部分,如上半部分繪製圖形,下半部分顯示圖表,這時就需要用到ActivityGroup。

定義一個類繼承自ActivityGroup,Java檔案如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.h.callsta;
 
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
 
public class DetailActivity extends ActivityGroup{
 
     private LinearLayout lo1, lo2; 
     // LocalActivityManager物件
     LocalActivityManager  lam = getLocalActivityManager();
      
     public void onCreate(Bundle savedInstanceState) { 
          
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.activity_detail);
            // 獲得LinearLayout物件
            lo1 = (LinearLayout) findViewById(R.id.lo1);
            lo2 = (LinearLayout) findViewById(R.id.lo2);
            start_activity();
             
     }
      
     public void start_activity(){
          
         // 載入兩個視窗
         Window win1 = lam.startActivity("0",
                 new Intent(DetailActivity.this, DetailChart.class));
         View view1 = win1.getDecorView();
         lo1.addView(view1);   
         Window win2 = lam.startActivity("1",
                 new Intent(DetailActivity.this, DetailList.class));
         View view2 = win2.getDecorView();
         lo2.addView(view2);
          
     }
      
}

在該類對應的XML佈局檔案中,定義了兩個LinearLayout,分別用來顯示兩個Activity。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
 
 
 
 
    <LinearLayout
        android:id="@+id/lo1"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
 
    </LinearLayout>
 
 
 
 
 
    <LinearLayout
        android:id="@+id/lo2"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
 
    </LinearLayout>
 
</LinearLayout>

在Java檔案中,通過getLocalActivityManager()獲得LocalActivityManager物件,再通過findViewById()方法獲得佈局檔案中的兩個LinearLayout。最後,用LocalActivityManager物件的startActivity()方法,分別啟動兩個Activity--DetailChart(圖形)和DetailList(列表),並分別用getDecorView()方法獲得View物件,addView()到兩個LinearLayout上,這樣,就實現了在一個Activity類同時顯示兩個Activity。


http://www.cnblogs.com/mstk/archive/2014/03/26/3625541.html


相關文章