用一个简单的开源项目说明XML文件在Android中的使用
最近学习应用集成,接触了较多的XML,所以就写写安卓项目中使用到的XML文件,简要分析一下他们的作用。
XML简述
XML:全称 Extensible Markup Language,翻译为扩展的标记语言,是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立。是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具。它与HTML一样都是SGML(Standard Generalized Markup Language,标准通用标记语言)。比SGML简单得多,比HTML更令编程人员喜爱,它包含了很多SGML特性,继承了SGML的优点,文档结构严谨,层次分明,语义更加明确,具有良好的可读性、易编写和易维护等特性,而且使得多媒体信息在不同的系统之间相互交流成为现实。 虽然XML占用的空间比二进制数据要占用更多的空间,但XML极其简单易于掌握和使用。这使得XML的应用越来越广泛,无论在数据交换、Web服务、内容管理、Web集成,还是配置数据等,都在使用XML。
使用的开源项目说明
项目地址:https://github.com/tigerguixh/ele_demo
项目简介:仿【饿了么】订餐软件的一个demo,实现功能较少,个人认为比较适合刚入门Android的新手开发者来学习,代码也比较简单易懂。
Android XML文件使用
在上述的开源项目源码文件中搜索xml文件,得到的结果如下图,其中同一种类型的文件被我用方框框在了一起。
一、布局文件
在layout目录下,使用比较广泛。负责定义安卓应用的布局,一个应用可以有一套、两套甚至多套布局,系统会根据不同情况自动找到最合适的布局文件。但是在同一界面的两套不同布局文件的文件名应该是相同的,只是放在了两个不同的目录下。
如ele_demo中layout目录下的welcome_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff36a1e9"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:src="@drawable/welcome" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全球首发 微信拼单"
android:textColor="@color/white"
android:textSize="23.0sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="微信群 多人订餐"
android:textColor="@color/white"
android:textSize="14.0sp" />
<TextView
android:id="@id/welcome_start_order"
android:layout_width="wrap_content"
android:layout_height="35.0dip"
android:layout_marginTop="20.0dip"
android:background="@drawable/selector_red_corners_start_button"
android:gravity="center"
android:paddingLeft="15.0dip"
android:paddingRight="15.0dip"
android:text="开始订餐"
android:textColor="@color/white"
android:textSize="18.0sp" />
</LinearLayout>
可以看出其欢迎页面使用的是线性布局,竖直排列。最终效果图如下:
二、图片文件
查看ele_demo中的项目文件,其结构如下图
发现与drawable相关的目录共有以下这些
drawable:存放各种位图文件,(.png,.jpg,.9png,.gif等)除此之外可能是一些其他的drawable类型的XML文件
drawable-hdpi:高分辨率,一般我们把图片存在这里
drawable-mdpi:中等分辨率,很少,除非兼容的的手机很旧
drawable-xhdpi:超高分辨率,手机屏幕材质越来越好,以后估计会慢慢往这里过渡
drawable-xxhdpi:超超高分辨率,这个在高端机上有所体现
系统会根据机器的分辨率来分别到这几个文件夹里面去找对应的图片。在开发程序时为了兼容不同平台不同屏幕,建议各自文件夹根据需求均存放不同版本图片。
开发者可以将已经做好的图片放到不同目录下,或者通过自定义XML文件来实现想要的图片当我们想让一个控件根据不同状态显示不同图片,可以直接在程序中控制,也可以在drawable目录建立XML文件达到相同的效果,。
如ele_demo中drawable目录下的home_menu_aboutus_icon.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/m_drawer_icon_about_selected" />
<item android:state_pressed="false" android:drawable="@drawable/menu_icon_about" />
</selector>
以上XML文件可以实现一个控件(假设为button),获取焦点,按下按钮,正常状态下显示不同图片的效果。
在ele_demo中,点击“关于我们”按钮时,图标变白;按钮松开时,图标恢复原来颜色。
三、菜单文件
在menu目录下,写代码时只需在onCreateOptionsMenu方法中用MenuInflater装载进去就OK了。
如ele_demo中menu目录下的main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>
可以看到,这个菜单栏中只有1个菜单项,其id是”@+id/action_settings”,title是”@string/action_settings”,这些现在不用弄清楚,在之后的内容会解释。
四、resource文件
在values目录下,之所以称之为resource文件,是因为values目录下xml文件都是以resource作为根节点。
resource文件有以下几种类型:
- 1.strings.xml——定义字符串的文件
如ele_demo中values目录下的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">饿了么</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
可以看到,其中一个字符串的name属性值为”action_settings”,说明上面有一处引用”@string/action_settings”的值即为这个字符串的值——Settings
- 2.colors.xml——定义颜色的文件,其格式如下:
<resources>
<!--定义图片颜色-->
<drawable name="screen_background_black">#ff000000</drawable>
<drawable name="translucent_background">#e0000000</drawable>
<drawable name="transparent_background">#00000000</drawable>
<!--定义文字颜色-->
<color name="solid_red">#f00</color>
<color name="solid_blue">#0000ff</color>
<color name="solid_green">#f0f0</color>
<color name="solid_yellow">#ffffff00</color>
</resources>
这里由于ele_demo中的colors.xml内容较长,所以就不贴出来了,其作用就是定义项目中使用到的颜色。
- 3.arrays.xml——定义数组的文件,格式如下:
<resources>
<string-array name="planets">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
<item>Pluto</item>
</string-array>
<integer-array name="numbers">
<item>100</item>
<item>500</item>
<item>800</item>
</integer-array>
</resources>
同理,这里由于ele_demo中的arrays.xml内容较长,所以就不贴出来了,其作用就是定义项目中使用到的字符串数组。
4.styles.xml——定义样式的文件,分为两种用途:
Style:以一个单位的方式用在布局XML单个元素(控件)当中。
Theme:以一个单位的方式用在应用中所有的Activity当中或者应用中的某个 Activity当中。
其格式如下:
<resources>
<!--Theme,可以用来定义activity的主题-->
<style name="Theme.Transparent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
<item name="android:windowBackground">@drawable/transparent_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>
<!--Style,可以用来定义某个View元素,这里是ImageView的样式-->
<style name="ImageView120dpi">
<item name="android:src">@drawable/stylogo120dpi</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
</resources>
5.dimen.xml——定义单位的文件,其格式如下:
<resources>
<dimen name="one_pixel">1px</dimen>
<dimen name="double_density">2dp</dimen>
<dimen name="sixteen_sp">16sp</dimen>
</resources>
同上,这里由于ele_demo中的dimen.xml内容较长,所以就不贴出来了,其作用就是定义项目中使用到的单位。
- 6.attrs.xml——定义属性的文件,主要用在自定义的组件中,其格式如下:
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
同上,这里由于ele_demo中的attrs.xml内容较长,所以就不贴出来了,其作用就是定义项目中使用到的属性。
五、动画文件
在anim目录下,动画资源分为两种:
- 1.实现图片的translate、scale、rotate、alpha四种变化,还可以设置动画的播放特性,称为Tween动画。其格式如下:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:interpolator="@android:anim/accelerate_interpolator"
android:fromXDelta="0" android:toXDelta="200" android:fromYDelta="0"
android:toYDelta="180" android:duration="2000" />
<scale android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="1.0" android:toXScale="2.0" android:fromYScale="1.0"
android:toYScale="2.0" android:pivotX="150%" android:pivotY="150%"
android:duration="2000" />
<alpha android:fromAlpha="1.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
<rotate ....各个属性></rotate>
<Interpolator >可以使用其子类和属性定义动画的运行方式,先快后慢,先慢后快等</Interpolator>
</set>
- 2.帧动画,逐帧播放设置的资源,称为Frame动画。其格式如下:
<animation-list xmlns:android=”http://schemas.android.com/apk/res/android”
android:oneshot=”true”>
<item android:drawable=”@drawable/rocket_thrust1″ android:duration=”200″ />
<item android:drawable=”@drawable/rocket_thrust2″ android:duration=”200″ />
<item android:drawable=”@drawable/rocket_thrust3″ android:duration=”200″ />
</animation-list>
ele_demo中menu目录下的main.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="180.0"
android:toDegrees="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="200"
/>
</set>
可以看出其属于Tween动画,完成了旋转功能。
六、raw目录下的文件
是直接复制到设备中的任意文件。它们无需编译,添加到你的应用程序编译产生的压缩文件中。一般为应用要用到的音频或视频文件等等,要使用这些资源,可以调用Resources.openRawResource(),参数是资源的ID,即R.raw.somefilename。
在ele_demo中,我们可以看到一个AndroidManifest.xml配置文件,我个人认为其应该是最重要的xml文件之一,下面来分析一下这个文件。
<?xml version="1.0" encoding="utf-8"?>
<!-- 定义程序所在的java包,版本号,版本名称-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ele_me"
android:versionCode="1"
android:versionName="1.0" >
<!-- 使用该app的最低sdk版本为2.3,系统开发版本为4.4
(根据Android SDK版本号与API Level对应关系)-->
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<!-- 使用网络功能所需权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<!-- 允许备份文件,定义应用的名称、图标、使用的主题等 -->
<application
android:name="com.example.BaseApplication"
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- 定义activity -->
<activity android:name="com.example.ele_me.activity.WelcomeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.ele_me.activity.HomePageActivity" >
</activity>
<activity android:name="com.example.ele_me.activity.RestaurantDetailActivity" >
</activity>
<activity android:name="com.example.ele_me.activity.DianPingWebActivity" >
</activity>
<activity android:name="com.example.ele_me.activity.LoginActivity" >
</activity>
</application>
</manifest>
七、xml目录下的文件
是程序中需要使用的普通xml文件。在运行时可以通过调用Resources.getXML()读取。
ele_demo中无xml目录,不做说明。
八、assets目录下的文件
是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件。
ele_demo中无asset目录,不做说明。
结语
本文通过对一个开源项目ele_demo项目的实例分析,可以看出Android项目中有许多不同类型的XML配置文件,它们分别有着不同的作用。通过不同XML的协作,使得显示层和数据层能够分离,提高了可维护性,也使得程序代码变得简洁易懂。