《Android Studio开发实战》学习(一)- Hello World
背景
这段时间在学习Android Studio开发App 1,实践过程中的心得体会在这里记录一下。
Android Studio的安装
Android Studio是基于IntelliJ IDEA的免费Android开发环境 2,它的最新版本(Android Studio Dolphin | 2021.3.1)可以在Google的官网下载,下载地址:Download Android Studio and SDK tools | Android Developers。
下载得到一个tar包的文件android-studio-2021.3.1.17-linux.tar.gz
,解压后进入bin目录,然后运行
tar -zxf android-studio-2021.3.1.17-linux.tar.gz
cd android-studio/bin/
./studio.sh
接着就按照弹出的对话框的提示进行下载各个组件,如果某个组件下载失败,可以退出再多运行./studio.sh
几次。
也可以输入命令sudo ./studio.sh
,这样在后面就可以避免生成apk时因为目录读写权限问题而报错。两个命令最好坚持使用一个,因为生成的AndroidStudioProjects文件夹所在的位置不同。
Android Studio的启动和运行
接着上一步,打开Android Studio,第一次运行会出现欢迎界面。
接下来,点击New Project新建一个项目,出现对话框,选Phone and Tablet面板下的Empty Activity
,项目名称写HelloWorld
,语言选Java
,Minimum SDK选API 16: Android 4.1 (Jelly Bean)
(大约100%手机都能运行),然后点Finish即到达其项目界面。
运行小应用Hello World
在Android Stuido项目界面左侧是当前工程的目录结构,包括manifests、java、res三个文件夹,一开始主要用到两个文件,一个是代码文件MainActivity.java
,位于java/com.example.myapplication
文件夹下;另一个是布局文件activity_main.xml
,位于res/layout
文件夹下。
MainActivity.java
代码如下:
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- 测试中文输入 -->
android:text="你好世界!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这样生成的app默认在手机上是纵向显示,如果想要横屏显示,则需要编辑manifests/AndroidManifest.xml
文件,在<activity>
标签中加一个属性android:screenOrientation="landscape"
3。
manifests/AndroidManifest.xml
全部代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<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.HelloWorld"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
然后需要准备Android手机一个,Android Studio自带的手机模拟器不推荐使用,因为通常半小时都不会有反应,而且最终还是要到真机上模拟,所以最好用Android Studio生成apk文件,然后传输到手机上安装运行。
APK文件的生成方法
从菜单栏中选择Build - Generate Signed Bundle / APK,这时候对话框询问选Android APP Bundle还是APK,选APK。下一步需要一个APK签名文件,如果没有可以新建一个,记录好密码(包括别名和密码)和该文件存放的路径,填入对话框中。
APK签名的窗口页面点击Next,Build Variants调试的话选debug,发布选release,点击Finish。查看AndroidStudioProjects
的app
文件夹,就会发现在其中多个一个debug
文件夹,其中生成了app-debug.apk
。
然后把这个apk文件传输到手机,安装运行。运行结果如下:
这样,Hello World App就制作成功了。