1、Hello World


  要创建第一个程序的话,当然还是从Hello World开始的好。既然Eclipse与Andriod Studio都没有用过,所以,选择其中带单词“Andriod”的工具似乎是上选:)——尽管AndriodStudio其实是基于IntelliJIDEA构建的,尽管当前的版本号还只是0.4.3。这一点,可以从Andriod SDK的官方网站提供的示例中得到体现,目前,官网暂时只提供了基于Andriod Studio的项目代码:http://developer.android.com/intl/zh-cn/samples/index.html。

  AndriodStudio的下载和安装无需说明。鉴于我只安装了Win7(32位)操作系统,所以,可能存在与您的操作习惯不一样的情况,敬请无视之。另,本人是初学者,闻道有先后,欢迎高人指点并提出意见建议。

  下面从创建HelloWorld程序做起,边学边记边悟,Let’sGo!

  1.1 打开Andriod Studio

  AndroidStudio载入成功后,会出现欢迎窗口(如下图所示)。


  其中,有三部分内容需要说明。一是左列的“Recent Projects”,显然为最近打开过的项目。二是“Quick Start”,有些快捷操作的意味,其中“New Project”为新建项目,“Import Project”为导入项目,“Open Project”为打开项目,“Check out from Version Control”为从各种VCS中取回数据,“Configure”为系统配置,“Docs And How-Tos”链接到了官网的帮助文档。三是底部显示了Andriod Studio的版本号,点击“Check”可以检查是否存在新版本并升级。

  说实在话,由于暂时还没用到“Import Project”,所以不知此项跟“Open Project”有何区别;“Check out from Version Control”则由于尚未安装subverion等,所以不知接下来如何操作:)

  不过,创建一个HelloWorld程序,用“NewProject”就够了,所以,就从新建开始吧。

  1.2新建项目

  新建项目对话框是这个样子的:

  Applicationname:是其在应用商店中的显示名。

  Module name:其实就是项目名,可以与Application Name一致。

  Package name:必须为合法的、唯一的Java包的名称。而且要记得,这个名称是用来鉴别不同版本间的应用为同一个应用的关键标示符。Package Name就像域名倒过来一样,假设你的网站叫your.com,你要开发的这个应用比如叫app,那么这个应用的url(如果可以利用浏览器访问的话)就应该是app.your.com,所以Package Name可以是这个样子的:com.your.app。

  Project location:项目的存放位置。

  Minimumrequired SDK:应用所支持的Andriod平台的最小版本号。把目标应用定位于API8(即Android 2.2)以上,等同于该应用将能在约95%的市场中运行良好。

  Target SDK:确定能运行该应用的最高版本平台。这意味着,你需要确认在此平台中做过测试,而系统不需要考虑您的应用向前兼容的问题。也意味着,如果您的应用所支持的API最高版本比Andriod系统当前使用的API版本低的话,那么恭喜,您的应用可能已经out了。

  Compile with:用来编译代码的API。

  LanguageLevel:使用的JDK语言版本。

  Theme:主题。

  Createcustom launcher icon:创建自定义图标。

  Createactiveity:创建Activeity。Andriod的应用程序基本可分为4类,一类是前台应用,二类是后台应用,三类是前后台都会有的应用,四类是Widget和Live Wallpaper。Activity,我的理解,是可以用肉眼看到的部分,也即前台应用。

  Mark thisproject as a library:将此项目标记为库。

  Support Mode:支持模式——Gridlayout(网格布局),Fragments,Navigation Drawer,Action Bar。都需要额外的库给予支持。

  1.3设置应用图标

  点击“Next”按钮,就会进入设置应用图标的对话框。

  Foreground:设置图标的格式——图片、剪贴画、文本。下面就以图片为例进行说明。

  Image file:图片文件。

  Additionalpadding:图片与边框之间的空白。

  Foregroundscaling:裁剪,或居中。

  Shape:边框形状——无、方形、圆形。

  BackgroundColor:背景色。

  1.4选择模板

  BlankActivity:空白Activity。包含操作栏和一个可选的导航条(如或横、或竖的Tab)。

  FullscreenActivity:全屏Activity。可在系统UI(状态栏、导航条)与用户UI(操作栏)之间正常切换。

  Master/DetailFlow:主辅数据流。分为左右两个部分,用两个fragment实现。这个模板最低需要SDK11才行。

  1.5还要起个名

  ActivityName:Activity名。好吧,我承认自己快被各种名弄晕了,反过头去查了查,确实还没有配置ActivityName。ApplicationName就像VS中的解决方案,Module Name就像是VS中的项目,那么Acitivity Name呢?我想更应该像是主函数或者主窗体的名称吧。如果这样想的话,Layout Name是不是就像是一个主窗体类?希望不会误导了大家。

  Layout Name:布局名。

  1.6 大功告成!

  点击HelloWorld左边三角,从HelloWorld/HelloWorld/src/main/下,找到AndroidManifest.xml,双击打开。

 

<?xml version="1.0"encoding="utf-8"?>

<manifestxmlns:android="http://schemas.android.com/apk/res/android"

   package="com.wenruohuan.helloworld" >

 

   <application

       android:allowBackup="true"

       android:icon="@drawable/ic_launcher"

       android:label="@string/app_name"

       android:theme="@style/AppTheme" >

       <activity

           android:name="com.wenruohuan.helloworld.FullscreenActivity"

           android:configChanges="orientation|keyboardHidden|screenSize"

           android:label="@string/app_name"

           android:theme="@style/FullscreenTheme" >

           <intent-filter>

                <actionandroid:name="android.intent.action.MAIN" />

 

                <categoryandroid:name="android.intent.category.LAUNCHER" />

           </intent-filter>

       </activity>

   </application>

 

</manifest>

 

  看到android:name="com.wenruohuan.helloworld.FullscreenActivity"这一行,我们大致能够明白Module Name及Activity Name之间的区别。

  各节点的具体含义请恕不一一介绍了。

  需要说明的还有两个文件:

  A. 布局文件:HelloWorld/HelloWorld/src/main/res/layout/activity_fullscreen.xml

  内容为:

<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:tools="http://schemas.android.com/tools"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   android:background="#0099cc"

   tools:context="com.wenruohuan.helloworld.FullscreenActivity">

 

   <!-- The primary full-screen view. This can be replaced with whateverview

        is needed to present your content, e.g. VideoView, SurfaceView,

        TextureView, etc. -->

   <TextView android:id="@+id/fullscreen_content"

       android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:keepScreenOn="true"

       android:textColor="#33b5e5"

       android:textStyle="bold"

       android:textSize="50sp"

       android:gravity="center"

       android:text="@string/dummy_content" />

 

   <!-- This FrameLayout insets its children based on system windowsusing

        android:fitsSystemWindows. -->

   <FrameLayout android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:fitsSystemWindows="true">

 

       <LinearLayout android:id="@+id/fullscreen_content_controls"

           style="?metaButtonBarStyle"

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           android:layout_gravity="bottom|center_horizontal"

           android:background="@color/black_overlay"

           android:orientation="horizontal"

           tools:ignore="UselessParent">

 

           <Button android:id="@+id/dummy_button"

               style="?metaButtonBarButtonStyle"

                android:layout_width="0dp"

               android:layout_height="wrap_content"

               android:layout_weight="1"

               android:text="@string/dummy_button" />

 

       </LinearLayout>

   </FrameLayout>

 

</FrameLayout>

  该布局文件中,定义了HelloWorld应用中,甚至可以说是该Activity中所需的各个元素。比如android:id="@+id/fullscreen_content"的Textview、android:layout_width="match_parent"的Framelayout等等。有点象在HTML中定义了各种网页元素的样子。而Textview的显示是这样定义的:android:text="@string/dummy_content",另外定义的一个Button类,Button上显示的字符串是由android:text="@string/dummy_button"决定的。而@string/dummy_content、@string/dummy_button的真正内容却是在下面要讲的strings.xml中定义的。

  B. 字符串定义:HelloWorld/HelloWorld/src/main/res/values/strings.xml

  values下.xml文件,就如同一个个字典,比如strings.xml,<string>节点中,@name中定义了关键字,<string></string>之间的内容则表示对应关键字的描述或说明。我们将<stringname="dummy_content"></string>的中间部分换为“Hello\nWorld!”。

  1.7运行

  从主菜单中选择Run/Run,应该会出现如下提示:

  点击“0. Edit Configurations”,出现“Run/Debug Configurations”

  点开Defaults/AndroidApplications,在右侧General分页里,从Target Device中,选中Emulator,然后从Prefer Android Virtual Device后面的列表框里选择已经定义好的虚拟设备。因为我们还没定义,所以点击后面的“…”按钮,然后弹出Android Virtual Device Manager对话框。


  当然点击“New…”按钮创建一个固然好,但Android Studio已经定义好了一些已知设备,点击“Android VirtualDevices”旁边的“DeviceDefinitions”试试看。

  选择其中一个设备(Device),然后点击“Create AVD…”,弹出Create new Android Virtual Device(AVD)对话框。

  这里我们直接点击“OK”就好了。

  点击“OK”之后,回到Android Virtual Device Manager对话框。

  点击“Start...”按钮,出现“Launch Options”对话框。


  点击“Launch”按钮之后,出现进度条:


  之后,在片刻的等待之后,看到下面的对话框没有??(没有?为什么呢?)


  1.7惑?还是获?

  Andriod的架构实现了用户界面与实际代码的分离,正如Hello World应用所表现的那样,用几个xml文件就制作了一个Android应用。

  不过,最后运行那一步你真的看到了想要的效果了??不可能吧。

  现在,利用File菜单中的Close Project。然后再打开HelloWorld项目。

  找到Run/Run ‘HelloWorld-HelloWorld’菜单,出现“Choose Device”对话框。

  点击“OK”,然后耐心等待看结果吧。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值