Andriod开发前准备
Hello今天要开始我们的Andriod开发之旅了,网上有很多关于andriod开发的视频,资料,但是用Visual Studio开发的人却比较少。今天我们就来看一下,要在VS上开发andriod,需要怎么去做。
首先去官网下载(http://xamarin.com/)
下载下来以后,进行安装,安装过程中对于360弹出的任何安全问题,一概通过,否则会安装失败。
双击后,一步步往下走,直到所有的东西都安装成功,如果中间有一环安装失败,或者安装的时候始终出于假死状态,不要紧,再次双击进行安装,他会检测已经了那些组件,哪些没有安装。
OK,安装完成后,我们有了两种开发环境可以用C#进行开发,如下
第一种Visual Studio,我的版本是2012。
第二种,是Xamarin Studio
除了这两种开发环境,我们还能用eclipse开发,如下
只是andriod的开发环境需要设置一些环境变量。这里我就不说了,网上关于andriod环境搭建的文章太多了。OK,现在我们就用VS进行我们的第一个app的开发,我们先新建一个andriod application。
然后从工具箱中拖拽几个按钮,布局方式为LinearLayout,里面嵌套一个TableLayout布局
OK,这个是我们通过鼠标拖拽出来的,简单吧,我们同时还可以在我们的开环境中对这些按钮的背景色和前景色进行设置。
设置后的界面如上第二幅图,我们看一下自己生成的前端代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<?
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代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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文件夹中。
|
1
2
3
4
5
6
7
8
9
10
|
<
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的实践,环境好了,我们就可以放开手脚去边学边做了。
本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/1335027,如需转载请自行联系原作者














