[Android development tips] layout optimization tools <include/> and ViewStub, androidviewstub
"For original works, please indicate the source for reprinting. --- Sun Guowei 』
[Original article address: http://blog.csdn.net/manoel/article/details/39036507 〕
When creating complex la S, you may find that many viewgroups and views are added. The problem is that the View tree is getting deeper and deeper, and the application is getting slower and slower, because UI rendering is very time-consuming.
At this time, we should optimize the layout. The following two methods are described: <include> label and ViewStub class.
<Include/>
<Include/> is used to avoid code duplication. Imagine that we need to add a footer for each view in the app, which is a TextView that displays the app name. Generally, multiple activities correspond to multiple XML layout files. Do you want to copy this TextView to each XML file? If TextView needs to be modified, it is a nightmare to modify every XML layout file.
One idea of object-oriented programming is code reuse. How can we reuse the layout? <Include/> takes effect.
If you have learned the C language, you should be familiar with # include. It is a pre-compiled command. Before the program is compiled into a binary file, the # include content is copied to the # include position.
<Include/> in Android can also be understood as copying some common xml code to the location where <include/> is located. Take an Activity as an example.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center_horizontal" android:text="@string/hello" /> <include layout="@layout/footer_with_layout_properties"/></RelativeLayout>
Footer_with_layout_properties.xml is a simple TextView. The Code is as follows:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp" android:gravity="center_horizontal" android:text="@string/footer_text" />
In the above Code, we use the <include/> label to achieve code reuse.
However, there are still some doubts.
Footer_with_layout_properties.xml uses the android: layout_alignParentBottom attribute. This attribute is feasible because the outer layout is RelativeLayout.
So what if the outer layout is changed to LinearLayout? The answer is obvious. So what should we do? You can write the specific attributes in the <include/> label and view the following code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center_horizontal" android:text="@string/hello"/> <include layout="@layout/footer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp"/></RelativeLayout>
The android: layout _ * attribute is directly used in the <include/> label.
Note: If you want to overwrite any android: layout _ * attribute specified by the contained layout in the <include/> label, the layout_width and layout_height attributes must be specified in the <include/> label. This may be a bug in the Android system.
ViewStub
During development, various interaction problems are inevitable, such as displaying or hiding a view. If you want to display a view only when needed, you can try to use the ViewStub class.
Let's take a look at the official introduction of ViewStub:
"ViewStub is an invisible view with a size of 0, which can be delayed until the runtime fills in the layout resources. When ViewStub is set to Visible or inflate () is called, the layout resource is filled and ViewStub is replaced by the filled view ".
Now that we know what ViewStub can do, let's look at an example. A MapView exists in a layout and is displayed only when it is needed.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:onClick="onShowMap" android:text="@string/show_map" /> <ViewStub android:id="@+id/map_stub" android:layout_width="fill_parent" android:layout_height="fill_parent" android:inflatedId="@+id/map_view" android:layout="@layout/map" /></RelativeLayout>
The map. xml file contains a MapView, which is displayed only when necessary.
<com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="my_api_key" android:clickable="true" />
In addition, inflatedId is the id returned after ViewStub is set to Visible or the inflate () method is called. This id is the id of the filled View. In this example, it is the id of MapView.
Next, let's take a look at how ViewStub works.
public class MainActivity extends MapActivity { private View mViewStub; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mViewStub = findViewById(R.id.map_stub); } public void onShowMap(View v) { mViewStub.setVisibility(View.VISIBLE); } @Override protected boolean isRouteDisplayed() { return false; }}
Digress
Some may ask, what is the difference between using ViewStub and simply setting the View as View. GONE or View. VISIBLE? Isn't it all show and hide? Using ViewStub is more troublesome.
There is indeed a difference that involves rendering the View tree and memory consumption.
For specific differences, please go to Google. As the saying goes, do it yourself!
References
Http://code.google.com/p/android/issues/detail? Id = 2863
Http://android-developers.blogspot.com.ar/2009/03/android-layout-tricks-3-optimize-with.html
Http://developer.android.com/reference/android/view/ViewStub.html