动态加载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的两种形式,里面可思考的很多,比如封装常用事件、资源,从而节省代码、节省资源;
最新文章
- [LeetCode] Contains Duplicate II 包含重复值之二
- IOS第七天(1:UiTableView 的基本用法)
- 并行开发——Parallel的使用 -摘自网络
- Android CTS測试Fail项改动总结(四)
- CDH集群搭建部署
- [Swift]LeetCode753. 破解保险箱 | Cracking the Safe
- String Formatting in C#
- Docker学习笔记-Redis 安装
- selenium 校验文件下载成功
- CentOS7.5安装Tomcat8
- js动态添加删除行,兼容ie和火狐
- Zabbix 监控 Cisco ASA5525 流量
- 性能调优之MySQL篇四:MySQL配置定位以及优化
- 使用svn 在 github上下载单个文件夹?
- asp.net MVC中实现调取web api
- WCF部署到IIS的一个浅水滩
- WebGL画点程序v3
- php模拟并发
- Scrum敏捷开发过程
- php使用flock堵塞写入文件和非堵塞写入文件
热门文章
- [TypeScript] Using Assertion to Convert Types in TypeScript
- [array] leetCode-16. 3Sum Closest -Medium
- Multivariate Linear Regression
- libevent源码分析-介绍、安装、使用
- php实现包含min函数的栈(这个题目用另外一个栈做单调栈的话时间复杂度会低很多)
- event.relatedTarget、event.fromElement、event.toElement
- linux中竖线&#39;|&#39;,双竖线‘||’,&;和&;&;的意思
- ios开发瀑布流框架的应用
- 【u018】电车
- 【转】priority_queue的用法