我们继续的沿用上一篇所建立的应用。
Android应用程序可以分为:应用程序源代码(.java),应用程序描述文件(.
可以这么理解: 安卓应用程序,通过java代码来实现其业务逻辑,由
我们来看看下面的几类文件:
一、资源极其描述文件。
1. strings.
strings.
打开HelloWorld项目,展开res/values/目录,找到strings.
建立项目的时候自动创建的代码:
<?
代码注释版:
<?
在AndroidManifest.
对应的效果:
images/loading.gif' data-original="http://images2015.cnblogs.com/blog/697835/201601/697835-20160101135309042-1410594546.png" />
2. styles.
styles.
创建项目的时候生成的代码:
<resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.--> <style name="AppBaseTheme" parent="android:Theme.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style></resources>
在name="AppBaseTheme"上加以下代码:
<item name="android:textSize">30sp</item>
<item name="android:textColor">#111</item>
在 name="AppTheme" 上加以下代码:
<item name="android:textSize">8sp</item>
代码解析:
<? Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.--> <style name="AppBaseTheme" parent="android:Theme.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.while customizations related to backward-compatibility can go here. --> <!-- 定义类 "AppBaseTheme" 样式文本的大小为 30sp ,颜色为 #111 --> <item name="android:textSize">30sp</item> <item name="android:textColor">#111</item> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <!-- 定义类 "AppTheme" ,在保持父样式的其他属性不变的情况下,该样式文本的大小为 8sp --> <item name="android:textSize">8sp</item> </style></resources>
3. dimens.
dimens.使用“<dimen>”标签指定一个维度资源 。
用标识符表示维度单位:
px(像素):屏幕上的像素。
in (英寸):长度单位。
mm(毫米):长度单位。
pt (磅):1/72英寸。
dip(与密度无关的像素):一种基于屏幕密度的抽象单位。
sp (与刻度无关的像素):与pd类似。
建议:使用sp作为文字的单位,使用dip作为其他元素的单位。
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen></resources>
4. colors.
有些应用可能会没有这个文件。
使用“<color>”标签定义一个颜色资源。
颜色值由RGB(三位16进制数)或RRGGBB (六位16进制数)表示,以“# ”符号开头。例如:#00f(蓝色), #00ff00(绿色)。
定义透明色,表示透明度的alpha通道值紧随“#”之后。例如: #600f(透明蓝色), #7700ff00(透明绿色)
<?
5. activity_main.
位于res文件夹的layout子文件夹中。
定义第一个显示界面布局(默认)。
<RelativeLayout ="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></RelativeLayout>
解析后的代码:
<? ="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <!-- 添加了一个textView 控件,其显示的内容是 string.TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></RelativeLayout>
6. AndroidManifest.
根目录下的AndroidManifest.
<?package="com.souvc.helloworld" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.souvc.helloworld.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
解析后的代码:
<?package="com.souvc.helloworld" android:versionCode="1" android:versionName="1.0" > <!-- 定义了包名 --> <!-- 定义了版本 --> <!-- 定义了版本名--> <!-- 可兼容的最低版本,以及当前版本 --> <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- 定义应用程序的图标 --> <!-- 定义应用程序的名字 --> <!-- 定义应用程序的主题 --> <!-- 应用程序第一个执行的是 com.souvc.helloworld 下的 MainActivity 类 --> <activity android:name="com.souvc.helloworld.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
二、 逻辑代码文件。
1. MainActivity.java 文件。
位于src文件夹中。
应用程序的操作控制部分在java源程序中定义
package com.souvc.helloworld;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
解析后的代码:
package com.souvc.helloworld;//引入相关的类import android.os.Bundle;import android.app.Activity;import android.view.Menu;/*** @Title: MainActivity.java* @Package com.souvc.helloworld* @Description:操作安卓界面的代码 * @author souvc * @date 2016-1-1* @version V1.0 */public class MainActivity extends Activity { //第一次创建该Activity时的回调方法 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);//调用父类的onCreate()构造函数,savedInstanceState保存当前的Activity的状态信息 setContentView(R.layout.activity_main);//设置当前显示的布局 } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
2. R.java 文件
由Android-Eclipse自动生成,不能直接修改。
用资源id的形式标注drawable、layout、values文件夹中的资源信息。
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */package com.souvc.helloworld;public final class R { public static final class attr { } public static final class color { public static final int character=0x7f040001; public static final int listDivider=0x7f040000; } public static final class dimen { /** Default screen margins, per the Android Design guidelines. Customize dimensions originally defined in res/values/dimens.*/ public static final int activity_horizontal_margin=0x7f050000; public static final int activity_vertical_margin=0x7f050001; } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int action_settings=0x7f090000; } public static final class layout { public static final int activity_main=0x7f030000; } public static final class menu { public static final int main=0x7f080000; } public static final class string { /** 字符串的为 appname ,内容为 “Settings” */ public static final int action_settings=0x7f060001; /** String 定义了三个字符串常量 ,该变量可以在java和*/ public static final int app_name=0x7f060000; /** 字符串的为 appname ,内容为 “Hello world!” */ public static final int hello_world=0x7f060002; } public static final class style { /** Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.*/ public static final int AppBaseTheme=0x7f070000; /** Application theme. */ public static final int AppTheme=0x7f070001; }}
以上的注释,当在
原标题:第四篇 Android应用程序详细解析
关键词:Android