让我们想象我们正在基于LinearLayout开发复合组件。因此,我们创建如下类:
public class SomeView extends LinearLayout {
public SomeView(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(LinearLayout.VERTICAL);
View.inflate(context, R.layout.somelayout, this);
}
}
如果我们将LinearLayout
用作somelayout.xml
的根视图,那么我们会多出一个视图层级,因此我们使用merge
标签:
如果我们将LinearLayout
用作somelayout.xml
的根视图,会增加一个额外的视图层级。因此,我们使用merge
标记来避免这种情况:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
android:textSize="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some other text"/>
</merge>
但在IDE的预览选项卡中,合并操作始终作为FrameLayout,我们将看到类似这样的东西:
(这是Android Studio,Intellij IDEA也一样,至于Eclipse我不清楚)
预览大大加快了布局开发速度,失去这样一个极好的帮助甚至对某些布局来说都很遗憾。也许有一种方法可以指定预览应该如何解释特定布局中的merge
标签?