Android中inflate和merge结合使用

原来自定义View时,使用inflate方法一直以为,是将布局文件的根节点作为View,今天偶然发现会将布局文件的根节点做为子View。

代码和层级如下:

public class MergeView extends LinearLayout {
	public MergeView(Context context) {
		super(context);
		init();
	}
	public MergeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	private void init() {
		inflate(getContext(), R.layout.layout_merge, this);
		
		TextView txt1 = (TextView) findViewById(R.id.txt1);
		TextView txt2 = (TextView) findViewById(R.id.txt2);
		TextView txt3 = (TextView) findViewById(R.id.txt3);
		txt1.setText("test1");
		txt2.setText("test2");
		txt3.setText("test3");
	}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal" 
    android:background="#ff0000">
    <TextView
        android:id="@+id/txt1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="title1"
        android:textSize="18dp" />
    <TextView
        android:id="@+id/txt2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="title2"
        android:textSize="18dp" />
    <TextView
        android:id="@+id/txt3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="title3"
        android:textSize="18dp" />
</LinearLayout>

第一个箭头,就是自定义LinearLayout

第二个箭头,就是布局文件的根节点

其实第二个节点,是多余的,所以可以使用merge标签来代替。

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal" 
    android:background="#ff0000">
    <TextView
        android:id="@+id/txt1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="title1"
        android:textSize="18dp" />
    <TextView
        android:id="@+id/txt2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="title2"
        android:textSize="18dp" />
    <TextView
        android:id="@+id/txt3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="title3"
        android:textSize="18dp" />
</merge>

再看下层级结构图

可以看到自定义LinearLayout下,没有多余节点了

注意:因为merge节点最终没有,所以设置在merge上的属性都没有用,需要在自定义View上设置。

看下使用merge后的效果,可以看到高度、颜色都没有了。

转载于:https://my.oschina.net/android520/blog/701445

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值