因为现在手头上做的需要显示很多不同布局,想着拆分开来不要全部都写到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. IQMath是什么 浮点转定点运算,dsp

    [转帖注明出处:blog.csdn.net/lanmanck] 网上搜了一下没发现非常合适的,特写出来与大家分享. 大家都知道嵌入式系统里带浮点运算指令的CPU都比較少,TI的DSP也是定点的廉价. ...

  2. [React] Create an Auto Resizing Virtualized List with react-virtualized

    In this lesson we'll show how to use the AutoSizer component from react-virtualized to automatically ...

  3. 关于浏览器不能执行JavaScrip问题的反思

    今天在一篇博客(http://blog.csdn.net/u011043843/article/details/27959563)的时候,写着用于演示的Javascript代码不能再浏览器执行,非常是 ...

  4. 关于stm32的输入输出

    https://blog.csdn.net/u011556018/article/details/72629082

  5. ng-cli搭建angular项目框架

    原文地址 https://www.jianshu.com/p/0a8f4b0f29b3 环境准备 以下步骤都不需要事先创建文件夹,只是环境的准备过程,只有到需要搭建项目的时候才需要创建文件夹用来存放项 ...

  6. cmder显示UTF-8字体

    https://blog.csdn.net/x_iya/article/details/50627829 WIN+ALT+P打开配置 搜索chcp chcp 65001

  7. [转载]Ocelot简易教程(六)之重写配置文件存储方式并优化响应数据

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9807125.html 很多人都说配置文件的配置很繁琐,如果存储在数据库就方便很多,可以通过自定义UI界面 ...

  8. Python 标准库和第三方库的安装位置、Python 第三方库安装的各种问题及解决

    首先使用 sys 下的 path 变量查看所有的 python 路径: import sys sys.path 标准库 lib 目录下(home 目录/pythonXX.XX/lib) 第三方库 在 ...

  9. Django表单上传

    任务描述:实现表单提交(上传文件) 1.项目目录: 2.源代码: regist.html <!DOCTYPE html> <html lang="en"> ...

  10. bash keys

    stty-a CTRL-S and CTRL-Q are called flow-control characters. Bash readline 使用技巧_Linux编程_操作系统_希赛网 htt ...