要理解这一点,您需要了解布局是如何膨胀和放置的。
例如,假设您有一个活动,这是您使用的布局 xml。这是放置布局文件之前活动的布局。
<FrameLayout> // This is the window
<FrameLayout> // This is activity
</FrameLayout>
</FrameLayout>
取决于设备/操作系统,可能有很少的其他层。
现在,当您扩充布局文件并将其放入时,这就是它的外观。
<FrameLayout> // This is the window
<FrameLayout> // This is activity
//A textview and 2 linearlayouts
</FrameLayout>
</FrameLayout>
你看到另一个FrameLayout里面的FrameLayout了吗?拥有它是多余的,因为它不会增加太多价值。要进行优化,您可以将外部FrameLayout 替换为<merge>
标签。这就是它的样子。
<merge> // This is the window
<FrameLayout> // This is activity
//A textview and 2 linearlayouts
</FrameLayout>
</merge>
注意没有额外的 FrameLayout。相反,它只是与活动的 FrameLayout 合并。只要有可能,您应该使用<merge>
. 这不仅适用于 FrameLayouts。你可以在这里读更多关于它的内容。http://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge
希望这可以帮助。