使用Android Studio创建应用


  1. 在 Android Studio 里面, 创建一个新的工程:
  • 如果没有打开的工程, 请在 Welcome 界面, 点击 New Project.
  • 如果已经有打开的项目, 在 File 菜单, 选择 New Project
  1. 填写屏幕上的字段, 点击 Next. 如果你填写相同的内容是很容易跟上这些课程的。
  • Application Name 是展示给用户的应用名称. 这个工程,可以使用 "My First App."
  • Company domain(公司域名) 提供将要附加到包名的限定词;Android Studio 会为你每个新建的项目记住这个限定词。
  • Package name 是项目的一个合适的名称(遵循Java变成语言包名的规则)。你的包名在安卓系统的所有包里必须是唯一的。你可以独立编辑这个值。
  • Project location 是放置工程文件的一个文件夹。
  1. 在 Select the form factors your app will run on, 选择 Phone and Tablet.
  2. For Minimum SDK, select API 8: Android 2.2 (Froyo). 所需的最低SDK版本是你的App支持的最早的安卓系统版本,称作使用的API级别。为了支持更多的设置,你应该设置设置成可用的最低版本,去使用它的核心功能集。如果你的App的某些功能仅能运行在新版本的安卓系统上,它也不是App的核心功能集,那么你可以在支持的系统版本运行时再启动它。(更多请参考 Supporting Different Platform Versions).
  3. 其他的选项(TV,Wear ,and Glass)无需选择,点击 Next.

Activities

An activity is one of the distinguishing features of the Android framework. Activities provide the user with access to your app, and there may be many activities. An application will usually have a main activity for when the user launches the application, another activity for when she selects some content to view, for example, and other activities for when she performs other tasks within the app. See Activities for more information.

  1. 在 Add an activity to <template> 下, 选择 Blank Activity ,然后点击 Next.
  2. 在 Customize the Activity 下, 把 Activity Name 改成 MyActivity. 把 Layout Name 改成 activity_my, 把 Title 改成 MyActivity.  Menu Resource Name 是 menu_my.
  3. 点击 Finish 按钮创建工程.

现在你的安卓项目是一个基本的“Hello World”App,包含了一些基本的文件。现在花一些时间去学习下最重要的那些部分。


app/src/main/res/layout/activity_my.xml

Android Studio 将会使用两种方式来展示这个文件 :文本视图和屏幕UI预览。这个文件包含一些素材设计库的默认页面元素,包括app bar 和浮动操作按钮。

app/src/main/res/layout/content_my.xml

activity_my.xml 里面, 包含了一些设置和一个Tex

tView元素。这个TextView元素显示一个消息"Hello world!".

app/src/main/java/com.mycompany.myfirstapp/MyActivity.java

会有一个tab显示出来。当你选择了这个文件,你会看到你为这个activity创建的类。当你构建然后运行这个App,那么这个activity的类就会启动activity ,加载布局文件,然后显示"Hello World!" app/src/main/AndroidManifest.xml

这个文件描述了App的基本特征,定义它的每一个组件。 app/build.gradle

build.gradle 。通常你仅关心模块的build.gradle 。这是你App的构建依赖关系,包括默认配置设置。Usually, you're only interested in the build.gradle file for the module, in this case the 

app or application module. This is where your app's build dependencies are set, including the 

defaultConfig settings:

compiledSdkVersionapplicationIdminSdkVersiontargetSdkVersion

  •  是你测试过的你的App支持的最高版本。一旦有了新版本,你必须在新版本做测试,并且更新这个值去匹配最新的版本,从而利用新的平台功能。indicates the highest version of Android with which you have tested your application. As new versions of Android become avaiFor more information, read Supporting Different Platform Versions.

See Building Your Project with Gradle for more information about Gradle.

/res 的子目录是应用程序的资源目录:

drawable-<density>/

绘图资源( drawable resources)目录, 除了启动图标。other than launcher icons, designed for various 

densities.

layout/

activity_my.xml中定义的的用户界面文件。它描述了MyActivity类的基本布局。 menu/

App menu项的文件. mipmap/

启动图标在这个文件夹里。这个文件夹里包含了默认App的默认图标。 values/

其他XML文件的文件夹,比如定义 String或者颜色的XML文件。