使用merge标签自定义View
来源:互联网 发布:免越狱安装软件 编辑:程序博客网 时间:2024/05/17 04:17
通常自定义View有三种方式
1.继承自View,处理手势和View的测量绘制
2.继承ViewGroup,处理子View测量Measure,布局Layout,绘制和手势控制
3.使用系统组件View通过组合实现需要的功能组件,一般是直接继承基本容器布局,在其中添加组件
最近项目中用到基本表单布局,因为后期可能要做动态表单,因此想先把各种表单项抽成独立的组件,之前实现是直接继承LinearLayout,在构造函数中通过inflate布局文件获得view,再通过addView添加view
public class FormItem1 extends LinearLayout{ public FormItem1(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { View v= LayoutInflater.from(getContext()).inflate(R.layout.form_item1,null); LinearLayout.LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); addView(v,params); }}form_item1.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:weightSum="1"> <TextView android:id="@+id/textView" android:layout_width="76dp" android:layout_height="wrap_content" android:layout_weight="0.06" android:gravity="center" android:singleLine="true" android:text="表单项" /> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="match_parent" android:hint="请填入信息" android:background="@null" android:layout_weight="0.06" /></LinearLayout>
上面的实现实际上是多嵌套了一层LinearLayout,在XML布局中我们是可以使用merge标签和include标签配合,那么动态添加View呢,
只要一点小改动布局文件使用merge标签替换LinearLayout
修改init
private void init() { View v= LayoutInflater.from(getContext()).inflate(R.layout.form_item1,this); }
看一下LayoutInflater代码
/** * Inflate a new view hierarchy from the specified xml resource. Throws * {@link InflateException} if there is an error. * * @param resource ID for an XML layout resource to load (e.g., * <code>R.layout.main_page</code>) * @param ***root Optional view to be the parent of the generated hierarchy.*** * @return The root View of the inflated hierarchy. If root was supplied, * this is the root View; otherwise it is the root of the inflated * XML file. */ public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) { return inflate(resource, root, root != null); }
root不为null的话,会将root作为inflate生成的View的父布局
0 0
- 使用merge标签自定义View
- 自定义View与merge节点
- 自定义view--打折标签view
- Merge标签使用
- <merge>标签使用
- 自定义标签View
- 为何要使用<merge\>标签
- 使用merge标签优化布局
- android <merge>标签的使用
- 为何要使用<merge\>标签
- include、merge、ViewStub 标签使用
- 自定义view之热门标签
- android自定义view 流式标签View
- 自定义View的使用
- 自定义view的使用
- android 自定义View使用
- 使用xib自定义 view
- 自定义View详细使用
- 链表中倒数第k个结点
- win7+qt4+qtcreator+OpenCV 2.4.10的开发环境搭建
- Android性能优化总结
- Angularjs留存(一)
- 要么读书,要么健身,要么恋爱(三)城南旧事
- 使用merge标签自定义View
- 各种数据库信息查看命令
- zynq学习06 zynq的PL中加入的AXI_GPIO IP核来控制GPIO操作
- html/css网站demo实战
- 119. PHP 性能问题(2)
- apache Kafka Replication设计分析
- Struts2 入门 初步了解Struts2(3) Copy文件时的注意事项
- 备忘
- JAVAEE基础知识点