zoukankan      html  css  js  c++  java
  • 【转载】使用ActivityGroup来切换Activity和Layout

    一、效果图

         

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

      二、 实现代码

        2.1  layout.xml

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

    2.2  TestView.java

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/**
     * 使用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进行设置。

  • 相关阅读:
    python数据分析008_Matplotlib绘柱图,饼图,散点图
    python数据分析007_使用Matplotlib绘折线图
    python数据分析006_Python 2D绘图库Matplotlib
    python数据分析005_pandas的时间序列
    python数据分析004_多层索引的取值和排序
    python数据分析003_数据的合并筛选排序
    Megacli 简易使用
    k8s ingress 增加 跨域配置
    k8s 1.15 版本生产线上证书时间调整(亲测)
    grafana 展示 k8s prometheus
  • 原文地址:https://www.cnblogs.com/spmoon/p/3443234.html
Copyright © 2011-2022 走看看