因为现在手头上做的需要显示很多不同布局,想着拆分开来不要全部都写到main.xml里,于是就想到动态加载Layout

目前试了下,

LinearLayout page = (LinearLayout)findViewById(R.id.page); //先找到父Layout的入口

然后:

View v2 = page.inflatethis, R.layout.son, null);
page.addView(v2);

或:

LayoutInflater inflater2 = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v3 = inflater2.inflate(R.layout.test_data, null);
page.addView(v3);

其实上面的做法是一样的,写法不同而已。

main里面的page,因为基本上用线性Layout就能解决问题,应该也可以用其他的吧:

<LinearLayout
android:id="@+id/page"
android:visibility="gone"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>

后来发现每次进去addView都会加一次,所以要么退出时 page.removeAllViews(); 要么先判断一下是否已经加载:

                TextView text = (TextView) v2.findViewById(R.id.tbd);
text.setText("Change!");
//或者:
TextView text2 = (TextView) page.findViewById(R.id.tbd);
text2.setText("Change 2!");

------------------------

转一篇别人写的总结,http://my.oschina.net/u/270164/blog/80838

小结:

一、  其他Layout共用一个layout

1.子布局适合大小;

2.主布局:<include layout="@layout/title"/>

3.调用:当前view .findViewByid得到子布局文件中的任意View 

二、静态主布局动态添加静态子布局

1.子布局适合大小;

2.主Layout要给子Layout设置一个容器box

3.// 子Layout要以view的形式加入到主Layout中 
private View mBarView; 
// 主Layout的容器加载子Layout的View 
private LinearLayout mLinearLayout;

// 加载子Layout 
mBarView = View.inflate(this, R.layout.main2, null); 
// 找到容器 
mLinearLayout = (LinearLayout)findViewById(R.id.box); 
// 加上View 结束 
mLinearLayout.addView(mBarView); 

 三、静态主布局动态加载子Layout

1.子布局适合大小;

2.构造layout类

//加载需要的属性,加载静态子Layout 
((Activity) getContext()).getLayoutInflater().inflate(R.layout.main2, this); 
//在此你可以封装很多方法

3.// 找到容器 
mLinearLayout = (LinearLayout)findViewById(R.id.box); 
// 实例化一个子View 
mMenuLandscapeLinearLayout=new MenuLandscapeLinearLayout(this); 
// 添加到容器 
mLinearLayout.addView(mMenuLandscapeLinearLayout);

 Android 在其他布局里共享同一布局文件
在任意LAYOUT文件里面将其他的LAYOUT文件拿过来使用,如下:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout

android:id="@+id/FrameLayout01"
        android:background="@drawable/layout_background"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

xmlns:android="http://schemas.android.com/apk/res/android">
   <TextViewandroid:text="欢迎你使用本软件"

android:id="@+id/TextView01"
       android:textColor="#f0f0f0"

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

子layout 文件起名为:title。然后我们在另外的布局文件如下使用:<include  layout="@layout/title"/> 即可将title 的布局直接拿到我们当前的布局文件中。
       使用include标记将layout 放入我们当前的layout文件,也可以直接使用当前view .findViewByid得到title 布局文件中的任意View 。

小结:子布局适合大小;

主布局:<include layout="@layout/title"/>

调用:当前view .findViewByid得到子布局文件中的任意View

动态加载Layout有两种方法:

方法1:静态主Layout动态加载静态子Layout

首先构建子Layout:main2 ( 子布局适合大小 )

<?xml version="1.0" encoding="utf-8"?> 
<!--布局可以任意定义,此处拿线性布局举例,里面有个按钮元素--> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/menubar" 
android:background="@drawable/menubar" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
<!--按钮--> 
<ImageButton android:id="@+id/button1" 
android:src="@drawable/btn1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
></ImageButton></LinearLayout>

然后构建主Layout:main

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/background" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/background"> 
<!--主Layout要给子Layout设置一个容器box,可以在此指定容器的位置,这段是关键部分--> 
<LinearLayout android:id="@+id/box" 
android:layout_alignParentBottom="true" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true"> 
</LinearLayout> 
</RelativeLayout>

最后在程序中加载子layout:

public class BackgroundTest extends Activity { 
// 子Layout要以view的形式加入到主Layout中 
private View mBarView; 
// 主Layout的容器加载子Layout的View 
private LinearLayout mLinearLayout; 

//给出关键内容 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
// 显示主Layout 
setContentView(R.layout.main); 
// 加载子Layout 
mBarView = View.inflate(this, R.layout.main2, null); 
// 找到容器 
mLinearLayout = (LinearLayout)findViewById(R.id.box); 
// 加上View 结束 
mLinearLayout.addView(mBarView); 

}

方法2:主Layout动态加载子Layout

首先构造你自己的子Layout和上面一样;

然后构建你自定义的Layout类:

public class MenuLandscapeLinearLayout extends LinearLayout{ 
// 构造函数 
public MenuLandscapeLinearLayout(Context context) { 
super(context); 
// TODO Auto-generated constructor stub 
//加载需要的属性,加载方法一的子Layout 
((Activity) getContext()).getLayoutInflater().inflate(R.layout.main2, this); 
//在此你可以封装很多方法 

}

最后在程序中动态实例化并加载即可:

public class BackgroundTest extends Activity { 
private LinearLayout mLinearLayout; 
//声明一个子Layout View对象 
private MenuLandscapeLinearLayout mMenuLandscapeLinearLayout; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
// 加载主Layout 
setContentView(R.layout.main); 
// 找到容器 
mLinearLayout = (LinearLayout)findViewById(R.id.box); 
// 实例化一个子View 
mMenuLandscapeLinearLayout=new MenuLandscapeLinearLayout(this); 
// 添加到容器 
mLinearLayout.addView(mMenuLandscapeLinearLayout); 


}

至此,完成了动态加载子Layout的两种形式,里面可思考的很多,比如封装常用事件、资源,从而节省代码、节省资源;

动态加载Layout的更多相关文章

  1. 动态加载Layout 与 论Activity、 Window、View的关系

    1)动态加载Layout的代码是 getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main, null)); ...

  2. Android在layout xml中使用ViewStub完成动态加载

    Android在layout xml中使用ViewStub完成动态加载 一.Layout XML文件常见的两种模块加载方式 1.静态加载:被加载的模块和其它模块加载的时间一样. <include ...

  3. Ext JS 如何动态加载JavaScript创建窗体

    JavaScript不需要编译即可运行,这让JavaScript构建的应用程序可以变得很灵活.我们可以根据需要动态从服务器加载JavaScript脚本来创建和控制UI来与用户交互.下面结合Ext JS ...

  4. 分享个刚写好的 android 的 ListView 动态加载类,功能全而代码少。

    (转载声明出处:http://www.cnblogs.com/linguanh/) 简介:      该ListView 实现动态加载数据,为了方便用户充分地自定义自己的数据源.点击事件,等核心操作, ...

  5. [转载] Android动态加载Dex机制解析

    本文转载自: http://blog.csdn.net/wy353208214/article/details/50859422 1.什么是类加载器? 类加载器(class loader)是 Java ...

  6. Android中插件开发篇之----动态加载Activity(免安装运行程序)

    一.前言 又到周末了,时间过的很快,今天我们来看一下Android中插件开发篇的最后一篇文章的内容:动态加载Activity(免安装运行程序),在上一篇文章中说道了,如何动态加载资源(应用换肤原理解析 ...

  7. Android中的动态加载机制

    在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...

  8. Android 动态加载 (二) 态加载机制 案例二

    探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法 重要说明 在实践的过程中大家都会发现资源引用的问题,这里重点声明两点: 1. 资源文件是不能直接inflate的,如果简单的话直接在程序 ...

  9. Android 动态加载 (一) 态加载机制 案例一

    在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...

随机推荐

  1. ZOJ 1914 Arctic Network (POJ 2349 UVA 10369) MST

    ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1914 POJhttp://poj.org/problem?id=23 ...

  2. MySQLDump在使用之前一定要想到的事情

    MySQLDump经常用于迁移数据和备份. 下面创建实验数据,两个数据库和若干表create database db1 ;use db1;create table t1(id int primary ...

  3. 快速杀死占用8080端口进程的批处理(kill-8080.bat)

    @echo off setlocal enabledelayedexpansion for /f "delims= tokens=1" %%i in ('netstat -aon ...

  4. 每天一个JavaScript实例-操作元素定位元素

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. gdb查看线程堆栈信息

    查看堆栈:gdb -quiet -batch -ex='thread apply all bt' -p pid查看运行位置:gdb -quiet -batch -ex='thread apply al ...

  6. keil编译后Program Size: Code=46284 RO-data=988 RW-data=580 ZI-data=1094588

    Program Size: Code=46284 RO-data=988 RW-data=580 ZI-data=1094588 Code      :   程序中代码所占字节大小 RO-data : ...

  7. 【44.10%】【codeforces 723B】Text Document Analysis

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【50.26%】【hdu 5907】Find Q

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) 问题描述 Byteasar迷恋上了 ...

  9. 【u124】环状最大两段子段和

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 给出一段环状序列,即认为A[1]和A[N]是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. ...

  10. 一个神奇的控件——Android CoordinatorLayout与Behavior使用指南

    CoordinatorLayout是support.design包中的控件,它可以说是Design库中最重要的控件. 本文通过模仿知乎介绍了自定义Behavior,通过模仿百度地图介绍了BottomS ...