Android开发优化之——对界面UI的优化(1)

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

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

1)利用系统定义的idweb

好比咱们有一个定义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)利用系统的图片资源ui

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

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menu_attachment"
        android:title="附件"
        android:icon="@android:drawable/ic_menu_attachment" />
</menu>

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

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

从错误的提示信息大概能够看出,因为该资源没有被公开,因此没法在咱们的应用中直接引用。既然这样的话,咱们就能够在Android SDK中找到相应的图片资源,直接拷贝到咱们的工程目录中,而后使用相似android:icon="@drawable/ic_menu_attachment"的代码片断进行引用。code

这样作的好处,一个是美工不须要重复的作一份已有的图片了,能够节约很多工时;另外一个是能保证咱们的应用程序的风格与系统一致。


经验分享:

Android中没有公开的资源,在xml中直接引用会报错。除了去找到对应资源并拷贝到咱们本身的应用目录下使用之外,咱们还能够将引用“@android”改为“@*android”解决。好比上面引用的附件图标,能够修改为下面的代码。

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

修改后,再次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资源,这样在开发过程当中,可以想到有相关资源而且直接拿来使用。

---------------------------------------------------------------------------

http://blog.csdn.net/arui319

《Android应用开发精解》已出版,本文是初稿的部份内容。欢迎购买阅读。

本文能够转载,可是请保留以上做者信息。

谢谢。

---------------------------------------------------------------------------

相关文章
相关标签/搜索