Andriod開發前準備

Hello今天要開始咱們的Andriod開發之旅了,網上有不少關於andriod開發的視頻,資料,可是用Visual Studio開發的人卻比較少。今天咱們就來看一下,要在VS上開發andriod,須要怎麼去作。前端

首先去官網下載(http://xamarin.com/)
android

165532657.png

下載下來之後,進行安裝,安裝過程當中對於360彈出的任何安全問題,一律經過,不然會安裝失敗。c#

雙擊後,一步步往下走,直到全部的東西都安裝成功,若是中間有一環安裝失敗,或者安裝的時候始終出於假死狀態,沒關係,再次雙擊進行安裝,他會檢測已經了那些組件,哪些沒有安裝。後端

OK,安裝完成後,咱們有了兩種開發環境能夠用C#進行開發,以下
安全

第一種Visual Studio,個人版本是2012。app

170122272.png

第二種,是Xamarin Studioeclipse

170647885.png這兩個開發工具我選擇了VS,畢竟我是作.net開發的。ide

除了這兩種開發環境,咱們還能用eclipse開發,以下工具

171504496.png

只是andriod的開發環境須要設置一些環境變量。這裏我就不說了,網上關於andriod環境搭建的文章太多了。OK,如今咱們就用VS進行咱們的第一個app的開發,咱們先新建一個andriod application。佈局

172305168.png172357309.png

而後從工具箱中拖拽幾個按鈕,佈局方式爲LinearLayout,裏面嵌套一個TableLayout佈局

172532468.png

OK,這個是咱們經過鼠標拖拽出來的,簡單吧,咱們同時還能夠在咱們的開環境中對這些按鈕的背景色和前景色進行設置。

173340261.png173416565.png

設置後的界面如上第二幅圖,咱們看一下本身生成的前端代碼

<?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,不然會部署失敗。

174444276.png

另外部署的時候必定要選release版本。當你插上你的andriod手機後,在release後面會出現你的手機名稱,若是你的手機比VS晚插上,請重啓VS,不然找不到你的手機。這個時候點擊啓動

175606738.png

程序開始往手機上部署,部署成功後,咱們來看一下手機上的運行效果。結果是失敗了。

爲何呢,後來查了一下緣由,緣由是我通過屬性選擇的這些東西都沒有加載進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自身的而一個默認圖標。

181507917.png

因此問題就出在這裏,把裏面的全部除Icon.png以外引用圖片的代碼都去掉,直接部署成功。

181730648.png

咱們打開以後,是咱們所寫的界面

181835195.png

咱們連續點擊第一個button,會自增。已經自增到了18。

181930659.png

OK,今天就到這裏,今天只是對環境的初步瞭解,以及一個簡單的app的實踐,環境好了,咱們就能夠放開手腳去邊學邊作了。

相關文章
相關標籤/搜索