学校开了一门移动应用开发课程,我一开始兴趣盎然,但是看到使用的环境是 Java 8 的时候心就凉了一半,在询问老师的意见之后决定使用现在比较常用的Android Studio完成学习,特此记录自学之路。
Android Studio的下载和项目创建平平无奇,唯一可能有问题的就是gradle文件的下载,如果没翻墙的话需要手动下载和配置,在此不多介绍。
1. 目录介绍
创建完目录后打开发现人麻了,因为Android Studio主张前后端分离,而且文件众多,这导致了它的项目文件十分复杂,对初学者不太友好。
manifests 存放配置文件的地方
java 存放源代码的地方
com.example.myappliction 平时写的代码在这里
MainActivity 源代码写这里
com.example.myappliction(androidTest) 用于测试
com.example.myappliction(test) 用于测试
res 存放资源文件的地方
drawable 放图片资源的地方
layout 放布局文件的地方
mipmap 放图标的地方
values 放常量资源(颜色等)
当然安卓也支持project视图,平时也可以用这个视图。
2. 创建虚拟机
打开虚拟机管理器,创建新的虚拟机。
选择手机机型,这里好像都是Google的手机。
选择操作系统,这里我就随便选一个。
最后选择横竖屏。
创建完毕后点击运行即可完成虚拟机的启动。
3. Hello World
联系第一部分的内容,如果想要做一个hello world界面,首先需要做什么?
编辑布局
所以应该在layout中编辑activity_main.xml
文件,我们改变一下背景颜色和字体大小:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#999">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
结果如下:
4. 一些理解
我们已经完成了一个hello world了,但是这个hello world是怎么出来的呢?
大概是这么一个流程:
我们点击运行程序
=> 电脑读取配置文件AndroidManifest.xml(注册信息)
=> 设置内容布局(MainActivity.xmlsetContentView(R.layout.activity_main)
)
=> 读取layout中的布局
=> 渲染出页面