For beginners, always copy and paste when they want to implement similar functions. This not only increases the workload, but also causes problems such as code redundancy. Here's a little talk about several ways to improve the reusability of Android code. (in order to be easy to understand, is to give the simplest example, if there is something wrong, please ask for improvement)
1. Utilization of include
Include Chinese translation is included in the meaning. The most direct and obvious use of the app is the title, because in an app, its title pattern is almost the same, every time to copy and paste, much trouble. Now let's introduce the simple use of include.
First, let's give an example, for example, to create a file named Include_title.xml in layout with the following code:
<?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=" 50DP " android:background=" @ Color/blue "> <imagebutton android:id=" @+id/imgbtnback " android:layout_width=" 40DP " android:layout_height= "Fill_parent" android:background= "@color/transparent" android:src= "@drawable/ Fanhui "/> <textview android:id=" @+id/tvtitle "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_centerinparent= "true" android:text= "title" Android:textcolor= "@color/white" android:textsize= "20DP"/></relativelayout>
Then add the <include layout= "@layout/include_title" to the XML file where you want the header to appear in the current page, such as:/>.
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <include layout= "@layout/include_title"/></linearlayout>
2. Using extends
On some pages, we always need to share some features of the page display effect, such as Toast,dialog and so on. At this point, we can encapsulate these things into an activity, when an activity needs to use the function inside, just go to inherit them, and then call the appropriate method. The following is an example code for making a baseactivity:
Import Android.app.activity;import Android.widget.toast;public class Baseactivity extends Activity {/** * pop-up hint (int) *
* @param intcontent */public void showtoast (int intcontent) {Toast.maketext (Getapplication (), Intcontent, Toast.length_short). Show ();} /** * Popup Hint (string) * * @param intcontent */public void Showtoast (String strcontent) {Toast.maketext (Getapplication () , Strcontent, Toast.length_short). Show ();}}
When we need to use a toast, just inherit this baseactivity class, and then call the Showtoast (parameter) method directly to pop the toast, is not simple.
3, Class of packaging
In 2, it's about encapsulating the effects of the page display, and this is about wrapping up the feature code. At some point, we need to call a function method repeatedly, do not think copy and paste is very troublesome, at this point, we just need to encapsulate its functional code for later invocation. This is the model layer in the MVC pattern. For example:
We create a new Java class called tools with the following code:
/** * Tool class * */public class Tools {public static void OutPut (Object obj) {System.out.println (obj);}}
At this point, if we want to output a data, directly call the Tools.output (parameter) method to output data.
4. Using String.xml and Color.xml
When developing an app, we will inevitably use some color values or text, at this point, We should put it in the relative color.xml or string.xml file, so that not only improve the reusability of code, but also easy to modify, and do not need to pity Dorado when the need to find the page everywhere to modify its color values and text.
Example: Color.xml file
<?xml version= "1.0" encoding= "Utf-8"?><resources> <color name= "Transparent" > #00000000 </ color> <color name= "Black" > #000000 </color> <color name= "Blue" > #5DC0F8 </color> </resources>
When used:
<textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android: text= "title" android:textcolor= "@color/blue"/>
String.xml is not an example, it is estimated that everyone understands.
5. Using the Library
As long as the project, you will find that, in fact, a lot of features or effects are very similar, at this time, we should collect the basic code, it is packaged in a library, wait for the time to load the library directly, and do not need to re-write. It's just like importing an open source framework.
Android improves code reusability