Android 开发

Android SDK版本

Android版本号 对应API
Android 13 33
Android 12 31
Android 11 30
Android 10 29
Android 9 28
Android 8 26/27
Android 7 24/25
Android 6 23
Android 5 21/22
Android 4.4 19/20

DEBUG

Android采用Log工具打印日志,它将各类日志划分为五个等级:

  • Log.e:表示错误信息
  • Log.w:表示警告信息
  • Log.i:表示一般消息
  • Log.d:表示调试信息,如在程序中插入一些变量的打印,方便调试
  • Log.v:表示冗余信息

设备与AndroidStudio使用ADB(Adroid Debug Bridge)连接

Android开发语言

  • Java
  • Kotlin

APP 工程目录结构

APP工程分为两个层次

  • 项目
  • 模块:模块依附于项目,每个项目至少有一个模块

一般意义上的“编译运行APP”指的是运行某个模块,而非整个项目,模块对应了实际的APP。

APP项目

app

manifests

该目录下面只有一个AndroidManifest.xml文件,它是APP的运行配置文件。

每个应用的根目录中必须包含一个AndroidManifest.xml文件,并且文件名一模一样,系统根据里面的内容运行APP的代码,显示界面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<!-- activity节点指定了该APP拥有的活动页面信息,其中拥有adroid.intent.action.MAIN的activity说明它是入口页面 -->
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
  • android:allowBackup:是否允许应用备份。允许用户备份系统应用和第三方应用的apk包和应用数据,以便在刷机或者数据丢失后恢复应用,用户可通过adb backup和abd restore来进行对应用数据的备份和恢复。为true表示允许,false表示不允许。
  • android:icon:指定APP的默认图标。
  • android:label:指定APP名称。
  • android:roundIcon:指定APP圆角图标。
  • android:supportsRtl:是否支持从右往左的文字排序
  • android:theme:指定APP显示风格
java

该目录下有三个com.example.myapp包,其中第一个包存放当前模块的java源代码,后面两个包存放测试用的Java代码。

  • Activity是一个应用程序组件,提供一个屏幕,用户可以利用它完成某项任务。
res

该目录下有4个子目录,存放当前模块的资源文件

  • drawable:存放图形描述文件与图片文件
  • layout:存放APP页面的布局文件
  • mipmap:存放APP的启动图标
  • values:存放一些常量定义文件
    • strings.xml:字符串常量
    • dimens.xml:像素常量
    • colors.xml:颜色常量
    • styles.xml:样式风格常量

Gradle Scripts

Gradle是一个项目自动化构建工具,帮我们做了依赖、打包、部署、发布、各种渠道的差异管理等工作。

  • build.gradle:该文件分为项目级与模块级两种,用于描述APP的编译规则。
  • proguard-rules.pro:该文件用于描述Java代码的混淆规则
  • gradle.properties:该文件用于编译工程的命令行参数,一般无需改动。
  • settings.gragle:该文件配置了需要编译哪些模块。初始化内容include’:app’,表示只编译app模块。
  • local.propeties:项目的本地配置文件,它在工程编译时自动生成,用于描述开发者计算机的环境配置,包括SDK本地路径、NDK的本地路径。
build.gradle

项目级别的build.gradle指定了当前项目的总体编译规则。

模块级别的build.gradle对应于具体模块,每个模块都有自己的build.gradle,它指定了当前模块的详细编译规则。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
android {
//指定编译用的SDK版本号。如32表示使用Android 12
compileSdk 32
//指定编译工具的版本号,这里的头两位数字必须与compileSdk保持一致,具体的版本号可在sdk安装目录的"sdk\build-tools"下找到
buildToolsVersion "33.0.0"
defaultConfig {
//指定该模块的应用编号,也就是APP包名
applicationId "com.example.myapplication"
//指定APP合适运行的最小SDK版本号。24表示至少要在Android 7上运行
minSdk 24
//指定目标设备的SDK版本号,表示APP最适合在哪个版本的Android上运行
targetSdk 32
//指定APP应用版本号
versionCode 1
//指定应用版本名称
versionName "1.0"
//单元测试类
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
//指定混淆文件
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
//指定APP依赖信息
dependencies {
//指定引用jar包的路径
implementation fileTree(dir:'libs', include:['*.jar'])
//指定编译Android的高版本支持库。如AppCompatActivity必须指定编译appcompat库
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

每个版本的AndroidStudio都有对应的Gradle版本,只有二者的版本正确对应,APP工程才能成功编译

Activity的创建与跳转

创建新的APP页面

  1. 在layout目录下创建XML文件\src\main\res\layout\activity_user.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/page1_name"/>
    </LinearLayout>
  2. 创建与XML文件对应的Activity即Java代码\src\main\java\com\example\myapplication\UserActivity.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package com.example.myapplication;

    import android.os.Bundle;

    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;

    public class UserActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user);

    }
    }

  3. 在AndroidManifest中注册配置Activity\src\main\AndroidManifest.xml

    1
    <activity android:name=".UserActivity"/>
  4. 在指定Activity中编写代码跳转到该页面

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tv = findViewById(R.id.tv);
    tv.setText("你好,世界!");

    Button bv = findViewById(R.id.button);//获取ButtonView
    //为Button设置一个点击事件监听
    bv.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intent = new Intent();
    intent.setClass(MainActivity.this, UserActivity.class);
    startActivity(intent);
    }
    });
    }
    }

快捷生成页面

右键点击:arrow_right:new:arrow_right:Activity:arrow_right:Empty​Activity

通过快捷方式可以一键生成Activity与对应的Layout文件

Android简单控件

文本

设置文本值

设置值文本有两种方式

  • 在Layout文件中通过属性android:text设置
  • 在Activity文件中通过调用视图对象的setText方法设置

strings.xml中定义的文本可以在Layout和Activity文件中使用,当需要修改时直接修改strings.xml就可以了

Layout中使用android:text="@string/name"引用;Activity中使用R.string.name引用

  • \src\main\res\values\strings.xml
1
2
3
4
<resources>
<string name="app_name">chapter03</string>
<string name="app_hello">你好,世界</string>
</resources>
  • \src\main\res\layout\activity_text_view.xml
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_hello"/>
</LinearLayout>
  • \src\main\java\com\example\chapter03\TextViewActivity.java
1
2
3
4
5
6
7
8
9
public class TextViewActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
TextView tv_hello = findViewById(R.id.tv_hello);//获取文本视图
tv_hello.setText(R.string.app_hello);//设置文本值
}
}

设置文本大小

设置文本大小有两种方式:

  • 在Activity文件中通过属性android:textSize指定文本大小
    • px:像素,分辨率越高每个单位长度所含的像素点就越多,屏幕就越清晰
    • dp:设备独立像素,dp与屏幕分辨率无关,只与屏幕尺寸有关,相同尺寸的屏幕其每dp的物理长度是相等的。
    • sp:sp与dp类似,sp专门用来设置字体大小,会随着系统字体大小而改变。
  • 在Layout文件中通过调用视图对象的setTextSize方法指定文本大小

dpi(像素密度)

dpi指的是屏幕上每英寸(1英寸=2.54厘米)距离中有多少个像素点

以一块4.95英寸(对角线长度),分辨率为1920*1080的屏幕为例:

dpi计算:

  1. 计算对角线像素数量:1920^2+1080^2=2202^2
  2. 计算dpi:2202/4.95=445
  3. 得到设备的dpi为445(每英寸包含445个像素点)

density(密度)

density指每平方英寸(2.54^2平方厘米)中含有的像素点

dpi为445,则density为445^2=198025。

dip/dp(设备独立像素)

dpi是开发中使用的长度单位之一,它最后仍然需要转换为px

1px=1dip*dpi/160

对于相同分辨率的屏幕,尺寸越大,同pd的组件占用的屏幕比例越小

相同尺寸的屏幕,即使分辨率不同,同pd的组件占用屏幕比例也相同

  • \src\main\res\layout\activity_text_size.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<!-- px -->
<TextView
android:id="@+id/tv_px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_hello"
android:textSize="30px"/>
<!-- dp -->
<TextView
android:id="@+id/tv_dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_hello"
android:textSize="30dp"/>
<!-- sp(字体推荐使用sp作为单位) -->
<TextView
android:id="@+id/tv_sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_hello"
android:textSize="30sp"/>

</LinearLayout>

设置文本颜色

  • 在XML文件中通过属性android:textColor指定文本颜色,色值由透明度alpha和RGB联合定义。

  • 色值有八位十六进制数六位十六进制数两种形式,如色值FFEEDDCC中FF表示透明度,EE表示红色度,DD表示绿色度,CC表示蓝色度。

  • \src\main\res\layout\activity_text_color.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/tv_code_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/code_color"
android:textSize="30sp"/>
<TextView
android:id="@+id/tv_code_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/code_color"
android:textSize="30sp" />
<TextView
android:id="@+id/tv_code_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/code_color"
android:textSize="30sp"
android:textColor="#FF0000"/>
<TextView
android:id="@+id/tv_code_res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/code_color"
android:textSize="30sp" />
</LinearLayout>
  • \src\main\java\com\example\chapter03\TextColorActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class TextColorActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_color);

TextView tv_code_color = findViewById(R.id.tv_code_color);
tv_code_color.setTextColor(Color.BLUE);//使用系统预设颜色代码

TextView tv_code_eight = findViewById(R.id.tv_code_eight);
tv_code_eight.setTextColor(0xFF00FF00);//使用8位16进制颜色代码

TextView tv_code_res = findViewById(R.id.tv_code_res);
tv_code_res.setBackgroundResource(R.color.teal_200);//使用资源自定义颜色代码
}
}