知识点:第一个app,显示Hello world
一、创建一个空项目
创建项目
启动 Visual Studio。 单击“文件”>“新建”>“项目”以创建新项目 。
在“新建项目” 对话框中,单击“Android 应用” 模板。 将新项目命名为 Hello(Phoneword是微软示例,本人太懒,不改了) ,然后单击“确定” :

在“新 Android 应用” 对话框中,依次单击“Blank App” 和“OK” ,以新建项目:

自动生成的MainActivity.cs代码为:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
namespace HelloWorld
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
自动生成的Resource\layout\activity_main.xml代码为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
连接上Android手机,进行运行测试

二、在屏幕空白部分显示Hello World!
创建布局
创建新项目之后,在“解决方案资源管理器” 中展开 Resources 文件夹,然后展开 layout 文件夹。 双击“activity_main.axml” ,以在 Android Designer 中打开它。 这是应用屏幕的布局文件:

提示
在“工具箱” (左侧区域)的搜索字段中输入 textView ,并将一个“文本(大)” 小组件拖动至 Design Surface 上(中央区域):
点中textView控件,调整其大小,同时在屏幕右下角可以设置其属性。
我将其 id 改为tv1.TextColor设置为纯红色。TextSize设置为60px(必须有单位). Text设置为Hello World
Resource\layout\activity_main.xml代码变为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="HelloWorld"
android:layout_width="match_parent"
android:layout_height="56.5dp"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/tv1"
android:textColor="#ffca1919"
android:textSize="60px" />
</RelativeLayout>
运行程序得到以下效果

以上。
|