使用merge标签自定义View

通常自定义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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值