Android ActivityGroup和TabActiviy的差异性

AndroidActivityGroup和TabActiviy的差异性

1)<wbr>ActivityGroup</wbr><wbr><wbr></wbr></wbr>

<wbr><wbr><wbr><span size="+0" style="word-wrap:normal; word-break:normal; line-height:24px">根据SDK的解释其功能“<span style="color:#ED1C24; word-wrap:normal; word-break:normal"><strong>A screen that contains and runs multiple embedded activities.”。</strong><span style="color:#000000; word-wrap:normal; word-break:normal">翻译成汉语也就是大概</span></span><span style="font-weight:bold"><span style="color:#ED1C24; word-wrap:normal; word-break:normal">"<span style="word-wrap:normal; word-break:normal; line-height:normal; font-weight:normal; font-size:12px; font-family:宋体,微软雅黑,'Lucida Grande',Geneva,Arial,Verdana,'Lucida sans Unicode',Helvetica,sans-serif; color:rgb(112,112,112); border-collapse:separate"><span style="word-wrap:normal; word-break:normal; line-height:19px; font-size:13px"><strong><span style="color:#ED1C24; word-wrap:normal; word-break:normal">在Android中,ActivityGroup类是Activity的容器,可以包含多个嵌套进来的Activitys</span></strong></span></span>".<span style="word-wrap:normal; word-break:normal; line-height:normal; font-weight:normal; font-size:12px; font-family:宋体,微软雅黑,'Lucida Grande',Geneva,Arial,Verdana,'Lucida sans Unicode',Helvetica,sans-serif; color:rgb(112,112,112); border-collapse:separate"><span style="word-wrap:normal; word-break:normal; line-height:19px; font-size:13px"><span style="color:#000000; word-wrap:normal; word-break:normal">接下来依然采用源码分析的方式来了解该类的内部实现。</span></span></span></span></span></span></wbr></wbr></wbr>

<wbr><wbr><wbr><span style="color:#000000; word-wrap:normal; word-break:normal">首先,从SDK中和源码中都可以获知,ActivityGroup类的父类是Activity,也就是说二者具有相同的接口和生命周期,同Activity一样,也有onCreate()、onPause()等函数可供我们重载。在ActivityGroup的源码中有成员变量<br><wbr><wbr><wbr>protected LocalActivityManager mLocalActivityManager;<br> 该变量在ActivityGroup的构造函数中创建并初始化,可见,ActivityGroup的功能实现肯定是要委托给这个对象来完成了。为了给用户开放对此对象的访问,ActivityGroup提供了</wbr></wbr></wbr></span></wbr></wbr></wbr>

<wbr></wbr>

1
2
3
4
5
public final LocalActivityManager getLocalActivityManager() {
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>return</strong></span> mLocalActivityManager<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span></wbr></wbr>

通过浏览ActivityGroup的源码可以发现,几乎全部是以通过LocalActivityManager对象来完成的具体动作,比如:

1
2
3
4
5
6
7
8
9
10
11
protected void onCreate(Bundle savedInstanceState) {
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>super</strong></span>.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">onCreate</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>savedInstanceState<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
        Bundle states <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> savedInstanceState <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">!=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">?</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>Bundle<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> savedInstanceState.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">getBundle</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>STATES_KEY<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">:</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
        mLocalActivityManager.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">dispatchCreate</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>states<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span></wbr></wbr></wbr></wbr></wbr>

下面,我们就来看一下LocalActivityManager的源码。在该类中,提供了一个私有类

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
private static class LocalActivityRecord extends Binder {
<wbr>
        LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,51,153); background-color:transparent">String</span> _id, Intent _intent<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
            id <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> _id<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
            intent <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> _intent<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>final</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,51,153); background-color:transparent">String</span> id<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>                <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Unique name of this record.</em></span>
<wbr>
        Intent intent<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>                  <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Which activity to run here.</em></span>
<wbr>
        ActivityInfo activityInfo<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>      <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Package manager info about activity.</em></span>
<wbr>
        Activity activity<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>              <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Currently instantiated activity.</em></span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,51,153); background-color:transparent">Window</span> window<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>                  <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Activity's top-level window.</em></span>
<wbr>
        Bundle instanceState<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>           <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Last retrieved freeze state.</em></span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>int</strong></span> curState <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> RESTORED<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>        <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Current state the activity is in.</em></span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

用于保存Activity的信息,并提供了

1
2
3
4
5
6
7
private final Map<String, LocalActivityRecord> mActivities
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>new</strong></span> HashMap<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">&lt;</span>String, LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">&gt;</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>private</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>final</strong></span> ArrayList<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">&lt;</span>LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">&gt;</span> mActivityArray
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>new</strong></span> ArrayList<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">&lt;</span>LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">&gt;</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span></wbr></wbr></wbr>

采用这样的数据结构用于对所有嵌入的子Activity信息进行保存处理。其中前者用于通过String快速查找,后者用于以数组的方式快速访问,是典型的以空间换时间的的方式。

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
43
44
45
46
47
public void dispatchCreate(Bundle state) {
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>if</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>state <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">!=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>final</strong></span> Iterator<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">&lt;</span>String<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">&gt;</span> i <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> state.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">keySet</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span>.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">iterator</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>while</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>i.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">hasNext</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>try</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>final</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,51,153); background-color:transparent">String</span> id <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> i.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">next</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>final</strong></span> Bundle astate <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> state.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">getBundle</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
                    LocalActivityRecord r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> mActivities.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">get</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>if</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">!=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
                        r.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">instanceState</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> astate<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>else</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
                        r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>new</strong></span> LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id, <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
                        r.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">instanceState</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> astate<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
                        mActivities.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">put</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id, r<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
                        mActivityArray.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">add</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>r<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>catch</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,51,153); background-color:transparent">Exception</span> e<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
……
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr><wbr><wbr>
        mCurState <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> CREATED<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

从这里我们可以看出,当有一个ActivityGroup被Create的时候,就会有对应的Activity信息被保存到数组中。

当我们调用LocalActivityManager的startActivity()以产生Window的时候,我们也可以看到

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
public Window startActivity(String id, Intent intent) {
<wbr>
                   ……
<wbr>
        LocalActivityRecord r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> mActivities.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">get</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>if</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">==</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
            r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>new</strong></span> LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id, intent<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
            adding <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>true</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr>
                   ……
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>if</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>adding<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
            mActivities.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">put</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id, r<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
            mActivityArray.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">add</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>r<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr>
        ……
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

有了这个数组,就可以遍历到ActivityGroup中嵌入的Activitys了,从而可以实现ActivityGroup的功能。

以上的分析结果产生的类图如下:

其核心的方面主要体现在:

<wbr><wbr><wbr><wbr><wbr><wbr>Intent i = new Intent(MainActivity.this,IndexActivity.class);<br><wbr><wbr><wbr><wbr><wbr><wbr>Window indexWindow = localManager.startActivity(INDEX_ID, i);<br><wbr><wbr><wbr><wbr><wbr><wbr>indexDecorView = indexWindow.getDecorView();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr>localManager = this.getLocalActivityManager();</wbr></wbr></wbr></wbr></wbr>

然后将view加进你想要用的模块,多个view之间控制好隐藏和显示,就可以了,用activityGroup你会被焦点的问题搞郁闷,我现在一直纠结,如何在window 之间切换,或者request到focus,有这个方面的经验可以谈下。

范例1 使用ActivityGroup来切换Activity和Layout

<wbr><wbr><wbr></wbr></wbr></wbr>在一个主界面中做Activity切换一般都会用TabActivity,使用方便,Activity互相之间相对独立,但是可定制性不强,而且修改起来很麻烦。当然也可以把layout分开,把逻辑代码全写在主界面的逻辑代码中,但是很明显可维护性相当差,这里通过ActivityGroup来解决这个问题。
一、效果图

    Android ActivityGroup和TabActiviy的差异性

    要求点击底部不同图片按钮切换不同的Activity,并在中间显示Activity对应的ContentView。

<wbr></wbr>

  二、 实现代码

    2.1  layout.xml

<?xml<wbr>version="1.0"<wbr>encoding="utf-8"</wbr></wbr>?>
<LinearLayout<wbr></wbr>xmlns:android="http://schemas.android.com/apk/res/android"
<wbr><wbr><wbr><wbr>android:layout_width</wbr></wbr></wbr></wbr>
="fill_parent"<wbr>android:orientation</wbr>="vertical"
<wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr>
="fill_parent">
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<LinearLayout<wbr></wbr>android:gravity="center_horizontal"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:background</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="@drawable/myinfor2"<wbr>android:layout_width</wbr>="fill_parent"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="wrap_content">
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<TextView<wbr></wbr>android:id="@+id/cust_title"<wbr>android:textColor</wbr>="@android:color/white"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:textSize</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="28sp"<wbr>android:text</wbr>="模块1"<wbr>android:layout_width</wbr>="wrap_content"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="wrap_content"></TextView>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
</LinearLayout>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<!--<wbr>中间动态加载View<wbr></wbr></wbr>-->
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<ScrollView<wbr></wbr>android:measureAllChildren="true"<wbr>android:id</wbr>="@+id/containerBody"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_weight</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="1"<wbr>android:layout_height</wbr>="fill_parent"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_width</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="fill_parent">
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
</ScrollView>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<LinearLayout<wbr></wbr>android:background="@android:color/black"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_gravity</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="bottom"<wbr>android:orientation</wbr>="horizontal"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_width</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="fill_parent"<wbr>android:layout_height</wbr>="wrap_content">
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<!--<wbr>功能模块按钮1<wbr></wbr></wbr>-->
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<ImageView<wbr></wbr>android:id="@+id/btnModule1"<wbr>android:src</wbr>="@android:drawable/ic_dialog_dialer"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginLeft</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="7dp"<wbr>android:layout_marginTop</wbr>="3dp"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginBottom</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="3dp"<wbr>android:layout_width</wbr>="wrap_content"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="wrap_content"<wbr></wbr>/>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<!--<wbr>功能模块按钮2<wbr></wbr></wbr>-->
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<ImageView<wbr></wbr>android:id="@+id/btnModule2"<wbr>android:src</wbr>="@android:drawable/ic_dialog_info"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginLeft</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="7dp"<wbr>android:layout_marginTop</wbr>="3dp"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginBottom</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="3dp"<wbr>android:layout_width</wbr>="wrap_content"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="wrap_content"<wbr></wbr>/>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<!--<wbr>功能模块按钮3<wbr></wbr></wbr>-->
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<ImageView<wbr></wbr>android:id="@+id/btnModule3"<wbr>android:src</wbr>="@android:drawable/ic_dialog_alert"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginLeft</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="7dp"<wbr>android:layout_marginTop</wbr>="3dp"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginBottom</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="3dp"<wbr>android:layout_width</wbr>="wrap_content"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="wrap_content"<wbr></wbr>/>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
</LinearLayout>
</LinearLayout>

 2.2  TestView.java


public<wbr></wbr>class<wbr>TestView<wbr></wbr></wbr>extends <wbr>ActivityGroup<wbr>{<br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>private<wbr>ScrollView<wbr>container<wbr></wbr></wbr></wbr>=<wbr></wbr>null ;
<wbr><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
protected<wbr></wbr>void
<wbr>onCreate(Bundle<wbr>savedInstanceState)<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>super .onCreate(savedInstanceState);
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
//<wbr>隐藏标题栏</wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>requestWindowFeature(Window.FEATURE_NO_TITLE);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>//<wbr>设置视图</wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>setContentView(R.layout.layout);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>= <wbr>(ScrollView)<wbr>findViewById(R.id.containerBody);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>//<wbr>模块1</wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>ImageView<wbr>btnModule1<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>= <wbr>(ImageView)<wbr>findViewById(R.id.btnModule1);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>btnModule1.setOnClickListener(</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>new <wbr>OnClickListener()<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>public<wbr></wbr>void <wbr>onClick(View<wbr>v)<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.removeAllViews();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.addView(getLocalActivityManager().startActivity(<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>"Module1" ,
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
new<wbr>Intent(TestView.</wbr>this,<wbr>ModuleView1.</wbr>class
)
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.getDecorView());<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>})<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
//<wbr>模块2</wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>ImageView<wbr>btnModule2<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>= <wbr>(ImageView)<wbr>findViewById(R.id.btnModule2);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>btnModule2.setOnClickListener(</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>new <wbr>OnClickListener()<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>public<wbr></wbr>void <wbr>onClick(View<wbr>v)<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.removeAllViews();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.addView(getLocalActivityManager().startActivity(<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>"Module2" ,
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
new<wbr>Intent(TestView.</wbr>this,<wbr>ModuleView2.</wbr>class
)
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.getDecorView());<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>});<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
//<wbr>模块3</wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>ImageView<wbr>btnModule3<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>= <wbr>(ImageView)<wbr>findViewById(R.id.btnModule3);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>btnModule3.setOnClickListener(</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>new <wbr>OnClickListener()<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>public<wbr></wbr>void <wbr>onClick(View<wbr>v)<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.removeAllViews();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.addView(getLocalActivityManager().startActivity(<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>"Module3" ,
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
new<wbr>Intent(TestView.</wbr>this,<wbr>ModuleView3.</wbr>class
)
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.getDecorView());<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>});<br><wbr><wbr><wbr><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

    代码说明:

      a).  ModuleView1、ModuleView2ModuleView3分别继承自Activity。

      b).  想动态改变标题可以通过cust_title获取TextView进行设置。 

注释:想代码实现将子activity的所用的layout加入到主activty中的layout

  public class FormActivity extends ActivityGroup {

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.form);

  LocalActivityManager m = getLocalActivityManager();

  Intent intent = new Intent().setClass(this, ContactFieldActivity.class);

  Window w = m.startActivity("tratat", intent);

  View v = w.getDecorView();

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

  container.addView(v);

  }

  }

<wbr>范例<wbr>2<wbr>ActivityGroup + GridView 实现Tab分页标签</wbr></wbr></wbr>

<wbr><wbr><wbr><a href="http://blog.csdn.net/hellogv/article/details/6057174" style="text-decoration:none; color:rgb(41,113,187)">http://blog.csdn.net/hellogv/article/details/6057174</a></wbr></wbr></wbr>

<wbr></wbr>

<wbr></wbr>2) tabActivity

<wbr><wbr><wbr>tabActivity继承自Activity,其内部定义好了TabHost,可以通过getTabHost()获取。TabHost 包含了两种子元素:一些可以自由选择的Tab 和tab对应的内容tabContentto,在Layout的&lt;TabHost&gt;下它们分别对应 TabWidget和FrameLayout。 <div>  使用TabActivity可以让同一个界面容纳更多的内容。我们将按照Standup Timer里的TeamDetailsActivity来讲述TabActivity的使用。在该例中,包含了两个Tab一个用于展示team的统计信息,一个用于展示team所参加的会议的列表(这是一个ListView)。</div> <div> <h2 style="margin:0px; padding:0px; border:0px; list-style:none; color:rgb(0,0,0); line-height:1.5"> <span style="word-wrap:normal; word-break:normal; line-height:normal; font-weight:normal; font-size:14px; border-collapse:separate">创建Layout<wbr></wbr></span> </h2> <div style="padding:0px; margin:0px">  这里需要注意的是不管你是使用TabActivity 还是自定义TabHost,都要求以TabHost作为XML布局文件的根。 <div style="border:1px solid rgb(204,204,204); padding:5px; overflow:auto; font-size:12px; margin:0px; word-break:break-all; font-family:'Courier new'; background-color:rgb(245,245,245)"> <pre style="white-space:normal; padding:0px; margin-top:0px; margin-bottom:0px; font-family:'Courier new'; word-wrap:break-word"></pre> <div style="padding:0px; margin:0px"> <span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&lt;?</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,255)">xml version="1.0" encoding="utf-8"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">?&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&lt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(128,0,0)">TabHost</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">xmlns:android</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="http://schemas.android.com/apk/res/android"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)"><br style="padding:0px; margin:0px"> android:id</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="@android:id/tabhost"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">android:layout_width</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="fill_parent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)"><br style="padding:0px; margin:0px"> android:layout_height</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="fill_parent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&lt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(128,0,0)">LinearLayout</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">android:orientation</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="vertical"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)"><br style="padding:0px; margin:0px"> android:layout_width</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="fill_parent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">android:layout_height</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="fill_parent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&lt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(128,0,0)">TabWidget</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">android:id</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="@android:id/tabs"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)"><br style="padding:0px; margin:0px"> android:layout_width</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="fill_parent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">android:layout_height</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="wrap_content"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">/&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&lt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(128,0,0)">FrameLayout</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">android:id</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="@android:id/tabcontent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)"><br style="padding:0px; margin:0px"> android:layout_width</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="fill_parent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">android:layout_height</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="fill_parent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)"><br style="padding:0px; margin:0px"><br style="padding:0px; margin:0px"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,128,0)">&lt;!--</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,128,0)">省略部分代码</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,128,0)">--&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)"><br style="padding:0px; margin:0px"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&lt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(128,0,0)">TextView</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">android:id</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="@+id/no_team_meetings"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)"><br style="padding:0px; margin:0px"> android:textSize</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="18sp"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">android:layout_width</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="fill_parent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)"><br style="padding:0px; margin:0px"> android:layout_height</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="fill_parent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">/&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)"><br style="padding:0px; margin:0px"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&lt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(128,0,0)">TextView</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">android:id</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="@+id/no_team_meeting_stats"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)"><br style="padding:0px; margin:0px"> android:textSize</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="18sp"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)">android:layout_width</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="fill_parent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(255,0,0)"><br style="padding:0px; margin:0px"> android:layout_height</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">="fill_parent"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">/&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&lt;/</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(128,0,0)">FrameLayout</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&lt;/</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(128,0,0)">LinearLayout</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&lt;/</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(128,0,0)">TabHost</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">&gt;</span> </div> </div> <p style="margin:5px auto; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal"> <wbr></wbr></p> </div> <div style="padding:0px; margin:0px">通常我们采用线性布局所以&lt;TabHost&gt; 的子元素是 &lt;LinearLayout&gt;。&lt;TabWidger&gt;对应Tab。&lt;FrameLayout&gt;则用于包含Tab需要展示的内容。需要注意的是&lt;TabWidger&gt; 和&lt;FrameLayout&gt;的Id 必须使用系统id,分别为android:id/tabs 和 android:id/tabcontent 。因为系统会使用者两个id来初始化TabHost的两个实例变量(mTabWidget 和 mTabContent)。</div> <h2 style="margin:0px; padding:0px; border:0px; list-style:none; color:rgb(0,0,0); line-height:1.5"> 编写Java代码</h2> <div style="padding:0px; margin:0px">  我们可以采用两种方法编写标签页:一种是继承TabActivity ,然后使用getTabHost()获取TabHost对象;第二种方法是使用自定的TabHost在布局文件上&lt;TabHost&gt;的自定义其ID,然后通过findViewById(),方法获得TabHost对象。</div> <div style="padding:0px; margin:0px">  本文中采用继承TabActivity的方法。</div> <div style="padding:0px; margin:0px"> <div style="border:1px solid rgb(204,204,204); padding:5px; overflow:auto; font-size:12px; margin:0px; word-break:break-all; font-family:'Courier new'; background-color:rgb(245,245,245)"> <pre style="white-space:normal; padding:0px; margin-top:0px; margin-bottom:0px; font-family:'Courier new'; word-wrap:break-word"></pre> <div style="padding:0px; margin:0px"> <span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">private</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">void</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">createTabs() {<br style="padding:0px; margin:0px"> TabHost tabhost</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">=</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">getTabHost();<br style="padding:0px; margin:0px"> tabhost.addTab(tabhost.newTabSpec(</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">stats_tab</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">).<br style="padding:0px; margin:0px"> setIndicator(</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">this</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">.getString(R.string.stats)).<br style="padding:0px; margin:0px"> setContent(createMeetingDetails(team)));<br style="padding:0px; margin:0px"><br style="padding:0px; margin:0px"> tabhost.addTab(tabhost.newTabSpec(</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">meetings_tab</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">).<br style="padding:0px; margin:0px"> setIndicator(</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">this</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">.getString(R.string.meetings)).<br style="padding:0px; margin:0px"> setContent(createMeetingList()));<br style="padding:0px; margin:0px"> getTabHost().setCurrentTab(</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">0</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">);<br style="padding:0px; margin:0px"> }</span> </div> </div> <p style="margin:5px auto; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal"> <wbr></wbr></p> Java代码中我们首先需要做的是获取TabHost对象,可以通过TabActivtiy里的getTabHsot()方法。如果是自定义TabHost,在添加Tabs前应该调用<span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><a href="http://developer.android.com/reference/android/widget/TabHost.html" target="_blank" style="text-decoration:none; color:rgb(41,113,187)">setUp()</a>方法。</div> <div style="padding:0px; margin:0px"> <pre style="white-space:normal; padding:0px; margin-top:0px; margin-bottom:0px; word-wrap:break-word"></pre> <div style="border:1px solid rgb(204,204,204); padding:5px; overflow:auto; font-size:12px; margin:0px; word-break:break-all; font-family:'Courier new'; background-color:rgb(245,245,245)"> <pre style="white-space:normal; padding:0px; margin-top:0px; margin-bottom:0px; font-family:'Courier new'; word-wrap:break-word"></pre> <div style="padding:0px; margin:0px"> <span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">mTabHost</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">=</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">(TabHost)findViewById(R.id.tabhost);<br style="padding:0px; margin:0px"> mTabHost.setup();<br style="padding:0px; margin:0px"> mTabHost.addTab(TAB_TAG_1,</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">Hello, world!</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">,</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">Tab 1</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">);</span> </div> </div> <pre style="white-space:normal">SDK上的原文:</pre> <pre style="white-space:normal; padding:0px; margin-top:0px; margin-bottom:0px; word-wrap:break-word"> <span style="word-wrap:normal; word-break:normal; line-height:19px; padding:0px; font-size:10pt; margin:0px"><span style="padding:0px; margin:0px"><span style="font-family:Courier New; word-wrap:normal; word-break:normal"> Call setup() before adding tabs if loading TabHost using findViewById().<wbr><span style="padding:0px; margin:0px">However<wbr></wbr></span>:<wbr></wbr></wbr></span></span></span><span style="padding:0px; margin:0px">You do not need to call setup() after getTabHost() in<wbr><code style="padding:0px; margin:0px"><a href="http://developer.android.com/reference/android/app/TabActivity.html" style="text-decoration:none; color:rgb(41,113,187)">TabActivity</a></code>.<wbr></wbr></wbr></span></pre> <span style="padding:0px; margin:0px"><br style="padding:0px; margin:0px"></span>  接着向TabHost添加tabs.即调用tabHost.addTab(TabSpec) 方法。<a href="http://developer.android.com/reference/android/widget/TabHost.TabSpec.html" target="_blank" style="text-decoration:none; color:rgb(41,113,187)">TabSpec</a>主要包含了setIndicator 和 setContent 方法,通过这两个方法来指定Tab 和 TanContent。</div> <div style="padding:0px; margin:0px">  TabSpec 通过<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px">.newTabSpec(String tag</span><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px">)来创建实例。实例化后对其属性进行设置。setIndicator()设置tab,它有3个重载的函数</span></wbr> </div> <div style="padding:0px; margin:0px"> <ul style="margin:0px 0px 0px 45px; padding:0px; border:0px; list-style-position:initial"> <li style="margin:0px; padding:0px; border:0px; list-style:disc"> public TabHost.TabSpec setIndicatior(CharSwquence label,Drawable icon).指定tab的标题和图标。</li> <li style="margin:0px; padding:0px; border:0px; list-style:disc">public TabHost.TabSpec (View view)通过View来自定义tab</li> <li style="margin:0px; padding:0px; border:0px; list-style:disc">public TabHost.TabSpec(CharSequence label) 指定tab的标题,此时无图标。</li> </ul> <div style="padding:0px; margin:0px">  <wbr>setContent 指定tab的展示内容,它也有3种重载</wbr> </div> </div> <div style="padding:0px; margin:0px"> <ul style="margin:0px 0px 0px 45px; padding:0px; border:0px; list-style-position:initial"> <li style="margin:0px; padding:0px; border:0px; list-style:disc">public TabHost.TabSpec setContent(TabHost.TabContentFactory )</li> <li style="margin:0px; padding:0px; border:0px; list-style:disc">public TabHost.TabSpec setContent(int ViewId)</li> <li style="margin:0px; padding:0px; border:0px; list-style:disc">public TabHost.TabSpec setContent(Intent intent)</li> </ul> <div style="padding:0px; margin:0px">  后两种方法比较后理解一个是通过 ViewId指定显示的内容,如.setContent(R.id.Team_EditText)。第三种则是直接通过Intent加载一个新的Activity页。如.setContent(new Intent(this, MeetingActivity.class)));</div> </div> <div style="padding:0px; margin:0px">  本例中是通过<a href="http://developer.android.com/reference/android/widget/TabHost.TabContentFactory.html" target="_blank" style="text-decoration:none; color:rgb(41,113,187)">TabContentFactory</a><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span>来指定对应的TabContent。<a href="http://developer.android.com/reference/android/widget/TabHost.TabContentFactory.html" target="_blank" style="text-decoration:none; color:rgb(41,113,187)">TabContentFactory</a><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span>是一个接口,其只包含了 一个返回 View 的createTabContent(String tag)方法。</div> <div style="padding:0px; margin:0px"> <div style="border:1px solid rgb(204,204,204); padding:5px; overflow:auto; font-size:12px; margin:0px; word-break:break-all; font-family:'Courier new'; background-color:rgb(245,245,245)"> <pre style="white-space:normal; padding:0px; margin-top:0px; margin-bottom:0px; font-family:'Courier new'; word-wrap:break-word"></pre> <div style="padding:0px; margin:0px"> <span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">private</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">TabContentFactory createMeetingDetails(Team team2) {<br style="padding:0px; margin:0px"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">return</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">new</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">TabHost.TabContentFactory() {<br style="padding:0px; margin:0px"><br style="padding:0px; margin:0px"> @Override<br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">public</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">View createTabContent(String tag) {<br style="padding:0px; margin:0px">           //设置View<br style="padding:0px; margin:0px"> setStatsTabContent();<br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">return</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">findViewById(R.id.teamStats);<br style="padding:0px; margin:0px"> }<br style="padding:0px; margin:0px"> };<br style="padding:0px; margin:0px"> }<br style="padding:0px; margin:0px"><br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">private</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">TabHost.TabContentFactory createMeetingList()<br style="padding:0px; margin:0px"> {<br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">return</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">new</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">TabHost.TabContentFactory() {<br style="padding:0px; margin:0px"><br style="padding:0px; margin:0px"> @Override<br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">public</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">View createTabContent(String tag) {</span> </div> <div style="padding:0px; margin:0px"> <span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">      <br style="padding:0px; margin:0px"> meetingListAdapter</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">=</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">createMeetingListAdapter<wbr>();<br style="padding:0px; margin:0px"> meetingList.setAdapter(meetingListAdapter);<br style="padding:0px; margin:0px"></wbr></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">return</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">meetingList;<br style="padding:0px; margin:0px"> }<br style="padding:0px; margin:0px"> };<br style="padding:0px; margin:0px"> }</span> </div> </div> <p style="margin:5px auto; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal"> <wbr></wbr></p> <div style="padding:0px; margin:0px"></div> 事先声明好的</div> <div style="padding:0px; margin:0px"> <div style="border:1px solid rgb(204,204,204); padding:5px; overflow:auto; font-size:12px; margin:0px; word-break:break-all; font-family:'Courier new'; background-color:rgb(245,245,245)"> <pre style="white-space:normal; padding:0px; margin-top:0px; margin-bottom:0px; font-family:'Courier new'; word-wrap:break-word"></pre> <div style="padding:0px; margin:0px"> <span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">private</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">ListView meetingList</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">=</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">null</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">;<br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">private</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">ArrayAdapter</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">&lt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">String</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">meetingListAdapter</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">=</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">null</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">;</span> </div> </div> <p style="margin:5px auto; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal"> <wbr></wbr></p> <div style="padding:0px; margin:0px">我们也可以让TabActivity去实现TabContentFactory 接口</div> <div style="padding:0px; margin:0px"> <div style="border:1px solid rgb(204,204,204); padding:5px; overflow:auto; font-size:12px; margin:0px; word-break:break-all; font-family:'Courier new'; background-color:rgb(245,245,245)"> <pre style="white-space:normal; padding:0px; margin-top:0px; margin-bottom:0px; font-family:'Courier new'; word-wrap:break-word"></pre> <div style="padding:0px; margin:0px"> <span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">public</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">class</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">Tabs2</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">extends</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">TabActivity</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">implements</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">TabHost.TabContentFactory</span> </div> </div> <p style="margin:5px auto; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal"> <wbr></wbr></p> 然后在TabActiviy类中实现createTabContent方法</div> <div style="padding:0px; margin:0px"> <div style="border:1px solid rgb(204,204,204); padding:5px; overflow:auto; font-size:12px; margin:0px; word-break:break-all; font-family:'Courier new'; background-color:rgb(245,245,245)"> <pre style="white-space:normal; padding:0px; margin-top:0px; margin-bottom:0px; font-family:'Courier new'; word-wrap:break-word"></pre> <div style="padding:0px; margin:0px"> <span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">@Override<br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">public</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">View createTabContent(String tag) {<br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">final</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">TextView tv</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">=</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">new</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">TextView(</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">this</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">);<br style="padding:0px; margin:0px"> tv.setText(</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">Content for tab with tag</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">+</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">tag);<br style="padding:0px; margin:0px"></span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,255)">return</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; margin:0px; color:rgb(0,0,0)">tv;<br style="padding:0px; margin:0px"> }</span> </div> </div> <p style="margin:5px auto; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal"> <wbr></wbr></p> </div> setStatsTabContent();方法</div> <div style="padding:0px; margin:0px"> <div style="border:1px solid rgb(204,204,204); padding:5px; overflow:auto; font-size:12px; margin:0px; word-break:break-all; font-family:'Courier new'; background-color:rgb(245,245,245)"> <img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" name="code_img_closed_2fabc5ab-8d5e-4559-b451-8019572189d4" alt="Android&lt;wbr&gt;&lt;wbr&gt;&lt;wbr&gt;ActivityGroup和TabActiviy的差异性" title="Android&lt;wbr&gt;&lt;wbr&gt;&lt;wbr&gt;ActivityGroup和TabActiviy的差异性" style="margin:0px; padding:0px 5px 0px 0px; border:0px; list-style:none; vertical-align:middle"><span style="word-wrap:normal; word-break:normal; line-height:1.5; border:1px solid rgb(128,128,128); padding:2px; margin:0px; background-color:rgb(255,255,255)">setStatsTabContent</span> </div> <p style="margin:5px auto; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal"> <wbr></wbr></p> </div> <div style="padding:0px; margin:0px">  最后将TabSpec 添加到 TabHost上,即tabHost.addTab(tabSpec)。我们发现TabSpec 的setIndicator 和 setContent 方法返回的都是 TabSpec 自身所以可以使用窜的方式编写代码:</div> <div style="border:1px solid rgb(204,204,204); padding:5px; overflow:auto; font-size:12px; margin:0px; word-break:break-all; font-family:'Courier new'; background-color:rgb(245,245,245)"> <h1 style="margin:0px; padding:0px; border:0px; list-style:none; color:rgb(0,0,0); line-height:1.5"> </h1> </div> <pre style="white-space:normal; padding:0px; font-size:12px; margin-top:0px; margin-bottom:0px; font-family:'Courier new'; word-wrap:break-word"></pre> <div style="padding:0px; margin:0px"> <span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; font-size:12px; margin:0px; color:rgb(0,0,0); font-family:'Courier new'">tabhost.addTab(tabhost.newTabSpec(</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; font-size:12px; margin:0px; color:rgb(0,0,0); font-family:'Courier new'">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; font-size:12px; margin:0px; color:rgb(0,0,0); font-family:'Courier new'">stats_tab</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; font-size:12px; margin:0px; color:rgb(0,0,0); font-family:'Courier new'">"</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; font-size:12px; margin:0px; color:rgb(0,0,0); font-family:'Courier new'">)<br style="padding:0px; margin:0px"> .setIndicator(</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; font-size:12px; margin:0px; color:rgb(0,0,255); font-family:'Courier new'">this</span><span style="word-wrap:normal; word-break:normal; line-height:1.5; padding:0px; font-size:12px; margin:0px; color:rgb(0,0,0); font-family:'Courier new'">.getString(R.string.stats))<br style="padding:0px; margin:0px"> .setContent(createMeetingDetails(team)));</span> </div> </div> </wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr> 3)两者之间的差异性

在一个主界面中做Activity切换一般都会用TabActivity,使用方便,Activity互相之间相对独立,但是可定制性不强,而且修改起来很麻烦。当然也可以把layout分开,把逻辑代码全写在主界面的逻辑代码中,但是很明显可维护性相当差,这里通过ActivityGroup来解决这个问题

你可能感兴趣的:(ActivityGroup)