通常自定义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的父布局