Include label layouts and custom headings

Source: Internet
Author: User

The performance optimizations on Android are that you can use the abstract layout label (Include,viewstub,merge), include to remove unnecessary nesting and view nodes, merge to reduce unnecessary inflate and other layout, Viewstub can hide the view.

The include tag is often used to extract the public parts of the layout for other layouts to be common for layout modularity, which facilitates layout authoring.

Activity_main. XML<linear xmlns:android="Http://schemes.android.com/apk/res/android"android:Layout_width="Match_parent"android:layout_height="0DP"android:layout_weight="1"/><include layout="@layout/foot"/></linearlayout>foot. XML<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"android:Layout_width="Match_parent"android:layotu_height="Match_parent"><buttonandroid:Id="!id/buton"android:Layout_width="Match_parent"android:layout_height="40DP"android:Layout_above="@+id/text"android:text="button"/><textandroid:Id="@id/text"android:Layout_width="Match_parent"android:layout_height="40DP"android:Layout_alignparentbuttom="true"android:text="@string/app_name"The only attribute required for the/></relativelayout><include> tag is the Layout property, which specifies the desired file to include. You can define the Android:id and Android:layout_* properties to override the corresponding attribute values that are introduced to the layout root node. The merge label can be used in two typical cases <merge/> tags riding a very important role in the structure optimization of the UI, He can prune the extra layers and optimize the UI. </merge> More is used to replace framelayout or when one layout contains another is the,</merge> label to eliminate the extra view group in the view hierarchy. For example, your main layout file is a relative layout and introduces a relative layout of include, which slows down your UI performance if the relativelayout used by the include layout is meaningless. You can use <merge/> tag optimization at this point.

Label
The viewstub tag can be used to introduce an external layout as well as an include tag, but the layout introduced by Viewstub does not expand by default, which saves both the display and the location, saving CPU and memory when parsing layout. Viewstub often used to hide and show view

Footlayout.xml file:<?xml version= "1.0" encoding= "Utf-8"?><relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" android:layout_width="Match_parent" android:layout_height="Match_parent" >        <buttonandroid:layout_width="Wrap_content"android:layout_height ="Wrap_content"android:id= "@+id/button"android:text=" No clicks, no response. "/>                                    <TextViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:id="@+id/text"android:layout_torightof="@id/button"android: Layout_marginleft="20px"android:text="friendly offer"/>                                                </relativelayout>Activity_main layout<linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  xmlns:tools  =" Http://schemas.android.com/tools " android:layout_width  = "match_parent"  android:layout_height  =" match_parent " android:orientation  =" vertical " tools:context  = "com.baozilichao.android2lesson_07_ Inerfacelayout. Mainactivity ";     <button  android:layout_ Width  = "wrap_content"  android:layout_height< /span>= "wrap_content"  android:id  = "@+id/button1"  android:text  = " Click to respond "/>     <TextViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" Android:text="Hello world!" android:id="@+id/text"/>                                    <!--include introduces the effect of reusing layouts, but if you do not set location information, it is ugly to set the Include label layout, you must first set the width height. If the ID is conflicting, the ID of the layout that is in response to this layout is overwritten with the IDs of the id,include imported.    <!--<include android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android Oid:layout_alignparentbottom= "true" layout= "@layout/footlayout"/>-->    <viewstubandroid:layout_width="Match_parent"android:layout_height= "Match_parent" android:id="@+id/viewstub"android:layout="@layout/footlayout"/>                                </linearlayout>1mainactivity.javapublic class Mainactivity extends Appcompatactivity {private Button button1;    View View1;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//The introduction of a control in a layout file can be obtained directly from the ID to button= (button) Findviewbyid (R.id.button        );        button1= (Button) Findviewbyid (R.id.button1); tv= (TextView) Findviewbyid (r.id.text);//After the viewstub is taken from the ID, the call inflate () will display the view and return as the return value view1= ((viewstub)        Findviewbyid (R.id.viewstub)). Inflate ();        View1.setvisibility (view.invisible);                 Button1.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {//                Tv.settext ("Red and Black Alliance");                if (view1.getvisibility () = = view.visible) {view1.setvisibility (view.invisible);                }else{view1.setvisibility (view.visible);      }      }        }); }}

In development, the system provides us with the title bar often does not meet our needs, this time, we need to use the custom title bar.
Style.xml

<resources>    <!--Base application theme.    <style name= "apptheme" parent=" Theme.AppCompat.Light.DarkActionBar "><! -- Customize your Theme here. --> <item name= "colorprimary" >@color/colorprimary</ item> <item name="Colorprimarydark"> @color/colorprimarydark</item> <item Name="Coloraccent"> @color/coloraccent</item> </style>    <!--Create your own style here for system theme usage--    <style name="Mytitlebar" parent="Android:theme"><!  --Set the height of the original title bar- -> <item name= "android: Windowtitlesize" >50dp</Item> <!  --Set a new title field full eye to the sides of the original header bar --> <item name= "Android c20>:p Adding ">0dp</Item></style>1title_layout.xml title bar Androidmanifest.xml<manifest xmlns:android="Http://schemas.android.com/apk/res/android"  package ="Com.baozilichao.android2lesson_07_mytitlebar">        <applicationandroid:allowbackup="true"android:icon="@mipmap/ic_ Launcher "android:label=" @string/app_name "android:supportsrtl=" true " android:theme="@style/apptheme">                                                <activity android:name=". Mainactivity "android:theme=" @style/mytitlebar ">            <!--!!!!!!!!! -            <intent-filter>                <action android:name="Android.intent.action.MAIN" />                <category android:name="Android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </Application>1public class Mainactivity extends Activity {//Custom title bar step//1. Custom title bar layout file//2. Specify the use of custom title bars//3 in the corresponding activity. Customize the Custom The title and theme are fused to create a theme of your own//4. Configure the manifest file, set using a custom theme @Override protected void OnCreate (Bundle savedinstancestate) {su Per.oncreate (savedinstancestate);//This line of code must be loaded before Setcontentview requestwindowfeature (window.feature_custom_tit        LE); Setcontentview (R.layout.activity_main);//Set Current activity using the custom title bar, specify the custom title bar used for the layout file you just created GetWindow (). Setfeaturei    NT (window.feature_custom_title,r.layout.title_layout); }

Include label layouts and custom headings

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.