Hello今天要开始咱们的Andriod开发之旅了,网上有不少关于andriod开发的视频,资料,可是用Visual Studio开发的人却比较少。今天咱们就来看一下,要在VS上开发andriod,须要怎么去作。前端
首先去官网下载(http://xamarin.com/)
android
下载下来之后,进行安装,安装过程当中对于360弹出的任何安全问题,一律经过,不然会安装失败。c#
双击后,一步步往下走,直到全部的东西都安装成功,若是中间有一环安装失败,或者安装的时候始终出于假死状态,没关系,再次双击进行安装,他会检测已经了那些组件,哪些没有安装。后端
OK,安装完成后,咱们有了两种开发环境能够用C#进行开发,以下
安全
第一种Visual Studio,个人版本是2012。app
第二种,是Xamarin Studioeclipse
这两个开发工具我选择了VS,毕竟我是作.net开发的。ide
除了这两种开发环境,咱们还能用eclipse开发,以下工具
只是andriod的开发环境须要设置一些环境变量。这里我就不说了,网上关于andriod环境搭建的文章太多了。OK,如今咱们就用VS进行咱们的第一个app的开发,咱们先新建一个andriod application。布局
而后从工具箱中拖拽几个按钮,布局方式为LinearLayout,里面嵌套一个TableLayout布局
OK,这个是咱们经过鼠标拖拽出来的,简单吧,咱们同时还能够在咱们的开环境中对这些按钮的背景色和前景色进行设置。
设置后的界面如上第二幅图,咱们看一下本身生成的前端代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/MyButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Hello" /> <TableLayout android:minWidth="25px" android:minHeight="25px" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tableLayout1"> <TableRow android:id="@+id/tableRow1"> <Button android:id="@+id/MyButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Hello" android:textColor="@android:drawable/sym_call_incoming" /> <Button android:id="@+id/MyButton1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Hello" android:textColor="@android:drawable/ic_notification_overlay" /> <Button android:id="@+id/MyButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Hello" /> </TableRow> <TableRow android:id="@+id/tableRow2"> <Button android:id="@+id/MyButton3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Hello" android:textColorHighlight="@android:drawable/btn_star_big_on" android:textColor="@android:drawable/screen_background_light_transparent" /> </TableRow> <TableRow android:id="@+id/tableRow3"> <Button android:id="@+id/MyButton4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Hello" android:textColor="@android:drawable/sym_def_app_icon" android:background="@android:drawable/btn_star_big_on" /> </TableRow> </TableLayout> </LinearLayout>
OK,好了,咱们看一下后端的cs代码
using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; namespace AndroidApplication1 { [Activity(Label = "AndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")] public class Activity1 : Activity { int count = 1; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // Get our button from the layout resource, // and attach an event to it Button button = FindViewById<Button>(Resource.Id.MyButton); button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); }; } } }
也就是点击MyButton按钮以后,它上面的text进行++变化。咱们插上咱们的andriod手机,运行一下
注意你们在部署的时候请尽可能用真机测试部署。另外部署的时候若是你的手机的andriod系统版本是2.3.7,那么在部署的时候,解决方案属性里面andriod编译版本要选2.3,不然会部署失败。
另外部署的时候必定要选release版本。当你插上你的andriod手机后,在release后面会出现你的手机名称,若是你的手机比VS晚插上,请重启VS,不然找不到你的手机。这个时候点击启动
程序开始往手机上部署,部署成功后,咱们来看一下手机上的运行效果。结果是失败了。
为何呢,后来查了一下缘由,缘由是我通过属性选择的这些东西都没有加载进drawable文件夹中。
<TableRow android:id="@+id/tableRow3"> <Button android:id="@+id/MyButton4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Hello" android:textColor="@android:drawable/sym_def_app_icon" android:background="@android:drawable/btn_star_big_on" /> </TableRow>
像这个里面的两个图片在drawable文件里根本没有,只有Andriod app自身的而一个默认图标。
因此问题就出在这里,把里面的全部除Icon.png以外引用图片的代码都去掉,直接部署成功。
咱们打开以后,是咱们所写的界面
咱们连续点击第一个button,会自增。已经自增到了18。
OK,今天就到这里,今天只是对环境的初步了解,以及一个简单的app的实践,环境好了,咱们就能够放开手脚去边学边作了。