胡家威 计研135班 http://hujiaweibujidao.github.io/
小应用 Magic8
Android 系统简介 Android 应用结构 Android 四大组件 Activity 生命周期 Android 资源管理 UI 组件和容器组件 内容概要 ( 上 )
Android 系统简介
Android 市场份额
Android 是如何诞生的? Android 之父 :Andy Rubin 现就职于 Google, 曾一直主管 Android 项目, 现在正在进行一项秘密的机器人项目
那些年的 Android 系统
Android 系统架构
Android 运行时 JRE(Java Runtime Environment) 包括 JVM(Java Virtual Machine) 和其他功能函数库 Android Runtime Environment 包括 Dalvik 虚拟机和核心库集 Dalvik 虚拟机和 JVM 不同, 它并没有遵守 JVM 规范, JVM 运行的是 Java 字节码 (.class) 文件, 而 Dalvik 虚拟机运行的是其专有的 Dalvik Executable (.dex) 文件 每个 Android 应用程序都运行在单独的 Dalvik 虚拟机中, 以便实现对应用程序的隔离 Dalvik 虚拟机针对移动平台进行了很多优化, 所以性能很好
Android 应用结构
Android 应用标准目录结构 AndroidManifest.xml 文件是应用程序的清单文件 src: 存放 Java 源代码 res: 存放应用程序的资源文件, 包括图片 音效 界面布局文件等, 它们都能通过 R 资源清单类进行访问 assets: 存放原生资源文件, 其中的资源不能通过 R 资源清单类进行访问 libs: 存放第三方或者应用内 NDK 开发得到的库文件 gen: 存放 aapt 工具自动生成的 Java 代码 bin: 存放编译结果和打包得到的文件, 例如 class 文件 dex 文件和 apk 文件等
AndroidManifest.xml 文件 整个 Android 应用的全局描述文件 (1) 应用兼容的最低版本 <uses- sdk android:minsdkversion="8" android:targetsdkversion="18" /> (2) 应用所需的权限声明 <uses- permission android:name="android.permission.internet" /> <uses- permission android:name="android.permission.vibrate" />
AndroidManifest.xml 文件 (3) 应用的基本信息 ( 名称 图标 主题等 ) <application android:icon="@drawable/magic8" android:label="@string/app_name" android:screenorientation="portrait" android:theme="@style/android:theme.notitlebar" > (4) 应用的组件声明 (activity service contentprovider 等 ) <activity android:name="edu.thu.magic.magicactivity" /> <activity android:name="edu.thu.magic.aboutactivity" />
应用资源字典类, 为各种资源生成索引文件 :gen/edu/thu/magic/r.java [ 代码片段 ] public static final class layout { } public static final int activity_about=0x7f030000; public static final int activity_logo=0x7f030001; public static final int activity_magic=0x7f030002; public static final class raw { } public static final int shake_match=0x7f040000; public static final int shake_nomatch=0x7f040001; public static final int shake_sound=0x7f040002; public static final class string { } R.java 文件 public static final int app_name=0x7f070000;
Android 四大组件
Activity 是应用程序的交互界面, 类似 Swing 编程中的 JFrame 控件 所有 Activity 组件都是继承自 Activity 基类, 它们拥有自己的生命周期 Activity 组件
Service组件 Service和Activity地位并 列 只是它通常位于后台 运行 一般不需要和用户 交互 所有Service组件 都要继承自Service基类 它拥有自己的生命周期
BroadcastReceiver 组件 BroadcastReceiver 组件是一个广播接收器, 类似于事件编程中的事件监听器, 它监听的事件源是 Android 应用中的其他组件
ContentProvider 组件 ContentProvider 为系统中为跨应用的数据交换提供了标准 应用可以通过 ContentProvider 提供数据, 其他应用可以通过 ContentResolver 来访问数据
Activity 生命周期
Activity 生命周期 结合 Servlet 程序, 浅谈对 生命周期 的理解 : 生命周期是指某个对象具有几个基本状态 ( 生命周期中的不同阶段 ), 对象实例在不同的状态下运行对应状态下的方法 ( 生命周期方法 ); 开发者不需要创建 Servlet 或者 Activity 的实例, 它们的实例都由 外部容器 创建, 对于 Servlet 来说是指 Web 容器, 对于 Activity 来说是指 Dalvik 虚拟机 ; 开发者不需要调用它们的方法, 它们的生命周期方法都是由 外部容器 通过回调的方式调用, 开发者只需要实现生命周期方法即可 ;
public class Activity extends ApplicationContext { protected void oncreate( ); protected void onstart(); protected void onrestart(); protected void onresume(); protected void onpause(); protected void onstop(); protected void ondestroy(); }
Activity 简单实例 关于作者 界面文件 :src/edu/thu/magic/aboutactivity.java public class AboutActivity extends Activity { protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_about); } } // 返回主界面 public void btn_about_back(view view) { AboutActivity.this.finish(); }
View 组件是所有 UI 组件和容器组件的基类, 它们需要放在容器组件中, 然后通过 Activity 调用 setcontentview 方法显示出来 setcontentview() 方法有两种使用方式 : 1. 传递一个布局管理器 (Layout); LinearLayout ll = new LinearLayout(this); setcontentview(ll); setcontentview 2. 传递一个布局管理资源的 ID setcontentview(r.layout.activity_about);
Android 资源管理
Android 应用资源可以分为两大类 : (1) 可以通过 R 清单类访问的原生资源, 保存在 res 中 ; (2) 不可以通过 R 清单类访问的资源, 保存在 assets 中 大部分资源都是放在 res 目录下, 在应用进行编译的时候会在 R 类中为资源生成对应的索引项 文件 :gen/edu/thu/magic/r.java [ 代码片段 ] public static final class layout { } Android 应用资源 public static final int activity_about=0x7f030000; public static final int activity_logo=0x7f030001; public static final int activity_magic=0x7f030002;
资源的类型和保存位置 res/layout res/menu res/anim res/drawable res/values res/raw 布局 XML 文件 菜单 XML 文件 动画 XML 文件 图片文件或者是能够编译成各种 Drawable 对象的 XML 文件多个命名为 drawable-?dpi 的文件夹存放的都是图片资源, 只不过是为了适应不同的屏幕大小存放不同分辨率的图片而已 存放各种简单值的 XML 文件, 这些简单值包括字符串 颜色 尺寸 样式 数组等, 它们分别有对应的 XML 文件名, 例如 strings.xml,colors.xml,dimens.xml,styles.xml,arrays.xml 原生资源, 例如音频或者视频文件 在 Java 代码中可以通过 openrawresource 方法得到该资源的二进制流 但是, 如果应用需要使用原生资源的话, 推荐将原生资源存放在 assets 目录下, 在 Java 代码中可以通过 AssetManager 来访问这些资源
在 Java 代码中使用资源 : R.[ 资源类别 ].[ 资源名称 ] 文件 src/edu/thu/magic/aboutactivity.java [ 代码片段 ] protected void oncreate(bundle savedinstancestate) { } 资源的使用方式 super.oncreate(savedinstancestate); setcontentview(r.layout.activity_about); 在 XML 文件中使用资源 : @[ 资源类别 ]/[ 资源名称 ] android:icon="@drawable/magic8 android:label="@string/app_name" android:textcolor="@color/text_default android:textsize="@dimen/text_titlebar_center android:theme= @style/android:theme.notitlebar [ 系统内资源 ]
字符串资源 定义应用中常用的字符串 文件 res/strings.xml <resources> <string name="app_name">magic8</string> </resources>
颜色资源 定义应用中常用的颜色 文件 res/colors.xml <resources> <color name="text_default">#ffffff</color> <color name="app_default">#711098</color> <color name="text_orange">#ff6600</color> <color name="text_green">#009e0e</color> <color name="text_light">#666</color> <color name="text_blue">#36c</color> <color name="text_preference_key">#000</color> <color name="text_preference_value">#f60</color> </resources>
定义应用中常用的尺寸大小 dp=dip=device independent pixels, sp=scaled pixels 文件 res/dimens.xml <resources> 尺寸资源 <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="btn_big_textsize">18sp</dimen> <dimen name="text_default_size">15sp</dimen> <dimen name="titlebar_height">45dp</dimen> <dimen name="text_titlebar_size">12sp</dimen> <dimen name="text_titlebar_center">20sp</dimen> <dimen name="btn_titlebar_size">70dp</dimen> </resources>
Drawable 的各种子类都代表了一种 Drawable 资源, 例如 StateListDrawable,ShapeDrawable,AnimationDrawable 等等 StateListDrawable 最常用, 例如 : <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_style_disabled" android:state_enabled="false"/> <item android:drawable="@drawable/btn_style_green_pressed" android:state_focused="true" android:state_pressed="true"/> <item android:drawable="@drawable/btn_style_green_pressed" android:state_focused="false" android:state_pressed="true"/> <item android:drawable="@drawable/btn_style_green_focused" android:state_focused="true"/> <item android:drawable="@drawable/btn_style_green_normal" android:state_focused="false"/> </selector> Drawable 资源
UI 组件和容器组件
View 和 ViewGroup Android 系统中的 UI 组件很多, 它们都是建立在 View 和 ViewGroup 之上的, 两者都是 View 的子类 ViewGroup 通常是作为其他 View 组件的容器来使用, 但是它也是 View 的子类, 所以 ViewGroup 中还可以包含其他的 ViewGroup ViewGroup ViewGroup View View View View
组件的两种控制方式 在 XML 布局文件中通过 XML 属性进行控制 <Button android:id="@+id/btn_logo_magic android:layout_width="fill_parent android:layout_height="wrap_content android:text=" 试试手气 /> 在 Java 代码中通过方法进行控制 Button button = new Button(this); button.settext(" 试试手气 "); button.setlayoutparams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
组件常用的 XML 属性 android:id android:layout_width android:layout_height android:text android:backgroud 设置组件的唯一标识 Java 代码可以通过 findviewbyid 方法来获取该组件对象 android:id="@+id/btn_logo_magic 设置组件宽度, 值为 fill_parent 或者 match_parent 或者 wrap_content android:layout_width="fill_parent 设置组件高度, 值同上 android:layout_height="wrap_content 设置组件上要显示的文本 android:text=" 试试手气 " 设置组件的背景, 可以是某张图片或者是某种颜色 android:background= #111 android:background="@drawable/btn_green"
组件常用的 XML 属性 android:layout_margin [Top Bottom Left Right] android:layout_padding [Top Bottom Left Right] android:visibility android:onclick 设置组件的外边距值 [ 上下左右 ] android:layout_marginleft="10dp 设置组件的内边距值 [ 上下左右 ] android:layout_paddingleft="10dp 设置组件的可见性, 值为 VISIBLE 或者 INVISIBLE 或者 GONE android:visiblity= GONE 为组件的单击事件绑定监听器 android:onclick="btn_about_back"
盒子模型
常用组件 文本框 TextView 文件 :res/activity_about.xml <TextView android:id="@+id/tv_aboutus_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:text=" 关于作者 " android:textcolor="@color/text_default" android:textsize="@dimen/text_titlebar_center" />
常用组件 按钮 Button 文件 :res/activity_logo.xml <Button android:id="@+id/btn_logo_magic" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:layout_marginright="10dp android:background="@drawable/btn_style_green" android:onclick="btn_logo_magic" android:text=" 试试手气 " android:textcolor="@color/text_default" android:textsize="@dimen/btn_big_textsize" />
常用组件 图片 ImageView 文件 :res/activity_about.xml <ImageView android:id="@+id/iv_about_version" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_centervertical="true android:src="@drawable/settings_version" />
其他常用组件 编辑框 EditText 提示消息 Toast 单选按钮 RadioButton 复选框 CheckBox 列表视图 ListView 网格视图 GridView 进度条 ProgressBar 拖动条 Seekbar 日期选择器 DatePicker 时间选择器 TimePicker 选项卡 TabHost 滚动视图 Scrollview 还有一大波组件没有列出来 更多内容参考 [Android 开发者网站 :User Interface]
Android 中的容器组件 ( 布局管理器 ) 大致有 : LinearLayou: 线性布局 RelativeLayout: 相对布局 AbsoluteLayout: 绝对布局 FrameLayout: 帧布局 容器组件 TableLayout: 表格布局 GridLayout: 网格布局 (Android4.0 新增 )
<LinearLayout android:id="@+id/rl_about_copyright" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:gravity="center_horizontal" android:orientation="vertical" > <TextView android:id="@+id/tv_about_copyright" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="copyright 2013 Tsinghua University." android:textcolor="@color/text_default" android:textsize="12sp" /> <TextView </LinearLayout> android:id="@+id/tv_about_copyrights" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginbottom="8dp" android:text="all Rights Resverved. " android:textcolor="@color/text_default" android:textsize="12sp" /> 线性布局
<RelativeLayout android:id="@+id/rl_shake_result" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="60dp" android:background="@drawable/shake_match_ok" > <ImageView android:id="@+id/iv_shake_magic8" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignparentleft="true" android:layout_marginleft="2dp" android:layout_centervertical="true" android:src="@drawable/magic8" /> <TextView android:id="@+id/tv_shake_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_torightof="@+id/iv_shake_magic8" android:padding="10dp" android:text="you are lucky!" android:textcolor="#fff" /> </RelativeLayout> 相对布局
菜单 Menu 菜单和其他组件一样, 也有两种创建方式, 既可以通过 XML 文件声明, 也可以通过 Java 代码来完成 不论使用哪种方式, 都要重写 oncreateoptionsmenu 方法 文件 src/edu/thu/magic/logoactivity.java [ 代码片段 ] public boolean oncreateoptionsmenu(menu menu) { menu.add(0, 0, 0, " 关于作者 ");//groupid,itemid,order menu.add(0, 1, 0, " 关闭音效 "); }
菜单事件处理 重写 onoptionsitemselected 方法对菜单事件进行处理 文件 src/edu/thu/magic/logoactivity.java [ 代码片段 ] public boolean onoptionsitemselected(menuitem item) { if (item.getitemid() == 0) { // 点击了 关于作者 } else { // 点击了 关闭音效 } return true; }
胡家威 计研135班 http://hujiaweibujidao.github.io/