-viewStub Android Layout of labels, requestFocus, merge, include

When defining Android Layout (XML), there are four more special label is very important, of which three are related to the reuse of resources, namely <viewStub />, <requestFocus />, <merge /> and <include / >. But in the past the cases we have contacted example or official documents are not focused to introduce the importance of these labels.

  • <ViewStub /> : This tag allows UI in exceptional cases, visual effects similar to setting View invisibility, but the larger (R) meaning that the label be wrapped Views do not take up the default any memory space. Views viewStub introduced from the outside through elements include.
    • Usage: The android: layout to specify the content contained. By default, ViewStub contains tags belonging visibility = GONE. viewStub by the method inflate () system call to load its Views inside.
      <ViewStub android:id="@+id/stub"
      android:inflatedId="@+id/subTree"
      android:layout="@layout/mySubTree"
      android:layout_width="120dip"
      android:layout_height="40dip" />
  • <the include /> : external XML can be loaded directly into the label by the present structure is a common label multiplexing UI resource.
    • Usage: the need to re-endow Layout property include labels with xml file path.
      <include android:id="@+id/cell1" layout="@layout/ar01" />
      <include android:layout_width="fill_parent" layout="@layout/ar02" />
  • <the requestFocus /> : View tab for focus within the specified screen.
    • Usage: The label is placed inside the Views tab <- Easy AdSenser V2.37 -!> <- Post [count: 2] -!>
      <EditText id="@+id/text"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:layout_weight="0"
                   android:paddingBottom="4">
             <requestFocus />
      </EditText>
  • <merge />:

    Separately <merge /> tag to be introduced, because it plays an important role in optimizing the structure UI. Object is achieved by the deletion of the extra or additional levels to optimize the structure of the entire Android Layout.

    To understand the role of the label produced by a practical example, this can be more intuitive understanding <merge /> usage.

    Build a simple Layout, Views which contains two elements: the ImageView and TextView  By default we put these two elements in FrameLayout. The effect is a picture full screen in the main view, after which the title appears on the picture, and positioned below the view. The following is the code xml:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
    
            android:scaleType="center"
            android:src="@drawable/golden_gate" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dip"
            android:layout_gravity="center_horizontal|bottom"
    
            android:padding="12dip"
    
            android:background="#AA000000"
            android:textColor="#ffffffff"
    
            android:text="Golden Gate" />
    
    </FrameLayout>

    Layout view applications running on top of:

    Start tools> hierarchyviewer.bat tool to view the current UI structure view:

    layouttutorial_merge_01

    layouttutorial_merge_02

    We can clearly see that there were two framelayout nodes by the red frame structure contained, it is clear that these two identical full sense of the node caused the waste of resources (here we can remind customary in the development project by hierarchyViewer view the current resource allocation UI), then how can we solve this problem it (on the current example is how to get rid of excess frameLayout node)? This time is necessary to use <merge /> tag to deal with a similar problem. We will top xml code framLayout replace merge:

    <!– Easy AdSenser V2.37 –><!– Post[count: 2] –>

    <merge xmlns:android="http://schemas.android.com/apk/res/android">
    
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
    
            android:scaleType="center"
            android:src="@drawable/golden_gate" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dip"
            android:layout_gravity="center_horizontal|bottom"
    
            android:padding="12dip"
    
            android:background="#AA000000"
            android:textColor="#ffffffff"
    
            android:text="Golden Gate" />
    
    </merge>

    After running the program results shown in the Emulator is the same, but by viewing the UI hierarchyviewer structure is changed, the original excess FrameLayout nodes are merged together, or can be understood as a subset of the tag is directly applied to merge Activity under the FrameLayout with node (It should draw attention to: the root of all the Activity view is frameLayout). If you do not use the created Layout framLayout as the root node (but the application LinerLayout such as the definition of root tag), the example above can not be applied to optimize the UI structure through merge.

    layouttutorial_merge_03

    In addition to the above example, meger there is another usage

    Include when applied or introduced ViewStub xml tag structure from the outside, may be introduced as a root node represents xml merge with, so that when the structure is embedded in the parent well can be fused to it it contains a subset of the parent structure, redundant nodes does not appear.

    Two additional points need special attention:

  • <Merge /> can only be used as the root xml layout.
  • When it is desired to expand by itself merge xml layout as the root node, then, you need to be introduced xml layout placed viewGroup while attachToRoot needs to be set to True. (See further instructions inflate () documentation)

Reproduced in: https: //www.cnblogs.com/bill-joy/archive/2012/06/27/2565984.html

Guess you like

Origin blog.csdn.net/weixin_33920401/article/details/94048926