In an application, there is typically more than one activity, and each activity corresponds to a UI layout file. In general, in order to keep the style of the different Windows unified, in these UI layout files, almost certainly will use a lot of the same layout. If we rewrite the same layout in every XML file, one is code redundancy, readability is poor, the other is cumbersome to modify, and very detrimental to late modification and maintenance. So, in general, we need to write the same layout code in a separate module, and then use the <include/> tag to reuse the layout code.
Frequently, some apps have a title bar at the top. Similar to the following.
Example of a graph title bar
If the layout of most activities in your project contains such a title bar, you can write the layout of the title bar separately into an XML file.
- <relativelayout
- Android:layout_width="Fill_parent"
- android:layout_height="Wrap_content"
- android:gravity="center"
- android:background="@drawable/navigator_bar_bg"
- xmlns:android="Http://schemas.android.com/apk/res/android" >
- <textview
- Android:id="@android: Id/title"
- Android:layout_width="Fill_parent"
- android:layout_height="Wrap_content"
- Android:layout_centervertical="true"
- android:gravity="center"
- android:hint="title"
- Android:textappearance="? Android:attr/textappearancemedium"/>
- <imageview
- Android:id="@android: Id/closebutton"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:layout_alignparentright="true"
- android:src="@drawable/close"/>
- </RelativeLayout>
We named the XML file above as "Navigator_bar.xml", and other XML layout files that require the activity of the title bar can refer to this file directly.
- <include layout="@layout/navigator_bar"/>
| Experience Sharing: Under normal circumstances, The overall UI style can be roughly defined at the beginning of the project. So in the early days you could do some planning and write out the generic modules first. Below is a common layout that you might be able to extract: 1) background. Some applications use a unified background in different interfaces. The default background may often be modified later, so the background can be made into a common module. 2) The title bar of the header. If the application has a uniform header title bar, it can be extracted. 3) at the bottom of the navigation bar. If the navigation bar is applied and most of the activity's bottom navigation bar is the same, the navigation bar can be written as a generic module. 4) ListView. Most applications use the ListView to show multiple data. The ListView style may often be adjusted later in the project, so it is better to use the ListView as a common module. |