android app推荐 2015,VS2015下的Android开发系列02——用VS开发第一个Android APP

配置Android模拟器

这算是第一篇漏下说的,配置好VS的各参数,新建Android项目后,会发现菜单下的工具栏会多出Android相关的工具栏,红色圈出的就是AVD。

b51364419de53ec24e0822786e3a157b.png

打开AVD后可以从模版处选一个设备,然后自己再做细节参数调整。

c89144a9888a9d4894bc64fe9ff9d4a3.png

然后选择要模拟的版本,因为APP有蓝牙BLE的支持需求,所以选择了至少API Level18,注意如果安装了HAXM,CPU/ABI项一定要选"Intel Atom (x86)",如果没有,说明组件未安装,赶紧去下载后再来;另外一个注意点是内存至少3G,要不运行起来卡成狗,不要问我是怎么知道的……

a9fccb3f458daad49b29e77fb5f2bc7e.png

运行效果

4e96b1fac2b64a19616db0831eb9fe1f.png

建立一个Android APP

通过新建项目对话框,新建一个空的Android项目

6dbec0806f7e1f8ac80c202c021511ec.png

新建后的项目基本目录如下(里头的layout1.axml和Activity1.cs是后面加的)

0944723927d28b524b2799d437510d28.png

AndroidManifest.xml

首选第一个要说的是AndroidManifest.xml,这是APP中非常重要的一部分,但在VS中不用像JAVA的开发环境中一样把相关配置写全,打开只看到基本的部分,其余的VS会在编译时补全。AndroidManifest.xml中的内容基本可以在项目属性中配置完成

8143fd7776e368681d278c76f8ebd069.png

(namespace、API Level等的配置)

da83fabe36703c7c217d48c172d58132.png

(APP版本、名称、权限等)

b66d58b65f7e709c57bd9d629587b527.png

(这里还没实际经验,保持默认,等搞清楚了再来说)

Assets和Resource资源目录

Assets里放的是二进制资源文件,如字体、声音,访问方式如下(具体参考Assets目录下的AboutAssets.txt文件里面有示例代码):

//Any raw assets you want to be deployed with your application can be placed in//this directory (and child directories) and given a Build Action of "AndroidAsset".//

//These files will be deployed with you package and will be accessible using Android‘s//AssetManager, like this:

public classReadAsset : Activity

{protected override voidOnCreate (Bundle bundle)

{base.OnCreate (bundle);

InputStream input= Assets.Open ("my_asset.txt");

}

}//Additionally, some Android functions will automatically load asset files:

Typeface tf= Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");

Resource目录中包含了布局、图片、字符串字典等资源文件,同样来看下资源的调用说明(目录中的AboutResources.txt):

/*Images, layout descriptions, binary blobs and string dictionaries can be included

in your application as resource files. Various Android APIs are designed to

operate on the resource IDs instead of dealing with images, strings or binary blobs

directly.

For example, a sample Android app that contains a user interface layout (main.axml),

an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)

would keep its resources in the "Resources" directory of the application:

Resources/

drawable/

icon.png

layout/

main.axml

values/

strings.xml

In order to get the build system to recognize Android resources, set the build action to

"AndroidResource". The native Android APIs do not operate directly with filenames, but

instead operate on resource IDs. When you compile an Android application that uses resources,

the build system will package the resources for distribution and generate a class called "R"

(this is an Android convention) that contains the tokens for each one of the resources

included. For example, for the above Resources layout, this is what the R class would expose:*/

public classR {public classdrawable {public const int icon = 0x123;

}public classlayout {public const int main = 0x456;

}public classstrings {public const int first_string = 0xabc;public const int second_string = 0xbcd;

}

}/*You would then use R.drawable.icon to reference the drawable/icon.png file, or R.layout.main

to reference the layout/main.axml file, or R.strings.first_string to reference the first

string in the dictionary file values/strings.xml.*/

Activity代码

除了这些目录以外,别的就是Activity的代码及自己的类代码啦,APP的默认启动Activity只要设备类的相关属性就OK了,不需要特定的名字,重点注意:“MainLauncher = true, Icon = "@drawable/icon"”。至于别的代码是不是和JAVA的基本一样一样的?

[Activity(Label = "演示APP_01", MainLauncher = true, Icon = "@drawable/icon")]public classMainActivity : Activity

{protected override voidOnCreate(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

}

}

如点击按钮启动另外一个Activity的代码,有没有熟悉的感觉?

Button btnOpenActivity = FindViewById(Resource.Id.btnOpenActivity);

btnOpenActivity.Click+= (sender, e) =>{

Intent intent= new Intent(this, typeof(Activity1));

StartActivity(intent);

};

为Activity添加两个按钮和一些简单的代码后调试运行效果图:

a8f626949954c8267846508c0d13b310.png

11e63c53a18612576bdd0445f36e4ae7.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值