If we rewrite the same layout in every xml file, one is code redundancy and poor readability, and the other is difficult to modify, which is very unfavorable for later modification and maintenance. Therefore, in general, we need to write code with the same layout into a single module. Then, we can reuse the layout code through the <include/> label when using it.
Normally, some applications have a title bar at the top. Similar.
Example of the graph title bar
If most of the Activity la s in the project contain such a title bar, you can separate the la s of the title bar into an xml file.Copy codeThe Code is as follows: <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 name the above xml file "navigator_bar.xml". Other xml layout files of the Activity that require the title bar can be directly referenced.Copy codeThe Code is as follows: <include layout = "@ layout/navigator_bar"/>
Copy codeThe Code is as follows:
Generally, the overall UI style can be roughly determined at the initial stage of the project. Therefore, you can make some planning in the early stages and write out the common modules first.
Below is a shared layout that may be extracted:
1)Background. Some applications use a unified background on different interfaces. The Default background may be modified frequently in the future, 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)The navigation bar at the bottom. If the application has a navigation bar and most activities have the same navigation bar at the bottom, you can write the navigation bar as a common module.
4) ListView. Most applications use ListView to display multiple data entries. The style of ListView may be adjusted frequently in the later stage of the project, so it is better to use ListView as a common module.