近期对android里面控件修改做了很多实验,由于公司需求很多,不得不重写很多控件。程序目标无非是:高效、轻巧、清晰、标准化
完成动态加载Layout有两种方法,依据个人喜好进行选择:
方法1:静态主Layout动态加载静态子Layout
首先构建子Layout:main2
<? xml version = "1.0" encoding = "utf-8" ?>
<!--布局可以任意定义,此处拿线性布局举例,里面有2个按钮元素-->
< 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" >
<!--按钮1-->
< ImageButton android:id = "@+id/button1"
android:src = "@drawable/btn1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
> </ ImageButton >
<!--按钮2-->
< ImageButton android:id = "@+id/button2"
android:src = "@drawable/btn2"
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 {
/** Called when the activity is first created. */
// 子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 {
/** Called when the activity is first created. */
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的两种形式,里面可思考的很多,比如封装常用事件、资源,从而节省代码、节省资源;
抛砖引玉,分享经验,希望能助大家优化自己的程序。
转自:http://blog.csdn.net/wzrthhj/article/details/6570204
分享到:
相关推荐
Android中在xml中静态添加Fragment 在activity_main.xml中添加LeftFragment和RightFragment
一、Layout XML文件常见的两种模块加载方式 1、静态加载:被加载的模块和其它模块加载的时间一样。 <include layout=@layout/otherLayout/> 2、动态加载:需要被加载的模块初始时并没有被加载进内存,在你...
Fragment动态加载和静态加载
jsp 动态转静态 URL重写 jsp 动态转静态 URL重写 jsp 动态转静态 URL重写 jsp 动态转静态 URL重写
android jni 静态注册 动态注册 一些基本的操作 静态注册Eclipse 动态注册android studio
静态网页和动态网页的区别 动态网页和静态网页的区别,首先要分别了解两个概念,就是什么是静态网页,什么是动态网页,并且学会区分哪些是静态哪些是动态。 静态网页: (1)静态网页不能简单地理解成静止不动的...
静态库的制作:(inc/lib/src/main.c) gcc -c src/*.c(编译src目录下的.c文件生成.o文件) ...静态库的使用: gcc main.c -L ./lib -l math -I ./inc -o main(-L:指定库名所在路径,-l:指定库名,-I:指定头文件路径)
Android 静态加载fragment,参考博客: http://blog.csdn.net/u010661782/article/details/51790987
下载了官方的 tinyxml_2_6_2 的库,只...tinyxml_2_6_2动态库.rar (修改版) 以及包含了工程文件以及编译好的DLL了。 tinyxml_2_6_2静态库.zip (原版) tinyxml.dll tinyxmlD.dll tinyxmlSTL.dll tinyxmlSTLD.dll
android studio 简单demo实现将c++代码 编译成静态库和动态库 简单的makefile文件一看就明白
通过android studio 和ndk生成Android 平台的静态库,以及使用静态库demo
Android Framelayout显示静态和动态的扑克牌 包含Handler 和Timer TimerTask的使用方法
android实现静态ip设置,很实用的代码,android客户端ip静态设置源码
只要是qt在android开发中调用动态库和静态库的例子。
内容概要:Android平台使用的FFmpeg静态库版本4.4.2。支持RTP拉流 适用人群:Android平台做RTP拉流的开发人员。 其他说明:在网上没有找到android平台的FFmpeg库,就自己编译了一个,我用的事RTP拉流的功能,教程上...
Android Studio 基于cmake 链接静态库,动态库,博客地址:https://blog.csdn.net/dreams_deng/article/details/104540775
Android_FragmentExample- Android Fragment示例静态和动态
使用简单的程序展示了C++调用动态库和静态库的方法。 文件结构: exe:笔者部署可运行文件,因开发环境版本不同,可能存在无法直接使用的情况,两个部署文件(copy_ldd.sh及useLib1.sh)可以结合文章分享的(ubuntu下...
fragment设计哲理、静态、动态加载
Android 静态和动态画虚线Demo(开发工具:Android Studio)