Android中对于布局的复用
2015-10-30 20:12
525 查看
在Android中,对于布局的复用,一般有三种:
一种是使用用代码写自定义控件,
一种是在xml布局文件中写布局,然后在要用到相同布局的时候,使用include 来引用布局;例如:
还有一种就是在xml中写好布局,然后在代码中写自定义布局的时候,使用LayoutInflater加载布局.
如:
一种是使用用代码写自定义控件,
一种是在xml布局文件中写布局,然后在要用到相同布局的时候,使用include 来引用布局;例如:
<include android:id="@+id/mylayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/myView1" layout="@layout/myview_layout" android:layout_marginTop="35dp" />
还有一种就是在xml中写好布局,然后在代码中写自定义布局的时候,使用LayoutInflater加载布局.
如:
package cn.com.cheng.layout_demo; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.RelativeLayout; public class MyLayoutView extends RelativeLayout{ public MyLayoutView(Context context) { this(context,null); } public MyLayoutView(Context context, AttributeSet attrs) { this(context, attrs,0); } public MyLayoutView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.myview_layout, this); } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories