展会信息港展会大全

使用 ActivityGroup 来切换 Activity 和 Layout
来源:互联网   发布日期:2016-01-14 10:07:23   浏览:1627次  

导读:在一个主界面中做Activity切换一般都会用TabActivity,使用方便,Activity互相之间相对独立,但是可定制性不强,而且修改起来 很麻烦。当然也可以把layout分开,把逻辑代码全写在主界面的逻辑代码中,但是很明显......

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

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

二、 实现代码

layout.xml

01

<?xml version="1.0" encoding="utf-8"?>

02

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

03

android:layout_width="fill_parent" android:orientation="vertical"

04

android:layout_height="fill_parent">

05

<LinearLayout android:gravity="center_horizontal"

06

android:background="@drawable/myinfor2"android:layout_width="fill_parent"

07

android:layout_height="wrap_content">

08

<TextView android:id="@+id/cust_title"android:textColor="@android:color/white"

09

android:textSize="28sp" android:text="模块1"android:layout_width="wrap_content"

10

android:layout_height="wrap_content"></TextView>

11

</LinearLayout>

12

<!-- 中间动态加载View -->

13

<ScrollView android:measureAllChildren="true"android:id="@+id/containerBody"

14

android:layout_weight="1" android:layout_height="fill_parent"

15

android:layout_width="fill_parent">

16

</ScrollView>

17

<LinearLayout android:background="@android:color/black"

18

android:layout_gravity="bottom" android:orientation="horizontal"

19

android:layout_width="fill_parent"android:layout_height="wrap_content">

20

<!-- 功能模块按钮1 -->

21

<ImageView android:id="@+id/btnModule1"android:src="@android:drawable/ic_dialog_dialer"

22

android:layout_marginLeft="7dp" android:layout_marginTop="3dp"

23

android:layout_marginBottom="3dp"android:layout_width="wrap_content"

24

android:layout_height="wrap_content" />

25

<!-- 功能模块按钮2 -->

26

<ImageView android:id="@+id/btnModule2"android:src="@android:drawable/ic_dialog_info"

27

android:layout_marginLeft="7dp" android:layout_marginTop="3dp"

28

android:layout_marginBottom="3dp"android:layout_width="wrap_content"

29

android:layout_height="wrap_content" />

30

<!-- 功能模块按钮3 -->

31

<ImageView android:id="@+id/btnModule3"android:src="@android:drawable/ic_dialog_alert"

32

android:layout_marginLeft="7dp" android:layout_marginTop="3dp"

33

android:layout_marginBottom="3dp"android:layout_width="wrap_content"

34

android:layout_height="wrap_content" />

35

</LinearLayout>

36

</LinearLayout>

TestView.java

01

/**

02

* 使用ActivityGroup来切换Activity和Layout

03

* @author 农民伯伯

04

* @version 2010-9-7

05

*

06

*/

07

public class TestView extends ActivityGroup {

08

09

private ScrollView container = null;

10

11

@Override

12

protected void onCreate(Bundle savedInstanceState) {

13

super.onCreate(savedInstanceState);

14

// 隐藏标题栏

15

requestWindowFeature(Window.FEATURE_NO_TITLE);

16

// 设置视图

17

setContentView(R.layout.layout);

18

19

container = (ScrollView) findViewById(R.id.containerBody);

20

21

// 模块1

22

ImageView btnModule1 = (ImageView) findViewById(R.id.btnModule1);

23

btnModule1.setOnClickListener(new OnClickListener() {

24

@Override

25

public void onClick(View v) {

26

container.removeAllViews();

27

container.addView(getLocalActivityManager().startActivity(

28

"Module1",

29

new Intent(TestView.this, ModuleView1.class)

30

.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))

31

.getDecorView());

32

}

33

});

34

35

// 模块2

36

ImageView btnModule2 = (ImageView) findViewById(R.id.btnModule2);

37

btnModule2.setOnClickListener(new OnClickListener() {

38

@Override

39

public void onClick(View v) {

40

container.removeAllViews();

41

container.addView(getLocalActivityManager().startActivity(

42

"Module2",

43

new Intent(TestView.this, ModuleView2.class)

44

.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))

45

.getDecorView());

46

}

47

});

48

49

// 模块3

50

ImageView btnModule3 = (ImageView) findViewById(R.id.btnModule3);

51

btnModule3.setOnClickListener(new OnClickListener() {

52

@Override

53

public void onClick(View v) {

54

container.removeAllViews();

55

container.addView(getLocalActivityManager().startActivity(

56

"Module3",

57

new Intent(TestView.this, ModuleView3.class)

58

.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))

59

.getDecorView());

60

}

61

});

62

}

63

}

赞助本站

人工智能实验室

相关热词: ActivityGroup 切换 Activity Layo

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港