Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55227
  • 博文数量: 8
  • 博客积分: 188
  • 博客等级: 入伍新兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-30 18:31
文章分类

全部博文(8)

文章存档

2011年(8)

我的朋友

分类: Java

2011-05-25 10:34:51

[Android]使用ActivityGroup来切换Activity和Layout



前言

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





声明

  欢迎转载,但请保留文章原始出处:)

    博客园:http://www.cnblogs.com
    农民伯伯: http://www.cnblogs.com/over140/

正文

  一、效果图

    

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



  二、 实现代码

    2.1  layout.xml

android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="fill_parent">
android:background="@drawable/myinfor2" android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:textSize="28sp" android:text="模块1" android:layout_width="wrap_content"
android:layout_height="wrap_content">



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

android:layout_gravity="bottom" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">

android:layout_marginLeft="7dp" android:layout_marginTop="3dp"
android:layout_marginBottom="3dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

android:layout_marginLeft="7dp" android:layout_marginTop="3dp"
android:layout_marginBottom="3dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

android:layout_marginLeft="7dp" android:layout_marginTop="3dp"
android:layout_marginBottom="3dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" />



    2.2  TestView.java
/**
* 使用ActivityGroup来切换Activity和Layout
* @author 农民伯伯
* @version 2010-9-7
*
*/
public class TestView extends ActivityGroup {

private ScrollView container = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 设置视图
setContentView(R.layout.layout);

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

// 模块1
ImageView btnModule1 = (ImageView) findViewById(R.id.btnModule1);
btnModule1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
container.removeAllViews();
container.addView(getLocalActivityManager().startActivity(
"Module1",
new Intent(TestView.this, ModuleView1.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
}
});

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

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

    代码说明:

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

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

  ScrollView卷轴视图是指当拥有很多内容,一屏显示不完时,需要通过滚动跳来显示的视图.ScrollView也是一个Layout布局,可以让它内部的数据显示不下的时候出现滚动条,要注意的是不能在ScrollView中放多个组件,如果放了多个组件,会出现如下错误:ERROR/AndroidRuntime(271): Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child (ScrollView只能包裹一个直接子元素)
阅读(2633) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~