1.1,安卓的体系结构
1.2,Dalvik VM 和传统JVM 的区别
1,传统JVM
编写.java文件 编译为.class文件
打包成.jar文件,将java源码编译为class文件,然后将一组class打包成jar。
基于栈结构,栈存储于内存中,所以需要用到内存空间
2,Dalvik VM
编写.java文件 编译为.class文件 打包成.dex文件
打包成.apk文件。将一组class编译为一个dex文件,然后有的平台为了更高
的效率会将一个dex转换成odex文件。将N多个class文件编译为一个dex,这
样执行效率会比较快,更适合在移动平台运行。基于寄存器的结构,寄存
器在cpu中,所以用到cpu的空间
1.3,ART 模式概述
ART模式是Android Runtime的简称,从Android 4.2开始出现。在Android
4.2以前,安卓手机系统的应用程序均在DalvikJava的虚拟机上运行,这种运行模式是还要依靠一个编译器来实现与应用程序
的沟通。应程序每次运行时,都需要将程序内的代码及时转变为机器码才能运
行,这无形中多附加了一道手续,这就造成了耗电相对较快、占用内存大、即
使是旗舰机用久了也会卡顿严重的现象。
ART模式通过在安装应用程序时,自动对程序进行代码预读取编译,让程
序直接编译成机器语言,免去了Dalvik模式要时时转换代码,实现高效率、省
电、占用更低的系统内存、手机运行流畅。但凡事总有正反两面,ART在解决
了该问题的同时,同时也有如:会占用略高一些的存储空间、安装程序时要相
比普通Dalvik模式要长一些时间来实现预编译。
1.4,Android 搭建开发环境
1,JDK 的安装和Java 环境变量设置
(1),下载JDK(Java Development Kit)
(2),配置JAVA_HOME
(3),配置path和CLASSPATH
2,下载、解压ADT(Android Deveelopment Tools)
1.5,SDK(Software Development Kit)目录
1.6,创建模拟器
1.7,adb ( android debug bridge ) 安卓调试桥
在path 环境变量中, 配置 adb 的目录,这样 才可以 使用adb 命令
adb的常用命令:
1.8,Android应用程序的目录结构
1.9,Android中常见的布局
1,线性布局:所有的 控件都是 串在 一条线上的, 这条线, 可能 是横着 的,也可能是竖直的 .
2,相对布局:控件摆放的位置都是相对的. 默认的都是相对于 父控件 . 你也可以自己去指定相对于谁…
3,表格布局:类似于 以前学过的html 中的table 表格
4,帧布局:控件的摆放是一层一层的, 后摆的 会将先摆放的给 盖住.
5,绝对布局:控件的位置 是 写死的. 代码 扩展性很差. 这种是 基本上是废弃的状态.
2.0,Android中单位
1,dp: dip 独立设备像素(经过系统换算过的 ), 这个单位 主要用于 指定控件的 宽高 使用
2,sp: 专门用来指定 文字的大小的
2.1,单击事件的四种方式
1,给控件直接添加 onclick 去实现.
2,通过 给,控件 添加id ,然后 拿到控件, 设置 点击事件的监听器
3,设置内部类
4,让activity类直接去实现 接口.
//布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="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" >
<Button
android:onClick="anniuyi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮一" />
<Button
android:id="@+id/btn_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮二" />
<Button
android:id="@+id/btn_third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮三" />
<Button
android:id="@+id/btn_fourth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮四" />
</LinearLayout>
//Activity
package com.itheima.clickmethod;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/*
* 第一种: 现在用的多一点.. 以后 进入 公司用的少一点..
*
* 这里 2, 3,4 是推荐使用的
* 第二种:
* 第三种:
* 第四种:
*
*/
public class MainActivity extends Activity implements OnClickListener {
Button btn_second;
Button btn_third;
Button btn_fourth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//把 按钮2, 3, 4 都初始化一下
btn_second = (Button) findViewById(R.id.btn_second);
btn_third = (Button) findViewById(R.id.btn_third);
btn_fourth = (Button) findViewById(R.id.btn_fourth);
//第二种方式 //匿名内部类
btn_second.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("按钮二 的 被点击了 ");
}
});
btn_third.setOnClickListener(new ThirdListener());
//第四种
btn_fourth.setOnClickListener(this);
btn_fourth.setOnClickListener(this);
btn_fourth.setOnClickListener(this);
btn_fourth.setOnClickListener(this);
btn_fourth.setOnClickListener(this);
btn_fourth.setOnClickListener(this);
btn_fourth.setOnClickListener(this);
}
//内部类
private class ThirdListener implements OnClickListener{
@Override
public void onClick(View v) {
System.out.println("按钮三 的 被点击了 ");
}
}
//第一种
public void anniuyi(View v){
System.out.println("按钮一 的 方法执行了 ");
}
@Override
public void onClick(View v) {
//都会进来 ...
int id = v.getId();
switch (id) {
case R.id.btn_fourth:
break;
default:
break;
}
}
}
2.2,Intent(意图)
1,意图对象 , 包含 动作和 数据
2,权限
2.3,短信发送器
3.1,布局示例
1,线性布局
示例:
//小米计算器
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|right"
android:hint="0"
android:lines="3"
android:paddingBottom="10dp"
android:paddingRight="25dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="c"
android:textColor="#F18300" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="15dp"
android:text="DEL" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="/" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="*" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="7" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="8" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="9" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="4" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="5" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="6" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="3dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="30dp"
android:text="0" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="25dp"
android:text="."
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#F18300"
android:gravity="bottom|right"
android:paddingBottom="15dp"
android:paddingRight="25dp"
android:text="="
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
2,相对布局
示例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中间" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/center"
android:layout_centerInParent="true"
android:text="上" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/center"
android:layout_centerInParent="true"
android:text="下" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="@id/center"
android:text="左" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@id/center"
android:text="右" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左上" />
<Button
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右上" />
<Button
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右下" />
<Button
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左下" />
</RelativeLayout>
3,表格布局
示例:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:src="@drawable/qq"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入QQ号码" />
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入QQ密码" />
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2" />
</TableRow>
</TableLayout>
4,帖布局
示例:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:gravity="bottom|right"
android:background="#ff0000"
android:text="111111"
android:layout_width="200dip"
android:layout_height="200dip"/>
<TextView
android:gravity="bottom|right"
android:background="#00ff00"
android:text="2222"
android:layout_width="150dip"
android:layout_height="150dip"/>
<TextView
android:gravity="bottom|right"
android:background="#0000ff"
android:text="33333"
android:layout_width="100dip"
android:layout_height="100dip"/>
</FrameLayout>
5,绝对布局
示例:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="158dp"
android:layout_y="65dp"
android:text="Button" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="97dp"
android:layout_y="238dp"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="202dp"
android:layout_y="134dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</AbsoluteLayout>