因为现在手头上做的需要显示很多不同布局,想着拆分开来不要全部都写到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. 【习题 5-6 UVA-1595】Symmetry

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每一个y坐标的点都找中点. 看看中点是不是都一样就好. [代码] #include <bits/stdc++.h> us ...

  2. 17、MJPG编码和AVI封装

    一.JPEG和MJPG编码介绍 1.JPEG编码 我个人简单的理解是,JPEG即是Joint Photographic Experts Group(联合图像专家组)的缩写,更是一种图像压缩编码算法.J ...

  3. ActiveMQ简单入门实例

    一.下载MQ 官方网站下载:http://activemq.apache.org/ 我用的是 apache-activemq-5.15.0-bin 二.安装 我用的是64位所以双击 apache-ac ...

  4. 【66.47%】【codeforces 556B】Case of Fake Numbers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【Lucene4.8教程之三】搜索 2014-06-21 09:53 1532人阅读 评论(0) 收藏

    1.关键类 Lucene的搜索过程中涉及的主要类有以下几个: (1)IndexSearcher:执行search()方法的类 (2)IndexReader:对索引文件进行读操作,并为IndexSear ...

  6. html js 上传图片 预览

    第一段代码是我自己修改之后能够多选显示多预览图,也就是加了个循环,后面的代码是一些解释说明之类的,抄的 <!DOCTYPE html> <html> <head> ...

  7. 【63.73%】【codeforces 560A】Currency System in Geraldion

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. [CSS] Showing horizontal scrollbar always for the table

    table { display: block; overflow: scroll; width: 200px; height:95vh; }

  9. 设置好ftp后用xftp连接提示无法打开,无法显示远程文件夹

    原文:设置好ftp后用xftp连接提示无法打开,无法显示远程文件夹 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/the_victory/artic ...

  10. 【icpc网络赛大连赛区】Sparse Graph

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submissi ...