Three big tabs for Android layout optimization

Source: Internet
Author: User
<span id="Label3"></p>1. Layout Reuse<code><code><include /></code></code> <ul> <ul> <li>The label can reuse the layout file, which is simple to use as Follows:</li> </ul> </ul><pre class="prettyprint"><pre class="prettyprint"><code class="hljs r"><linearlayout xmlns:android=<span class="hljs-string"> "http:/ /schemas.android.com/apk/res/android "</span> android:orientation=<span class=" hljs-string ">" Vertical "</span> an Droid:layout_width= "match_parent" android:layout_height= "match_parent" android:background=<span class="hljs-stri ng ">" @color/app_bg "</span> android:gravity=<span class=" hljs-string ">" center_horizontal "</span> > <include Layout=<span class="hljs-string"> "@layout/titlebar" </span>/> <textview android:layout_width= "match_parent" Android:layout_height=<span class="hljs-string"> "wrap_content" </span> Android:text=<span class="hljs-string"> "@string/hello" </span> Android:padding=<span class="hljs-string"> "10dp" </span>/> <sp an class="hljs-keyword"> ... </LinearLayout> </sp></code> </pre></pre> <ul> <ul> <li>1) the <code><include /></code> label can use a separate layout property, which is also required.</li> <li>2) other properties can be Used. <code><include /></code>If the tag specifies the id attribute and your layout defines the id, then the ID of your layout will be overwritten with the Solution.</li> <li>3) all android:layout_* in the include tag are valid, provided that you have to write Layout_width and layout_height two Properties.</li> <li>4) The layout can contain two identical include tags, which can be resolved using the following methods (reference):</li> </ul> </ul><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs avrasm">View bookmarks_container_2 = findViewById(R<span class="hljs-preprocessor">.id</span><span class="hljs-preprocessor">.bookmarks</span>_favourite)<span class="hljs-comment">; </span>bookmarks_container_2<span class="hljs-preprocessor">.findViewById</span>(R<span class="hljs-preprocessor">.id</span><span class="hljs-preprocessor">.bookmarks</span>_list)<span class="hljs-comment">; </span></code></pre></pre>2. Reduce the view level<code><code><ViewStub /></code></code>As the name implies, is the meaning of merging and merging. Using it can effectively optimize some of the extra levels that match the Conditions. There are two main places to use the Merge:<pre><pre><code>(1) 自定义View中使用,父元素尽量是FrameLayout,当然如果父元素是其他布局,而且不是太复杂的情况下也是可以使用的 (2) Activity中的整体布局,根元素需要是FrameLayout下面这个例子将融合这两种情况,来展示如何缩减布局层次。总体显示界面:</code></pre></pre><p><p></p></p> <ul> <ul> <li><p>Where the pink circle is part of our custom VIEW.</p></li> <li><p>The layout of the entire interface is layout_mergedemo.xml as Follows:</p></li> </ul> </ul><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">framelayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute"> Xmlns:tools</span>=<span class="hljs-value">"http://schemas.android.com/tools"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"fill_parent" </span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"fill_parent"</span>> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">textview </span> <span class="hljs-attribute">android:layout _width </span> =<span class="hljs-value"> "fill_parent" </span> <span class="hljs-attribute">android:layout_height< /span>=<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:textsize </span> =< Span class= "hljs-value" > "25sp" </span> <span class="hljs-attribute">android:textcolor </span> =<span class="hljs-val UE ">" #FF0000FF "</span> <span class=" hljs-attribute ">android:gravity </span> =<span class=" hljs-value ">" center_ Horizontal "</span> <span class=" hljs-attribute ">android:text </span> =<span class=" hljs-value ">" my home "</span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">com.example.myandroiddemo.MyItemView</span><span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/myitem_view"</span> <span class="hljs-attribute"> Android:layout_width</span>=<span class="hljs-value">"fill_parent"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"wrap_content"</span> <span class="hljs-attribute"> Android:layout_gravity</span>=<span class="hljs-value">"center"</span> /> </span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">framelayout</span>></span></span> </code></pre> <ul> <ul> <li>Custom Layouts View_item.xml as Follows:</li> </ul> </ul><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">linearlayout </span> <span class="hljs-attribute">xmlns: Android </span> =<span class="hljs-value"> "http://schemas.android.com/apk/res/android" </span> <span class=" Hljs-attribute ">xmlns:tools </span> =<span class=" hljs-value ">" http://schemas.android.com/tools "</span> <span class="hljs-attribute">android:layout_width </span> =<span class="hljs-value"> "fill_parent" </span> <span class=" Hljs-attribute ">android:layout_height </span> =<span class=" hljs-value ">" fill_parent "</span> <span class=" Hljs-attribute ">android:orientation </span> =<span class=" hljs-value " " horizontal "< span>; </span></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">imageview </span> <span class="hljs-attribute">android:id <!-- Span-->=<span class="hljs-value"> "@+id/view_item_img" </span> <span class="hljs-attribute">android:layout_width=<span class="hljs-value"> "50dp" </span> <span class="hljs-attribute">android:layout_height </span> =<span clas s="hljs-value"> "50dp" </span> <span class="hljs-attribute">android:scaletype </span> =<span class="hljs-value"> " Fitxy "</span>/> </span></span></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">TextView</span><span class="hljs-attribute">android:id</span>= "<span class="hljs-value">@+id/view_item_txt"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">" Fill_parent "</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" 50DP "</span><span class="hljs-attribute">android:gravity</span>=<span class="hljs-value">" Center_vertical " </span> <span class="hljs-attribute">android:layout_marginleft</span>=<span class="hljs-value">"10dp"</span><span class="hljs-attribute">android:textsize</span>=<span class="hljs-value">"20sp"</span> <span class="hljs-attribute"> Android:textcolor</span>=<span class="hljs-value">"#FFFF0000"</span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">linearlayout</span>></span></span> </code></pre> <ul> <ul> <li>Use the following to resolve layouts in custom view:</li> </ul> </ul><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword">public</span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyItemView</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">LinearLayout</span> {</span> ...... <span class="hljs-keyword">private</span><span class="hljs-keyword">void</span><span class="hljs-title">initView</span>(Context context) { mContext = context; <span class="hljs-keyword">this</span><span class="hljs-keyword">true</span>); mMyItemImg = (ImageView)view.findViewById(R.id.view_item_img); mMyItemText = (TextView)view.findViewById(R.id.view_item_txt); } ...... } </code></pre></pre> <ul> <ul> <li>After the entire feature development is complete, use Hierarchyviewer to look at the layout hierarchy:<br></li> </ul> </ul>We found that a simple feature used a six-tier layout, including Phonewindow$decorview and Framelayout (id/content), which are automatically added to each WINDOW. It is obvious that there is redundancy in this layout, such as the second and third layers of framelayout, such as the fourth Myitemview (linearlayout Subclass) and the fifth linearlayout, can be reduced. <ul> <ul> <li>well, That's Our merge Tag. To modify the layout like This:</li> <li>Simplified layout_mergedemo.xml:</li> </ul> </ul><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">merge</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span><span class="hljs-attribute">xmlns: Tools</span>=<span class="hljs-value">"http://schemas.android.com/tools"</span>> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">textview </span> <span class="hljs-attribute">android:layout _width </span> =<span class="hljs-value"> "fill_parent" </span> <span class="hljs-attribute">android:layout_height< /span>=<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:textsize </span> =< Span class= "hljs-value" > "25sp" </span> <span class="hljs-attribute">android:textcolor </span> =<span class="hljs-val UE ">" #FF0000FF "</span> <span class=" hljs-attribute ">android:gravity </span> =<span class=" hljs-value ">" center_ Horizontal "</span> <span class=" hljs-attribute ">android:text </span> =<span class=" hljs-value ">" my home "</span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">com.example.myandroiddemo.MyItemView</span><span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/myitem_view"</span> <span class="hljs-attribute"> Android:layout_width</span>=<span class="hljs-value">"fill_parent"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"wrap_content"</span> <span class="hljs-attribute"> Android:layout_gravity</span>=<span class="hljs-value">"center"</span> /> </span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">Merge</span>></span></span> </code></pre> <ul> <ul> <li>Simplified view_item.xml:</li> </ul> </ul><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">merge</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span><span class="hljs-attribute">xmlns: Tools</span>=<span class="hljs-value">"http://schemas.android.com/tools"</span> > </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">ImageView</span><span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/view_item_img"</span><span class="hljs-attribute">android:layout_width</span>= <span class="hljs-value">"50dp"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"50dp"</span><span class="hljs-attribute">android:scaletype</span>=<span class="hljs-value">"fitxy"</span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">TextView</span><span class="hljs-attribute">android:id</span>= "<span class="hljs-value">@+id/view_item_txt"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">" Fill_parent "</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" 50DP "</span><span class="hljs-attribute">android:gravity</span>=<span class="hljs-value">" Center_vertical " </span> <span class="hljs-attribute">android:layout_marginleft</span>=<span class="hljs-value">"10dp"</span><span class="hljs-attribute">android:textsize</span>=<span class="hljs-value">"20sp"</span> <span class="hljs-attribute"> Android:textcolor</span>=<span class="hljs-value">"#FFFF0000"</span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">Merge</span>></span></span> </code></pre><p><p><strong>Because the merge instead of the layout element here is linearlayout, not framelayout, we need to add the LinearLayout property in the custom layout code, such as a vertical or horizontal layout, such as a background color, which is set to horizontal layout:</strong></p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">private</span><span class="hljs-keyword">void</span><span class="hljs-title">initView</span>(Context context) { setOrientation(LinearLayout.HORIZONTAL); …… } </code></pre></pre> <ul> <ul> <li>OK, It's that simple, let's take a look at the layout level:</li> </ul> </ul><p><p></p></p><p><p>haha, only four layers, loading speed is also significantly accelerated, especially if the layout is more complex, sub-view more cases, reasonable use of the merge can greatly improve the speed and smoothness of the Program.<br>however, There are some limitations to using the merge tag, which include the following:</p></p> <ul> <ul> <li><p>(1) merge can only be used in the root element of the layout XML file</p></li> <li><p>(2) when using merge to inflate a layout, you must specify a viewgroup as its parent element, and the Attachtoroot parameter to set inflate is True. (refer to inflate (int, viewgroup, BOOLEAN))</p></li> <li><p>(3) You cannot use the merge tag in viewstub. One of the most intuitive reasons is that Viewstub's inflate method does not have attachtoroot settings at All.</p><p>Speaking of which, the use of the merge is Finished. Also want to nag about the performance of the project, because I have limited level, and the product of this display demand is really compared to bt, so the whole project display framework did a lot of nesting, the specific point is Activitygroup nested viewflipper, and then nested tabhost, And then nested their own activity, roughly a few, up to 20 layers, My god! know the principles of Android layout design? Preferably 10 floor below, try not to more than 15 layers, if more performance will be reduced, there may be problems, that is, we see the stackoverflow, this project has been confirmed in some low-end machine that this error occurred, such as htc, such as a model, oppo, such as a model, (do not condemn me, no malicious derogatory meaning <_>), finally I use the merge shrinking ah, the majority of the layout is reduced to 15 layers below, a test, pass!</p><p>also, because the window form (for example, Activity) loads automatically adds the Phonewindow$decorview and framelayout (id/content) two-tier layouts, So if we use merge in the Activity's custom layout root element and want to set the overall background, you can use (id/content) to remove the framelayout and set the properties, which can be done:</p></li> </ul> </ul><pre class="prettyprint"><code class=" hljs avrasm">Setcontentview (R<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Layout</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. Layout</span></span>_showset)<span class="hljs-comment"><span class="hljs-comment">; </span></span>Framelayout Framelayout = (framelayout) This<span class="hljs-preprocessor"><span class="hljs-preprocessor">. GetWindow</span></span>()<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Getdecorview</span></span>()<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Findviewbyid</span></span>(android<span class="hljs-preprocessor"><span class="hljs-preprocessor">. R</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. ID</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. Content</span></span>)<span class="hljs-comment"><span class="hljs-comment">; </span></span>Framelayout<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Setbackgroundresource</span></span>(R<span class="hljs-preprocessor"><span class="hljs-preprocessor">. drawable</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. BG</span></span>_repeated_main)<span class="hljs-comment"><span class="hljs-comment">; </span></span>Layoutinflater<span class="hljs-preprocessor"><span class="hljs-preprocessor">.</span> from</span>(this)<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Inflate</span></span>(R<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Layout</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. Layout</span></span>_showset, framelayout, true);</code></pre>3. Use when needed<code><code><merge /></code></code><p><p>What is this play? is actually a lightweight page, we usually use it to do pre-loading processing, to improve page loading speed and improve the smoothness, viewstub itself does not occupy the hierarchy, it will eventually be replaced by its designated level.</p></p><pre><pre><code> 还是说说演出项目吧,还说?对了,实践才能发现问题嘛,在哪儿发现问题就在那儿改进。由于项目中用到了比较多的动画,而且嵌套布局比较复杂,所以在Android低端机上进行页面切换时候经常让人感觉卡卡的,不怎么流畅,因为页面切换动画和标题旋转动画是同时进行的,所以为了达到更好的体验就需要使用一种方法,在动画进行时尽量的减少其他操作,特别是页面加载重绘。赶紧想办法,起初我想先将要加载的页面所有的组件都初始为gone显示状态,整个页面只留一下加载滚动条,后来发现这是不行滴,因为在Android的机制里,即使是将某一个控件的visibility属性设置为不可见的gone,在整个页面加载过程中还是会加载此控件的。再后来就用到了ViewStub,在做页面切换动画时,只在页面中放一个loading加载图标和一个ViewStub标签,像下面这样:</code></pre></pre><p><p>> Layout_loading.xml layout file:</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">merge</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span>></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">viewstub</span><span class="hljs-attribute">android:id</span>= "<span class="hljs-value">@+id/viewstub"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"fill_ Parent "</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" fill_parent "</span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">neterrandloadview</span><span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/start_loading_lay"</span><span class="hljs-attribute">android:layout_ Width</span>=<span class="hljs-value">"fill_parent"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"fill_parent"</span> /> </span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">Merge</span>></span></span></code></pre><p><p>This page is fairly lightweight, which makes the animation load very fast and smooth. After the page switch animation is finished, We then specify the Viewstub resource to load the actual page, which is the layout file of the actual page to be Loaded. For example, to load the mainactivity layout file layout_main.xml,oncreate implemented as Follows:</p></p><pre class="prettyprint"><code class=" hljs avrasm">protected void OnCreate (Bundle Savedinstancestate) {super<span class="hljs-preprocessor"><span class="hljs-preprocessor">. OnCreate</span></span>(savedinstancestate)<span class="hljs-comment"><span class="hljs-comment">; </span></span>Setcontentview (R<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Layout</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. Layout</span></span>_loading)<span class="hljs-comment"><span class="hljs-comment">; </span></span>Mloadhandler = new Handler ()<span class="hljs-comment"><span class="hljs-comment">; </span></span>Mloadingview = (neterrandloadview) Findviewbyid (R<span class="hljs-preprocessor"><span class="hljs-preprocessor">. ID</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. Start</span></span>_loading_lay)<span class="hljs-comment"><span class="hljs-comment">; </span></span>Mloadingview<span class="hljs-preprocessor"><span class="hljs-preprocessor">. startloading</span></span>()<span class="hljs-comment"><span class="hljs-comment">; </span></span>Mviewstub = (viewstub) Findviewbyid (R<span class="hljs-preprocessor"><span class="hljs-preprocessor">. ID</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. Viewstub</span></span>)<span class="hljs-comment"><span class="hljs-comment">; </span></span>Mviewstub<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Setlayoutresource</span></span>(R<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Layout</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. Layout</span></span>_main)<span class="hljs-comment"><span class="hljs-comment">; </span></span>Mloadhandler<span class="hljs-preprocessor"><span class="hljs-preprocessor">. postdelayed</span></span>(new Runnable () {@Override public void Run () {mviewstub<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Inflate</span></span>()<span class="hljs-comment"><span class="hljs-comment">; </span></span>Mloadingview<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Hide</span></span>()<span class="hljs-comment"><span class="hljs-comment">; </span></span>} },<span class="hljs-number"><span class="hljs-number"></span> -</span>)<span class="hljs-comment"><span class="hljs-comment">; </span></span>}</code></pre><p><p>The above 500 units are ms, which is the time of delay loading. The above is used to dynamically add viewstub to the layout resources (mviewstub.setlayoutresource (r.layout.layout_main)), of course, According to the need can be directly in the Viewstub layout block settings, You need to set the Layout property under the Viewstub tab (android:layout= "@layout/layout_main").</p></p><pre><pre><code> ViewStub也是有少许缺点的,下面所说: 1、 ViewStub只能Inflate一次,之后ViewStub对象会被置为空。按句话说,某个被ViewStub指定的布局被Inflate后,就不能够再通过ViewStub来控制它了。所以它不适用 于需要按需显示隐藏的情况。 2、 ViewStub只能用来Inflate一个布局文件,而不是某个具体的View,当然也可以把View写在某个布局文件中。如果想操作一个具体的view,还是使用visibility属性吧。 3、 VIewStub中不能嵌套merge标签。(前面好像说过)</code></pre></pre><p><p>But the certainty is harmless, and we can still use viewstub to do a lot of things.<br>Sometimes you really have to admire the foresight and architecture of Google's Android team, and this subtle design can be considered and used.</p></p> <p><p>Three big tabs for Android layout optimization</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.