2012年4月1日 星期日

[AndroidDev] merge

  • The <merge /> tag was created for the purpose of optimizing Android layouts by reducing the number of levels in view trees.
  • 如果 root layout 的大小設為和 parent 一樣 (ex: filll_parent)並且沒其他參數調整需求,那麼其實此 root layout 在實質上是沒有作用的,這時候便可以改以 merge 做為你的 root layout,這樣一來 children 便會直接被加在 merge's parent 上而能少拉一層 view。
    • Since our FrameLayout has the same dimension as its parent, by the virtue of using the fill_parent constraints, and does not define any background, extra padding or a gravity, it is totally useless.When the LayoutInflater encounters this tag, it skips it and adds the <merge /> children to the <merge /> parent. (The children will be added directly to the top-level FrameLayout.)
  • 但 merge 是不能用來取代 LinearLayout 的!
    • You could not apply this trick if your layout was using a LinearLayout as its root tag for instance. The <merge /> can be useful in other situations though.

  • The <merge /> tag is extremely useful and can do wonders in your code. However, it suffers from a couple of limitations:
    • <merge /> can only be used as the root tag of an XML layout
    • When inflating a layout starting with a <merge />, you must specify a parent ViewGroup and you must set attachToRoot to true (see the documentation for inflate(int, android.view.ViewGroup, boolean) method)


* Reference
- Layout Tricks: Merging Layouts | Android Developers
- Android里merge和include标签的使用_云开雾散_百度空间

沒有留言:

張貼留言