动态加载Layout
因为现在手头上做的需要显示很多不同布局,想着拆分开来不要全部都写到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的更多相关文章
- 动态加载Layout 与 论Activity、 Window、View的关系
1)动态加载Layout的代码是 getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main, null)); ...
- Android在layout xml中使用ViewStub完成动态加载
Android在layout xml中使用ViewStub完成动态加载 一.Layout XML文件常见的两种模块加载方式 1.静态加载:被加载的模块和其它模块加载的时间一样. <include ...
- Ext JS 如何动态加载JavaScript创建窗体
JavaScript不需要编译即可运行,这让JavaScript构建的应用程序可以变得很灵活.我们可以根据需要动态从服务器加载JavaScript脚本来创建和控制UI来与用户交互.下面结合Ext JS ...
- 分享个刚写好的 android 的 ListView 动态加载类,功能全而代码少。
(转载声明出处:http://www.cnblogs.com/linguanh/) 简介: 该ListView 实现动态加载数据,为了方便用户充分地自定义自己的数据源.点击事件,等核心操作, ...
- [转载] Android动态加载Dex机制解析
本文转载自: http://blog.csdn.net/wy353208214/article/details/50859422 1.什么是类加载器? 类加载器(class loader)是 Java ...
- Android中插件开发篇之----动态加载Activity(免安装运行程序)
一.前言 又到周末了,时间过的很快,今天我们来看一下Android中插件开发篇的最后一篇文章的内容:动态加载Activity(免安装运行程序),在上一篇文章中说道了,如何动态加载资源(应用换肤原理解析 ...
- Android中的动态加载机制
在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...
- Android 动态加载 (二) 态加载机制 案例二
探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法 重要说明 在实践的过程中大家都会发现资源引用的问题,这里重点声明两点: 1. 资源文件是不能直接inflate的,如果简单的话直接在程序 ...
- Android 动态加载 (一) 态加载机制 案例一
在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...
随机推荐
- P2P平台公司的9种职位
1.技术 开发类:开发系统,实现新功能 维护类:服务器和网站维护 更多:Web前端等 观点:技术岗位很关键,但是很多不懂技术的老板,不会认可你的价值. 太多的老板,以为技术 ...
- SiFive Unleashed启动
SiFive Unleashed启动 请仔细参看SiFive官网的文档HiFive Unleashed 使用串口连接过程 连接好硬件(电源+USB) 尝试打开电源键,检测硬件能被识别 配置minico ...
- php BC高准确度函数库
<? php *************************************************************************************** *p ...
- Swift3.0 功能二 (表情键盘与图文混排)
随着iOS越来越多表情键盘以及图文混排的需求,本文运用Swift3.0系统的实现其功能以及封装调用方法,写的不好,如有错误望各位提出宝贵意见,多谢 项目源码地址: 相关知识点都有标识 项目源码地址 废 ...
- JAVA Concurrent包 中的并发集合类
我们平时写程序需要经常用到集合类,比如ArrayList.HashMap等,但是这些集合不能够实现并发运行机制,这样在服务器上运行时就会非常的消耗资源和浪费时间,并且对这些集合进行迭代的过程中不能进行 ...
- php压缩
php压缩的一个demo,随便测试了一下,可以用 <?php class PHPZip { private $ctrl_dir = array(); private $datasec = arr ...
- itunes app 下载链接的几种表现形式
第一种:itunes://itunes.apple.com/cn/app/id794862904 ,这是最普通的一种. 直接在浏览器中输入.就能够打开电脑上安装的itunes,并跳转到相应的app下载 ...
- 将HTML格式的String转化为HTMLElement 分类: C1_HTML/JS/JQUERY 2014-08-05 12:01 1217人阅读 评论(0) 收藏
代码如下: <meta charset="UTF-8"> <title>Insert title here</title> </head& ...
- Android CursorAdapter的使用详解
一.CursorAdapter介绍 CursorAdapter这个类是继承于BaseAdapter的它是一个虚类它为Cursor和ListView连接提供了桥梁 二.CursorAdapter详解 1 ...
- TortoiseGit拉取或推送项目提示 HTTP Basic: Access denied fatal: Authentication failed.
TortoiseGit拉取或推送项目提示 HTTP Basic: Access denied fatal: Authentication failed. 大体意思是,HTTP基本认证失败,访问被拒 ...