1.廢話背景介紹
在Build 2016開發者大會上,微軟宣布,Xamarin將被整合進所有版本的Visual Studio之中。
前面都是科普廢話,上正文.
2.環境安裝
抱着試一試的心態,更新了VS 2015 Updata2, 因為以前就玩過Xamarin,注冊了帳號發現升級后還是使用過去,很郁悶 以為微軟忽悠我們呢.
試着找找 無意中在Tools --> Options --> Other 里面看到 Xamarin for Visual Studio Updatas
咦這是什么鬼?
點擊 Check Now 試試 如圖:
等到一段時間, 牆的威力太大,有必要的話請翻牆更新.另外Android SDK NDK的安裝(什么,你知道怎么安裝,請baidu一下)也請翻牆,下載鏡像可以設置為東軟的鏡像 mirrors.neusoft.edu.cn
如圖:
這些下載更新快多了.
環境這些准備好了.
我們來回歸正題.
3.Android底部導航條
終於可以寫代碼了,開始不了解Xamarin Android的原理,還導出找教程,尼瑪最后才知道不管Android還是IOS Xamarin都可以直接使用原生的界面布局加C#語法編譯打包.這下簡單了找個原生的例子直接使用,略加翻譯Java-->C#即可,Java C#相似度極高. 聰明的你們肯定一看就會.以下實例本人均翻譯至Java的實例.
簡單說說原理,主要使用TabActivity TabHost Intent. 你問我他們分別是干什么的? 呃!~~~ 這個可以將一本書了, 用了你就知道了.
現在 TabActivity據說已經過時 Fragment已取代了他.有興趣的朋友可以試試,找個例子改改.
TabActivity在API 13(Android 3.2)被標記為過期,需要使用Fragment來實現,Fragment是Android 3.0引入的一個概念,主要就是為了適應各種不同的屏
先上張效果圖,這是最終要實現的樣子:
開始建工程
這個大家都會吧,不多說.
創建Maintabs.axml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" />
<TabWidget
android:id="@android:id/tabs"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
<RadioGroup
android:gravity="center_vertical"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:id="@+id/main_radio"
android:background="@drawable/maintab_toolbar_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio_button0"
android:layout_marginTop="2.0dip"
android:text="@string/main_home"
android:drawableTop="@drawable/icon_1_n"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button1"
android:layout_marginTop="2.0dip"
android:text="@string/main_news"
android:drawableTop="@drawable/icon_2_n"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button2"
android:layout_marginTop="2.0dip"
android:text="@string/main_manage_date"
android:drawableTop="@drawable/icon_3_n"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button3"
android:layout_marginTop="2.0dip"
android:text="@string/main_friends"
android:drawableTop="@drawable/icon_4_n"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button4"
android:layout_marginTop="2.0dip"
android:text="@string/more"
android:drawableTop="@drawable/icon_5_n"
style="@style/main_tab_bottom" />
RadioGroup>
LinearLayout>
TabHost>
修改 MainActivity.cs
2 using Android.App;
3 using Android.Content;
4 using Android.Runtime;
5 using Android.Views;
6 using Android.Widget;
7 using Android.OS;
8
9 namespace MyAppTest
10 {
11 [Activity(Label = "MyAppTest", MainLauncher = true, Icon = "@drawable/icon",Theme = "@android:style/Theme.DeviceDefault.NoActionBar")]
12 public class MainTabActivity : TabActivity, CompoundButton.IOnCheckedChangeListener
13 {
14 private TabHost mTabHost;
15 private Intent mAIntent;
16 private Intent mBIntent;
17 private Intent mCIntent;
18 private Intent mDIntent;
19 private Intent mEIntent;
20
21 protected override void OnCreate(Bundle bundle)
22 {
23 base.OnCreate(bundle);
24 RequestWindowFeature(WindowFeatures.NoTitle);
25 // Set our view from the "main" layout resource
26 SetContentView(Resource.Layout. Maintabs);
27
28 this.mAIntent = new Intent(this, typeof (AActivity));
29 this.mBIntent = new Intent(this, typeof (BActivity));
30 this.mCIntent = new Intent(this, typeof (CActivity));
31 this.mDIntent = new Intent(this, typeof (DActivity));
32 this.mEIntent = new Intent(this, typeof (EActivity));
33
34 ((RadioButton)FindViewById(Resource.Id.radio_button0)).SetOnCheckedChangeListener(this);
35 ((RadioButton)FindViewById(Resource.Id.radio_button1)).SetOnCheckedChangeListener(this);
36 ((RadioButton)FindViewById(Resource.Id.radio_button2)).SetOnCheckedChangeListener(this);
37 ((RadioButton)FindViewById(Resource.Id.radio_button3)).SetOnCheckedChangeListener(this);
38 ((RadioButton)FindViewById(Resource.Id.radio_button4)).SetOnCheckedChangeListener(this);
39
40
41 SetupIntent();
42 }
43
44 public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
45 {
46 if (isChecked)
47 {
48 switch (buttonView.Id)
49 {
50 case Resource.Id.radio_button0:
51 this.mTabHost.SetCurrentTabByTag("A_TAB");
52 break;
53 case Resource.Id.radio_button1:
54 this.mTabHost.SetCurrentTabByTag("B_TAB");
55 break;
56 case Resource.Id.radio_button2:
57 this.mTabHost.SetCurrentTabByTag("C_TAB");
58 break;
59 case Resource.Id.radio_button3:
60 this.mTabHost.SetCurrentTabByTag("D_TAB");
61 break;
62 case Resource.Id.radio_button4:
63 this.mTabHost.SetCurrentTabByTag("MORE_TAB");
64 break;
65 }
66 }
67 }
68
69 private void SetupIntent()
70 {
71 this.mTabHost = this.TabHost;
72 TabHost localTabHost = this.mTabHost;
73
74 localTabHost.AddTab(BuildTabSpec("A_TAB", Resource.String.main_home,Resource.Drawable.icon_1_n, this.mAIntent));
75
76 localTabHost.AddTab(BuildTabSpec("B_TAB", Resource.String.main_news,
77 Resource.Drawable.icon_2_n, this.mBIntent));
78
79 localTabHost.AddTab(BuildTabSpec("C_TAB",
80 Resource.String.main_manage_date, Resource.Drawable.icon_3_n,
81 this.mCIntent));
82
83 localTabHost.AddTab(BuildTabSpec("D_TAB", Resource.String.main_friends,
84 Resource.Drawable.icon_4_n, this.mDIntent));
85
86 localTabHost.AddTab(BuildTabSpec("MORE_TAB", Resource.String.more,
87 Resource.Drawable.icon_5_n, this.mEIntent));
88
89 }
90
91 private TabHost.TabSpec BuildTabSpec(string tag, int resLabel, int resIcon, Intent content)
92 {
93 return this.mTabHost.NewTabSpec(tag).SetIndicator(GetString(resLabel),
94 Resources.GetDrawable(resIcon)).SetContent(content);
95 }
96 }
97 }
注意紅色部分,與自動創建的不同.
創建其他 Activity.cs / AActivity BActivity CActivity DActivity EActivity
using System;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace MyAppTest
{
[Activity(Label = " AActivity ", Icon = " @drawable/icon ", Theme = " @android:style/Theme.DeviceDefault.NoActionBar ")]
public class AActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
RequestWindowFeature(WindowFeatures.NoTitle);
TextView tv = new TextView( this);
tv.SetText( " This is A Activity! ", TextView.BufferType.Normal);
tv.Gravity = GravityFlags.Center;
SetContentView(tv);
}
}
}
涉及到的資源這里沒有列出,請參考源碼.
4.真機調試
代碼寫完了,該編譯以下了, 看看是否有錯, 編譯順利通過, 插上真機(我這里有個買車險送的 200塊的 Android平板 准備扔了的,試了下很真可以調試程序),
要VS直接調試的話,需要VS是以管理員方式啟動的, 查上設備后你會看到:
設備顯示在調試的位置,直接F5就可以了. 什么?無圖無真相?
運行效果:
5.打包
調試完了,該打包發布了是吧?
等等 打包(Export Android Package(.apk))怎么是灰色的?
別着急,大微軟肯定不會忽悠你.
原來你需要把編譯 模式選擇為 Release,默認的Debug是不能打包的. 看這里. 看這里.
打包完成:
你問我卡不卡呀? 我這不到200塊錢的白送的pad都可以流暢的跑.
結束了,該上碼了:
本文源碼下載:
http://files.cnblogs.com/files/crazybird/MyAppTest.zip