http://blog.csdn.net/arui319/article/details/8549849

 

 

 

在Android应用开发过程中,屏幕上控件的布局代码和程序的逻辑代码通常是分开的。界面的布局代码是放在一个独立的xml文件中的,这个文件里面是树型组织的,控制着页面的布局。通常,在这个页面中会用到很多控件,控件会用到很多的资源。Android系统本身有很多的资源,包括各种各样的字符串、图片、动画、样式和布局等等,这些都可以在应用程序中直接使用。这样做的好处很多,既可以减少内存的使用,又可以减少部分工作量,也可以缩减程序安装包的大小。

下面从几个方面来介绍如何利用系统资源。

 

1)利用系统定义的id

比如我们有一个定义ListView的xml文件,一般的,我们会写类似下面的代码片段。

<ListView

    android:id="@+id/mylist"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"/>

 

这里我们定义了一个ListView,定义它的id是"@+id/mylist"。实际上,如果没有特别的需求,就可以利用系统定义的id,类似下面的样子。

<ListView

    android:id="@android:id/list"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"/>

 

在xml文件中引用系统的id,只需要加上“@android:”前缀即可。如果是在Java代码中使用系统资源,和使用自己的资源基本上是一样的。不同的是,需要使用android.R类来使用系统的资源,而不是使用应用程序指定的R类。这里如果要获取ListView可以使用android.R.id.list来获取。

 

2)利用系统的图片资源

    假设我们在应用程序中定义了一个menu,xml文件如下。

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item

        android:id="@+id/menu_p_w_upload"

        android:title="附件"

        android:icon="@android:drawable/ic_menu_p_w_upload" />

</menu>

 

其中代码片段android:icon="@android:drawable/ic_menu_p_w_upload"本来是想引用系统中已有的Menu里的“附件”的图标。但是在Build工程以后,发现出现了错误。提示信息如下:

error: Error: Resource is not public. (at 'icon' with value '@android:drawable/ic_menu_p_w_upload').

 

从错误的提示信息大概可以看出,由于该资源没有被公开,所以无法在我们的应用中直接引用。既然这样的话,我们就可以在Android SDK中找到相应的图片资源,直接拷贝到我们的工程目录中,然后使用类似android:icon="@drawable/ic_menu_p_w_upload"的代码片段进行引用。

这样做的好处,一个是美工不需要重复的做一份已有的图片了,可以节约不少工时;另一个是能保证我们的应用程序的风格与系统一致。

 

经验分享:

Android中没有公开的资源,在xml中直接引用会报错。除了去找到对应资源并拷贝到我们自己的应用目录下使用以外,我们还可以将引用“@android”改成“@*android”解决。比如上面引用的附件图标,可以修改成下面的代码。

android:icon="@*android:drawable/ic_menu_p_w_upload"

修改后,再次Build工程,就不会报错了。

 

3)利用系统的字符串资源

    假设我们要实现一个Dialog,Dialog上面有“确定”和“取消”按钮。就可以使用下面的代码直接使用Android系统自带的字符串。

    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content">

        <Button

            android:id="@+id/yes"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1.0"

            android:text="@android:string/yes"/>

        <Button

            android:id="@+id/no"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1.0"

            android:text="@android:string/no"/>

    </LinearLayout>

 

如果使用系统的字符串,默认就已经支持多语言环境了。如上述代码,直接使用了@android:string/yes和@android:string/no,在简体中文环境下会显示“确定”和“取消”,在英文环境下会显示“OK”和“Cancel”。

 

    4)利用系统的Style

    假设布局文件中有一个TextView,用来显示窗口的标题,使用中等大小字体。可以使用下面的代码片段来定义TextView的Style。

    <TextView

        android:id="@+id/title"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceMedium" />

 

    其中android:textAppearance="?android:attr/textAppearanceMedium"就是使用系统的style。需要注意的是,使用系统的style,需要在想要使用的资源前面加“?android:”作为前缀,而不是“@android:”。

 

5)利用系统的颜色定义

除了上述的各种系统资源以外,还可以使用系统定义好的颜色。在项目中最常用的,就是透明色的使用。代码片段如下。

android:background ="@android:color/transparent"

 

经验分享:

Android系统本身有很多资源在应用中都可以直接使用,具体的,可以进入android-sdk的相应文件夹中去查看。例如:可以进入$android-sdk$\platforms\android-8\data\res,里面的系统资源就一览无余了。

开发者需要花一些时间去熟悉这些资源,特别是图片资源和各种Style资源,这样在开发过程中,能够想到有相关资源并且直接拿来使用。