提高Android代码复用性的几种方式

cm54 9年前

对于初学者来说,当自己要实现相似的功能时候,总是复制粘贴。这样不仅增加了工作量,同时也造成了代码冗余等问题。下面,就由小言来介绍几种提高Android代码复用性的方法。(为了浅显易懂,都是举最简单的例子,假如里面有什么不对的,敬请提出改善)

1、活用include

include中文翻译是包含包括的意思。最直接明显的运用的地方便是APP的标题,因为在一个APP中,其标题的格局差不多一致,每次都要复制粘贴,多麻烦。现在就来介绍一下include的简单运用。

首先,我们先举一个例子,例如在layout中创建一个名为include_title.xml的文件,其代码为:

<?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="标题"          android:textColor="@color/white"          android:textSize="20dp" />    </RelativeLayout>

然后在需要添加标题的xml文件中加上    <include layout="@layout/include_title" />这话便可将标题显示在当前页面中,例如:
<?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:orientation="vertical" >        <include layout="@layout/include_title" />    </LinearLayout>

2、使用extends

在一些页面中,我们总是需要共用一些页面显示效果的功能,例如Toast,Dialog等等。在这时,我们可以将这些东西封装到一个Activity中,当某个Activity需要使用里面的功能的时候,只要直接去继承它们,然后调用相应的方法即可。以下为制作一个BaseActivity的例子代码:

import android.app.Activity;  import android.widget.Toast;    public class BaseActivity extends Activity {     /**    * 弹出提示(int)    *     * @param intContent    */   public void showToast(int intContent) {    Toast.makeText(getApplication(), intContent, Toast.LENGTH_SHORT).show();   }      /**    * 弹出提示(String)    *     * @param intContent    */   public void showToast(String strContent) {    Toast.makeText(getApplication(), strContent, Toast.LENGTH_SHORT).show();   }  }

当我们需要使用Toast的时候,只要继承这个BaseActivity这个类,然后直接调用showToast(参数)方法便可以直接弹出Toast,是不是简单一些呢。


3、类的封装

在2中讲的是将页面显示的效果封装起来,而这里讲的是将功能代码封装起来。在一些时候,我们需要重复调用一个功能方法,是不是觉得复制粘贴很麻烦呢,在这时,我们只需要将其功能代码封装起来,供以后调用。这也就是MVC模式中的Model层。例如:

我们新建一个名为Tools的Java类,其代码为:

/**   * 工具类   *    */  public class Tools {     public static void outPut(Object obj) {        System.out.println(obj);       }  }


在这时,假如我们想输出一个数据的时候,直接调用Tools.outPut(参数)方法便可输出数据。


4、使用string.xml和color.xml

开发一个APP的时候,我们难免会使用到一些颜色值或者文字,在这时,我们应该将其放在相对应的color.xml或string.xml文件中,这样不仅提高代码的复用性,而且也便于修改,而不用到时改点需求的时候,就要到处找出页面修改其颜色值和文字。

例如:color.xml文件

<?xml version="1.0" encoding="utf-8"?>  <resources>        <color name="transparent">#00000000</color>      <color name="black">#000000</color>      <color name="blue">#5DC0F8</color>    </resources>

使用的时候:

    <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="标题"          android:textColor="@color/blue" />

string.xml的就不举例了,估计大家都懂了。


5、使用library

当做项目做多的时候,就会发现,其实,很多功能或者效果什么的,都非常相似,在这时,我们就该收集一下那些基本代码,将其封装在一个library中,等到用时直接加载这个library,而不需要重新写。其实就是和导入开源框架一样。