編輯:關於 Android 開發
這是一篇面向零基礎入門者的 Android 速成筆記,從移動通信基礎常識一路鋪到環境搭建、ADB 指令、四大佈局與兩個小案例。
第一代模擬制式(1G):就是「大哥大」那個年代,手機類似於簡單的無線電雙工電台,通話鎖定在一定頻率上,因此用可調頻電台就能竊聽通話。
第二代數字制式(2G):GSM、CDMA 等成熟標準讓通話質量穩定、待機時間合適,並支持彩信業務的 GPRS、上網業務的 WAP,以及各式各樣的 Java 程序。
第三代(3G):3rd Generation 的縮寫,把無線通信與互聯網等多媒體通信結合起來,能處理圖像、音樂、視頻流等多種媒體形式,提供網頁瀏覽、電話會議、電子商務等信息服務。
第四代(4G):包含 TD-LTE 與 FDD-LTE 兩種制式,集 3G 與 WLAN 於一體,可傳輸高質量視頻圖像。4G 系統能以 100Mbps 級別的速率下載,並可在 DSL 與有線電視調制解調器未覆蓋的地區先行部署,再逐步擴展到整個區域。
各代網絡制式對應運營商:
2G:GSM(移動/聯通)、CDMA(電信)
3G:WCDMA(聯通)、TD-SCDMA(移動)、CDMA2000(電信)
4G:FDD-LTE(電信/聯通)、TD-LTE(移動/電信/聯通)
1G:已徹底退出歷史舞台
帶寬差異:2G 屬於撥號上網,約 12.2k 每用戶;3G 屬於寬帶上網,約 384k~2M 每用戶;4G 好比光纖到戶,可達 100M 每用戶。

安卓之父:安迪·魯賓
1)2003 年 10 月,安迪·魯賓牽頭創建了名為 Android 的公司,開始開發一款面向手機端的操作系統。
2)2005 年 8 月,谷歌低調收購了這家公司及其團隊,安迪·魯賓出任 Google 工程部副總裁,繼續負責 Android 項目。
3)2007 年 11 月,谷歌正式對外展示這款名為 Android 的操作系統,並宣布組建全球性聯盟組織,由 34 家手機製造商、軟件開發商、電信運營商及芯片製造商共同構成,搭建起 Android 最早的生態圈。
4)2011 年,Android 在全球市場份額首次超過塞班系統躍居第一,此後主要競爭對手變為 iOS。
版本進化史:1.5 Cupcake(紙杯蛋糕)、1.6 Donut(甜甜圈)、2.1 Eclair(閃電泡芙)、2.2 Froyo(凍酸奶)、2.3 Gingerbread(姜餅)、3.0 Honeycomb(蜂巢)、4.0 Ice Cream Sandwich(冰激凌三明治)、4.1~4.3 Jelly Bean(果凍豆)、4.4 KitKat(巧克力)、5.0 Lollipop(棒棒糖)、6.0 Marshmallow(棉花糖)。

Linux Kernel:Android 基於 Linux 修改而來,底層大多是操作硬件的驅動,如 Display Driver、Audio Drivers 等。
Libraries:用 C 語言編寫、完成 Android 核心功能的類庫,例如 OpenGL|ES(圖形引擎簡化版)、WebKit(瀏覽器內核)、SQLite(輕量級數據庫)、Surface Manager(界面管理器)、Media Framework(多媒體框架)、FreeType(字體類庫)、SGL、SSL(基於 TCP 的安全協議)、libc 等。
Application Framework:應用框架層,全部用 Java 編寫,供開發者調用。
Applications:應用層,我們安裝的所有應用都屬於這一層。
Android Runtime:包含 Core Libraries(核心類庫)與 Dalvik Virtual Machine(虛擬機)。由於 Android 底層是 C/C++ 編寫的 Linux,Java 編寫的應用需要虛擬機才能運行。
jvm:.java -> .class -> .jar
dvm:.java -> .class -> .dex -> .apk
DVM 執行的是 .dex 格式,JVM 執行的是 .class 格式。Android 程序編譯後生成 .class,再由 dex 工具處理成 .dex,最後把資源檔與 .dex 一起打包成 .apk(Android package)。

DVM 效率更高的原因:
1)每個 class 檔都帶有一個 Header 保存初始信息(如生成它的 JDK 版本),而 apk 中的 dex 檔只有一個 Header,所有 class 的初始信息集中存放,效率更高。
2)dex 把所有 class 中的常量統一放進常量池、把所有字節碼放進統一的字節碼常量池。打包時稍慢,但讀取時更快。
3)class 檔存在大量冗余信息,dex 工具會去除冗余並把所有 class 整合進一個 dex,減少 I/O 操作、提升類查找速度。
ART 虛擬機:Android 5.0 以上版本正式開始使用 ART。差別在於:Dalvik 每次運行時都需要通過即時編譯器把字節碼轉為機器碼,拖慢效率;ART 則在應用首次安裝時就把字節碼預先編譯成機器碼,使其成為真正的本地應用,啟動與執行速度顯著提升,代價是需要同時存儲兩份指令,比較耗內存。
SDK(Software Development Kit)即軟件開發工具包。谷歌官方地址需要特殊網絡環境才能訪問,可改用 Android Studio 中文社區官網下載開發工具。
工具包通常包含:Eclipse + ADT 插件、Android SDK、Android Platform-tools、最新的 Android 開發平台,以及最新的模擬器鏡像。

SDK Manager

SDK 文件夾目錄結構
點擊 AVD Manager 圖示,在新窗口點擊 New 按鈕:





第一步:項目編譯打包成 apk。apk 中包含:
.dex 檔案resources.arsc(apk 中的「配置文件」、「佈局文件」、「圖片」的索引)AndroidManifest.xml(同樣原封不動打包)真正被編譯的只有 .java 代碼。
第二步:簽名。然後通過 ADB 運行到 Devices(真機)或 Emulator(模擬器)上。系統判斷兩個應用是否屬於同一款應用,先確認清單文件中 Manifest 標籤的 package 屬性值(應用在系統中的唯一標識)是否相同,再判斷簽名是否相同——兩者都相同,才會出現高版本覆蓋低版本。簽名屬於企業商業機密,是用一個 key 檔案(相當於密鑰)計算出的字符串,不同密鑰生成的結果必然不同,所以一般無法山寨其他公司的應用。
DDMS(Dalvik Debug Monitor Service)是 Android 開發環境中的虛擬機調試監控服務,提供測試設備截屏、針對特定進程查看線程與堆信息、Logcat、廣播狀態信息、模擬電話來電、接收 SMS、虛擬地理坐標等能力。
ADB(Android Debug Bridge,Android 調試橋)用於建立 Android 設備與開發環境之間的連接。模擬器中的操作日誌本來無法在 Windows 平台輸出,正是有了 ADB,LogCat 才能打印出來。
ADB 是一個可執行指令,在 CMD 中使用,使用前需先配置環境變數,例如把 D:\develop\adt-bundle-windows-x86_64_20140101\sdk\platform-tools 加入 PATH。
常用指令:
1、adb install xxx(絕對路徑).apk:安裝指定 apk 到設備
2、adb uninstall 包名:從設備刪除指定應用
3、adb start-server:啟動 adb 進程
4、adb kill-server:結束 adb 進程
5、adb devices:列出與開發環境建立連接的設備
6、adb shell:進入 Linux 命令行,其中 ls 列出當前目錄結構、ps 列出所有進程
7、netstat -ano:查看端口佔用情況
注意:adb 本身也是一個進程,若該進程結束,設備與開發環境的連接會中斷,此時控制台會在嘗試連接約 11 秒後恢復。
佈局檔 res\layout\activity_main.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入號碼" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:text="撥號"
android:onClick="call" />
</LinearLayout>
MainActivity.java:
package com.example.codedemo;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText mEtNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtNumber = (EditText) findViewById(R.id.et_number); // 獲取輸入框對象
}
public void call(View view) {
String number = mEtNumber.getText().toString(); // 獲取號碼
if (TextUtils.isEmpty(number)) {
Toast.makeText(this, "號碼不能為空號", Toast.LENGTH_SHORT).show();
return;
} else {
Intent in = new Intent(); // 創建意圖對象
in.setAction(Intent.ACTION_CALL); // 設置撥號動作
in.setData(Uri.parse("tel:" + number)); // 設置號碼
startActivity(in); // 開啟打電話應用的 Activity
}
}
}
記得添加權限:
<uses-permission android:name="android.permission.CALL_PHONE"/>


<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- android:inputType="phone" 表示只能輸入電話號碼 -->
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入號碼"
android:inputType="phone" />
<!-- android:lines="5" 表示只能輸入 5 行 -->
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入內容"
android:lines="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="send"
android:text="發送" />
</LinearLayout>
package com.example.codedemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText mEtPhone;
private EditText mEtContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件
mEtPhone = (EditText) findViewById(R.id.et_phone);
mEtContent = (EditText) findViewById(R.id.et_content);
}
public void send(View view) {
String phone = mEtPhone.getText().toString();
String content = mEtContent.getText().toString();
// 獲取發送短信的 API
// SmsManager 要使用 android.telephony 包內的,
// 不要使用 android.telephony.gsm 包內的,那個已過時。
SmsManager sm = SmsManager.getDefault();
// 如果短信過長,可以拆分成若干條短短信
ArrayList<String> smss = sm.divideMessage(content);
for (String str : smss) {
/**
* String destinationAddress 對方號碼
* String scAddress 短信服務中心號碼,可傳 null 表示使用默認地址
* String text 短信內容
* PendingIntent sentIntent 發送結果廣播,傳 null 則不回執
* PendingIntent deliveryIntent 對方接收成功後的回執
*/
sm.sendTextMessage(phone, null, str, null, null);
}
}
}
權限:
<uses-permission android:name="android.permission.SEND_SMS"/>

方式一:匿名內部類。佈局中給按鈕加 id,代碼中 findViewById 找到對象後設置監聽器。
<Button
android:id="@+id/btCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="點擊" />
Button mBtCall = (Button) findViewById(R.id.btCall);
if (mBtCall != null) {
mBtCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 點擊事件需要實現的功能
}
});
}
方式二:自定義內部類。
Button mBtCall = (Button) findViewById(R.id.btCall);
MyListener ml = new MyListener();
if (mBtCall != null) mBtCall.setOnClickListener(ml);
public class MyListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// 點擊事件需要實現的功能
}
}
方式三:Activity 實現接口。讓 Activity 實現 OnClickListener,監聽器傳 this。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mBtCall = (Button) findViewById(R.id.btCall);
if (mBtCall != null) mBtCall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// 點擊事件需要實現的功能
}
}
方式四:佈局中直接聲明 onClick。方法簽名必須是 public void send(View view)。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="send"
android:text="發送" />
public void send(View view) {
// 點擊事件需要實現的功能
}
補充:多個 Button 的 onClick 屬性值可以相同,點擊任何一個都會調用同一個方法,通過 id 判斷具體是哪個按鈕被按下。
dp 最終顯示多少像素取決於屏幕密度,屏幕越大,1dp 換算出的像素越多。
320x480 屏幕上:1dp = 1px,160dp = 160px,正好是屏幕寬度的一半。
480x800 屏幕上:1dp = 1.5px,160dp = 240px,同樣是屏幕寬度的一半。
sp 與 dp 類似,只是 sp 專用於字體大小,dp 用於長度大小。
1)LinearLayout(線性佈局)
2)RelativeLayout(相對佈局)
3)FrameLayout(幀佈局)
4)TableLayout(表格佈局)
5)AbsoluteLayout(絕對佈局)
orientation:指定排列方向,horizontal 水平、vertical 垂直gravity:指定當前控件「內容」的顯示位置(left/right/top/bottom)layout_gravity:指定當前控件在父元素中的位置layout_weightSum:把線性佈局的剩餘空間分成 N 份layout_weight:指定當前控件佔 N 份visibility:visible 顯示、invisible 不顯示但佔空間、gone 隱藏且不佔空間權重案例:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:layout_weight="1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="第一個"
android:layout_gravity="right"
android:textSize="20sp" />
<TextView
android:layout_weight="1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="第二個"
android:textSize="20sp"
android:background="#00ff00" />
</LinearLayout>
權重的含義是把線性佈局的剩餘空間分成 N 份。上例中水平方向分為 2 份,兩個 TextView 各佔一份:第一個先佔 100dp,第二個先佔 50dp,剩下的部分再平均分給兩者。

若把權重分為 3 份,並將兩個 TextView 的 android:layout_width 設為 0dp、權重各佔一份,結果如下:

由此可得結論:水平排列並使用權重分配時,一定要搭配把 android:layout_width 設為 0dp,這樣計算最直觀;同理,垂直排列時應搭配 android:layout_height="0dp"。
注意:
1、android:weightSum 可以不寫。若第一個 TextView 權重為 1、第二個為 2,則 weightSum 即為 3,兩者按 1:2 分配。但如果顯式設為 4,相當於把寬度切成四份,前三份按 1:2 分給兩個 TextView,第四塊空著不分配。
2、TextView 若不寫 android:weight,相當於權重為 0,不會分到任何剩餘空間。此時若又把 layout_width 設為 0dp 就會出問題,因為這個 TextView 已經無法顯示。
對齊規則:豎直排列(vertical)時,左右對齊有效、水平居中有效,頂部底部對齊與豎直居中無效;水平排列(horizontal)時,頂部底部對齊與豎直居中有效,左右對齊與水平居中無效。此外,線性佈局中的所有元素永遠不會重疊。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal" >
<TextView android:layout_weight="1" android:layout_width="0dp"
android:layout_height="match_parent" android:background="#ff0000" />
<TextView android:layout_weight="1" android:layout_width="0dp"
android:layout_height="match_parent" android:background="#ffffff" />
<TextView android:layout_weight="1" android:layout_width="0dp"
android:layout_height="match_parent" android:background="#000000" />
<TextView android:layout_weight="1" android:layout_width="0dp"
android:layout_height="match_parent" android:background="@android:color/darker_gray" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical" >
<TextView android:layout_weight="1" android:layout_width="match_parent"
android:layout_height="0dp" android:background="#00ff00" />
<TextView android:layout_weight="1" android:layout_width="match_parent"
android:layout_height="0dp" android:background="#000000" />
<TextView android:layout_weight="1" android:layout_width="match_parent"
android:layout_height="0dp" android:background="#aaa" />
<TextView android:layout_weight="1" android:layout_width="match_parent"
android:layout_height="0dp" android:background="#ff0000" />
</LinearLayout>
</LinearLayout>

特點:所有組件可以疊加;各組件的佈局互相獨立、互不影響;默認位置都在左上角(頂部、左部對齊)。
相對位置屬性:layout_toRightOf(在指定控件右邊)、layout_toLeftOf(左邊)、layout_above(上邊)、layout_below(下邊)
對齊屬性:layout_alignBaseline(與指定控件水平對齊)、layout_alignLeft、layout_alignRight、layout_alignTop、layout_alignBottom
與父容器對齊:layout_alignParentLeft、layout_alignParentTop、layout_alignParentRight、layout_alignParentBottom
居中屬性:layout_centerVertical(垂直居中)、layout_centerHorizontal(水平居中)、layout_centerInParent(完全居中)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/center"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="中間"
android:layout_centerInParent="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上面"
android:layout_above="@id/center"
android:layout_alignLeft="@id/center"
android:layout_alignRight="@id/center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下面"
android:layout_below="@id/center"
android:layout_alignLeft="@id/center"
android:layout_alignRight="@id/center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左邊"
android:layout_toLeftOf="@id/center"
android:layout_alignTop="@id/center"
android:layout_alignBottom="@id/center"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右邊"
android:layout_toRightOf="@id/center"
android:layout_alignTop="@id/center"
android:layout_alignBottom="@id/center"
android:layout_alignParentRight="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左上"
android:layout_toLeftOf="@id/center"
android:layout_above="@id/center"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左下"
android:layout_toLeftOf="@id/center"
android:layout_below="@id/center"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右上"
android:layout_toRightOf="@id/center"
android:layout_above="@id/center"
android:layout_alignParentRight="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右下"
android:layout_toRightOf="@id/center"
android:layout_below="@id/center"
android:layout_alignParentRight="true" />
</RelativeLayout>

組件可以重疊,所有組件默認位置為左對齊、頂部對齊,佈局屬性與線性佈局一致(主要靠 layout_gravity 定位)。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:layout_width="240dp" android:layout_height="240dp"
android:background="#ff0000" android:layout_gravity="center" />
<TextView android:layout_width="200dp" android:layout_height="200dp"
android:background="#00ff00" android:layout_gravity="center" />
<TextView android:layout_width="160dp" android:layout_height="160dp"
android:background="#0000ff" android:layout_gravity="center" />
<TextView android:layout_width="120dp" android:layout_height="120dp"
android:background="#ffff00" android:layout_gravity="center" />
<TextView android:layout_width="80dp" android:layout_height="80dp"
android:background="#8000EE" android:layout_gravity="center" />
<TextView android:layout_width="40dp" android:layout_height="40dp"
android:background="#ffffff" android:layout_gravity="center" />
</FrameLayout>
根節點 <TableLayout/> 的子節點寬為 match_parent、高為 wrap_content。
<TableRow/> 的子節點寬高均為 wrap_content;每個 <TableRow/> 是一行,它的每個子節點是一列。
表格佈局中的節點可以不設置寬高,因為設了也無效,以上預設屬性無法修改。
根節點可設置 android:stretchColumns="1",表示讓第 1 列拉伸填滿屏幕寬度的剩餘空間。
直接通過 android:layout_x、android:layout_y 指定組件的 x、y 坐標。由於缺乏自適應能力,實戰中基本不再使用。
Android 自定義控制項學習筆記
Android 自定義控制項學習筆記 本筆記整理自定義控制項的基本概念、常見系統控制項,以及多個實戰案例,適合想進一步理解 Android 介面客製化的開發者。 自定
FloatingActionButton 隨列表滾動滑入滑出
FloatingActionButton 隨列表滾動滑入滑出FloatingActionButton 是常見的懸浮操作按鈕。makovkastar 的開源實作可讓按鈕依附
深入解析 ListView 聯動:仿餓了麼點餐界面
透視雙 ListView 聯動的實現原理 本文將對「仿餓了麼點餐界面」中兩個 ListView 聯動的效果進行代碼層面的深入剖析。通過拆解大神的經典實現,既能方便自己日後
Android 熱修復:以 Gradle Plugin 1.5 Transform API 改造 Nuwa
以 Gradle Plugin 1.5 Transform API 改造 Nuwa 熱修復流程 Nuwa 是常見的 Android 熱修復方案,但在 Gradle Plu