源码左侧标签,从上到下:Project、Structure、Captures、Favorites。
Project标签,表示该项目的目录结构。
Structure标签,表示该代码的内部方法结构。
手动重新编译:
- Build→Make Project,编译整个项目下的所有模块。
- Build→Make Module ***,编译指定名称的模块。
- Build→Clean Project,然后选择菜单Build→Rebuild Project,先清理项目,再对整个项目进行重新编译。
前一个为AVD Manager按钮,模拟器管理的管理窗口;后一个为SDK Manager,SDK版本的管理窗口。
Android工程创建分两个层级:
- File→New→New Project:创建新的工作空间
- File→New→New Module:创建新的模块,即单独的APP工程
manifests子目录下的AndroidManifest.xml,是APP的运行配置文件。
java子目录下第一个包为java源代码,而后两个为java测试代码。
res子目录下:
- drawable目录存放的是图形描述文件与用户图片
- layout目录存放的是APP页面的布局文件
- mipmap目录存放的是启动图标
- values目录存放的是一些常量定义文件,strings.xml为字符串常量、dimens.xml为像素常量、colors.xml为颜色常量、styles.xml为样式风格定义
Gradle Script主要是工程的编译配置文件。常用的为模块级别的build.gradle(分为项目级与模块级)
build.gradle(Module:app)代码及解释:
apply plugin: 'com.android.application'
android {
//指定编译用的SDK版本号
compileSdkVersion 28
//指定编译工具的版本号。这里的头两位数字必须与compileSdkVersion保持一致,具体的版本号可以在sdk安装目录的sdk\build-tools下找到
buildToolsVersion "28.0.1"
defaultConfig {
//指定该模块的应用编号,即APP的包名。该参数为自动生成,无需修改
applicationId "com.example.myapplication"
//指定App适合运行的最小SDK版本号
minSdkVersion 17
//指定目标设备的SDK版本号,即该App最希望在哪个版本的Android上运行
targetSdkVersion 28
//指定App的应用版本号
versionCode 1
//指定App的应用版本名称
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
//指定是否开启代码混淆功能。
minifyEnabled false
//指定代码混淆规则文件的文件名
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
//指定App编译的依赖信息
dependencies {
//指定应用jar包的路径
implementation fileTree(dir: 'libs', include: ['*.jar'])
//指定编译Android的高版本支持库
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//指定单元测试编译用的junit版本号
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
实例:在代码中操纵控件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/tv_hello"/>
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.myapplication;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView方法直接引用了布局文件的名字activity_main,往App界面内填充activity.xml的布局内容
setContentView(R.layout.activity_main);
//获取名字为tv_hello的TextView控件
TextView tv_hello = (TextView)findViewById(R.id.tv_hello);
//给TextView控件设置文字内容
tv_hello.setText("今天的天气真热啊,火辣辣的");
//给TextView控件设置文字颜色
tv_hello.setTextColor(Color.RED);
//给TextView控件设置文字大小
tv_hello.setTextSize(30);
}
}
(摘自《Android Studio 开发实战 从零基础到App上线(清华大学出版社)》)