Layout Tricks
Layout Tricks: Creating Reusable UI Components
The
<include /> element does exactly what its name suggests; it includes another XML layout. Using this tag is straightforward as shown in the following example, taken straight from the source code of the Home application that ships with Android:Layout Tricks: Using ViewStubs
AViewStub is a dumb and lightweight view. It has no dimension, it does not draw anything and does not participate in the layout in any way. This means that aViewStub is very cheap to inflate and very cheap to keep in a view hierarchy. A ViewStub can be best described as a lazy include. The layout referenced by aViewStub is inflated and added to the user interface only when you decide so.Layout Tricks: Merging Layouts
The<merge /> tag was created for the purpose of optimizing Android layouts by reducing the number of levels in view trees. When the
LayoutInflater encounters this tag, it skips it and adds the <merge /> children to the <merge />parent.Obviously, using
<merge /> works in this case because the parent of an activity's content view is always a FrameLayout. 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. For instance, it works perfectly when combined with the <include /> tag. You can also use <merge /> when you create a custom composite view. 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 parentViewGroupand you must setattachToRoottotrue(see the documentation forinflate(int, android.view.ViewGroup, boolean)method)
浙公网安备 33010602011771号