MonoDroid学习笔记(四)—— MonoDroid程序界面的布局

上一篇文章中,我们已经简要分析了一个MonoDroid工程的基本结构,这次我们来讨论更多。每一种应用程序基本都会有用户界面(UI),在.Net里,winform程序的UI是一个form窗体,wpf或silverlight程序的UI是一个xaml,asp.net页面是一个aspx,那么在MonoDroid里,就是一个Activity。上面说的.net里的程序都可以既使用可视化界面进行拖拽,也可以在源代码中进行编辑,但MonoDroid中,只能对xml文件或axml文件进行编辑或者在Activity类中使用c#代码进行动态加载。那么如何对程序的界面进行布局呢?我们这篇文章就来进行探讨。

    打开Main.xml,可以看到Button是被嵌套在LiearLayout里的,这个LiearLayout直接翻译过来就是线性布局,它是MonoDroid中布局的一种,MonoDroid的支持多种布局方式:有LinearLayout(线性布局),RelativeLayout(相对位置布局),TableLayout(表格布局),GridView(网格视图),TabLayout(标签页布局),ListView(列表视图)等等,如下图所示,详细的布局可以参考Android.Widget命名空间下以Layout结尾的类都有哪些。

 

                  线性布局                                         相对布局                                        表格布局

          

 

                 网格视图                                          标签页布局                                       列表视图

        

 

   下面我们来试试写一个例子,把几个布局都用上。设计这样一个界面:有四个标签页,第一个标签页内有线性布局和相对布局;第二个标签页内有表格布局,第三个标签页内有网格视图,第四个标签页内有列表视图。

    首先我们要先设计标签页界面。需要使用TabHost和TabWidget。TabHost必须是布局的根节点,它包括用来显示标签页的TabWdiget和显示标签内容的FrameLayout。在Main.axml中添加如下代码:

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.   <LinearLayout  
  7.       android:orientation="vertical"  
  8.       android:layout_width="fill_parent"  
  9.       android:layout_height="fill_parent"  
  10.       android:padding="5dp">  
  11.     <TabWidget  
  12.         android:id="@android:id/tabs"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"/>  
  15.     <FrameLayout  
  16.         android:id="@android:id/tabcontent"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="fill_parent"  
  19.         android:padding="5dp"/>  
  20.   </LinearLayout>  
  21. </TabHost>  

在Resources/Layout文件夹下增加Tab1~Tab4共四个axml布局文件,分别表示四个标签页的内容:

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation=    "vertical"  
  4.     android:layout_width=    "fill_parent"  
  5.     android:layout_height=    "fill_parent"    >  
  6.   <LinearLayout  
  7.       android:orientation=    "horizontal"  
  8.       android:layout_width=    "fill_parent"  
  9.       android:layout_height=    "wrap_content"  
  10.       android:layout_weight=    "1"    >  
  11.     <TextView  
  12.         android:text=    "红"  
  13.         android:gravity=    "center_horizontal"  
  14.         android:background=    "#aa0000"  
  15.         android:layout_width=    "wrap_content"  
  16.         android:layout_height=    "wrap_content"  
  17.         android:layout_weight=    "1"    />  
  18.     <TextView  
  19.         android:text=    "绿"  
  20.         android:gravity=    "center_horizontal"  
  21.         android:background=    "#00aa00"  
  22.         android:layout_width=    "wrap_content"  
  23.         android:layout_height=    "wrap_content"  
  24.         android:layout_weight=    "1"    />  
  25.     <TextView  
  26.         android:text=    "蓝"  
  27.         android:gravity=    "center_horizontal"  
  28.         android:background=    "#0000aa"  
  29.         android:layout_width=    "wrap_content"  
  30.         android:layout_height=    "wrap_content"  
  31.         android:layout_weight=    "1"    />  
  32.     <TextView  
  33.         android:text=    "黄"  
  34.         android:gravity=    "center_horizontal"  
  35.         android:background=    "#aaaa00"  
  36.         android:layout_width=    "wrap_content"  
  37.         android:layout_height=    "wrap_content"  
  38.         android:layout_weight=    "1"    />  
  39.   </LinearLayout>  
  40.   <LinearLayout  
  41.     android:orientation=    "vertical"  
  42.     android:layout_width=    "fill_parent"  
  43.     android:layout_height=    "wrap_content"  
  44.     android:layout_weight=    "1"    >  
  45.     <TextView  
  46.         android:text=    "第一行"  
  47.         android:layout_width=    "fill_parent"  
  48.         android:layout_height=    "wrap_content"  
  49.         android:layout_weight=    "1"    />  
  50.     <TextView  
  51.         android:text=    "第二行"  
  52.         android:layout_width=    "fill_parent"  
  53.         android:layout_height=    "wrap_content"  
  54.         android:layout_weight=    "1"    />  
  55.     <TextView  
  56.         android:text=    "第三行"  
  57.         android:layout_width=    "fill_parent"  
  58.         android:layout_height=    "wrap_content"  
  59.         android:layout_weight=    "1"    />  
  60.     <TextView  
  61.         android:text=    "第四行"  
  62.         android:layout_width=    "fill_parent"  
  63.         android:layout_height=    "wrap_content"  
  64.        android:layout_weight=    "1"    />  
  65.   </LinearLayout>  
  66.   <RelativeLayout  
  67.     android:layout_width="fill_parent"  
  68.     android:layout_height="wrap_content">  
  69.     <TextView  
  70.         android:id="@+id/label"  
  71.         android:layout_width="wrap_content"  
  72.         android:layout_height="wrap_content"  
  73.         android:text="账号:"/>  
  74.     <EditText  
  75.         android:id="@+id/entry"  
  76.         android:layout_width="fill_parent"  
  77.         android:layout_height="wrap_content"  
  78.         android:layout_below="@id/label"/>  
  79.     <TextView  
  80.     android:id="@+id/label2"  
  81.     android:layout_width="wrap_content"  
  82.     android:layout_height="wrap_content"  
  83.     android:layout_below="@id/entry"  
  84.     android:text="密码:"/>  
  85.     <EditText  
  86.         android:id="@+id/entry2"  
  87.         android:layout_width="fill_parent"  
  88.         android:layout_height="wrap_content"  
  89.         android:layout_below="@id/label2"  
  90.         android:password="true" />  
  91.     <Button  
  92.         android:id="@+id/ok"  
  93.         android:layout_width="wrap_content"  
  94.         android:layout_height="wrap_content"  
  95.         android:layout_below="@id/entry2"  
  96.         android:layout_alignParentRight="true"  
  97.         android:layout_marginLeft="10dip"  
  98.         android:text="OK" />  
  99.     <Button  
  100.         android:layout_width="wrap_content"  
  101.         android:layout_height="wrap_content"  
  102.         android:layout_toLeftOf="@id/ok"  
  103.         android:layout_alignTop="@id/ok"  
  104.         android:text="Cancel" />  
  105.   </RelativeLayout>  
  106. </LinearLayout>  

 

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:stretchColumns="1">  
  6.   <TableRow>  
  7.     <TextView  
  8.         android:layout_column="1"  
  9.         android:text="打开..."  
  10.         android:padding="3dip"/>  
  11.     <TextView  
  12.         android:text="Ctrl-O"  
  13.         android:gravity="right"  
  14.         android:padding="3dip"/>  
  15.   </TableRow>  
  16.   <TableRow>  
  17.     <TextView  
  18.         android:layout_column="1"  
  19.         android:text="保存..."  
  20.         android:padding="3dip"/>  
  21.     <TextView  
  22.         android:text="Ctrl-S"  
  23.         android:gravity="right"  
  24.         android:padding="3dip"/>  
  25.   </TableRow>  
  26.   <TableRow>  
  27.     <TextView  
  28.         android:layout_column="1"  
  29.         android:text="另存为..."  
  30.         android:padding="3dip"/>  
  31.     <TextView  
  32.         android:text="Ctrl-Shift-S"  
  33.         android:gravity="right"  
  34.         android:padding="3dip"/>  
  35.   </TableRow>  
  36.   <View  
  37.       android:layout_height="2dip"  
  38.       android:background="#FF909090"/>  
  39.   <TableRow>  
  40.     <TextView  
  41.         android:text="X"  
  42.         android:padding="3dip"/>  
  43.     <TextView  
  44.         android:text="导入..."  
  45.         android:padding="3dip"/>  
  46.   </TableRow>  
  47.   <TableRow>  
  48.     <TextView  
  49.         android:text="X"  
  50.         android:padding="3dip"/>  
  51.     <TextView  
  52.         android:text="导出..."  
  53.         android:padding="3dip"/>  
  54.     <TextView  
  55.         android:text="Ctrl-E"  
  56.         android:gravity="right"  
  57.         android:padding="3dip"/>  
  58.   </TableRow>  
  59.   <View  
  60.       android:layout_height="2dip"  
  61.       android:background="#FF909090"/>  
  62.   <TableRow>  
  63.     <TextView  
  64.         android:layout_column="1"  
  65.         android:text="退出"  
  66.         android:padding="3dip"/>  
  67.   </TableRow>  
  68. </TableLayout>  

 

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <GridView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/gridview"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:columnWidth="90dp"  
  7.     android:numColumns="auto_fit"  
  8.     android:verticalSpacing="10dp"  
  9.     android:horizontalSpacing="10dp"  
  10.     android:stretchMode="columnWidth"  
  11.     android:gravity="center"  
  12. />  

 

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:padding="10dp"  
  6.     android:textSize="16sp" >  
  7.     
  8. </TextView>  

 

新建一个Tabs文件夹,在文件夹中增加四个Activity,分别表示四个标签页的后台类:

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Android.App;  
  6. using Android.Content;  
  7. using Android.OS;  
  8. using Android.Runtime;  
  9. using Android.Views;  
  10. using Android.Widget;  
  11. namespace MonoDroidTest.Tabs  
  12. {  
  13.     [Activity(Label = "My Activity")]  
  14.     public class Tab1 : Activity  
  15.     {  
  16.         protected override void OnCreate(Bundle bundle)  
  17.         {  
  18.             base.OnCreate(bundle);  
  19.             // Create your application here  
  20.             SetContentView(Resource.Layout.Tab1);  
  21.             Button btnOK = FindViewById<Button>(Resource.Id.ok);  
  22.             btnOK.Click += (sender, e) =>  
  23.             {  
  24.                 EditText entry = FindViewById<EditText>(Resource.Id.entry);  
  25.                 EditText entry2 = FindViewById<EditText>(Resource.Id.entry2);  
  26.                 AlertDialog.Builder dlg = new AlertDialog.Builder(this);  
  27.                 dlg.SetTitle("提示");  
  28.                 dlg.SetMessage(string.Format("你输入的账号是:{0},密码是:{1}", entry.Text, entry2.Text));  
  29.                 dlg.SetPositiveButton("确定"delegate { });  
  30.                 dlg.Show();  
  31.             };  
  32.         }  
  33.     }  
  34. }  

 

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Android.App;  
  6. using Android.Content;  
  7. using Android.OS;  
  8. using Android.Runtime;  
  9. using Android.Views;  
  10. using Android.Widget;  
  11. namespace MonoDroidTest.Tabs  
  12. {  
  13.     [Activity(Label = "My Activity")]  
  14.     public class Tab2 : Activity  
  15.     {  
  16.         protected override void OnCreate(Bundle bundle)  
  17.         {  
  18.             base.OnCreate(bundle);  
  19.             // Create your application here  
  20.             SetContentView(Resource.Layout.Tab2);  
  21.         }  
  22.     }  
  23. }  

 

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Android.App;  
  6. using Android.Content;  
  7. using Android.OS;  
  8. using Android.Runtime;  
  9. using Android.Views;  
  10. using Android.Widget;  
  11. namespace MonoDroidTest.Tabs  
  12. {  
  13.     [Activity(Label = "My Activity")]  
  14.     public class Tab3 : Activity  
  15.     {  
  16.         protected override void OnCreate(Bundle bundle)  
  17.         {  
  18.             base.OnCreate(bundle);  
  19.             try  
  20.             {  
  21.                 // Create your application here  
  22.                 SetContentView(Resource.Layout.Tab3);  
  23.                 var gridview = FindViewById<GridView>(Resource.Id.gridview);  
  24.                 gridview.Adapter = new ImageAdapter(this);  
  25.                 gridview.ItemClick += (sender, e) =>  
  26.                 {  
  27.                     Toast.MakeText(this, e.Position.ToString(), ToastLength.Short).Show();  
  28.                 };  
  29.             }  
  30.             catch (Exception ex)  
  31.             {  
  32.                 AlertDialog.Builder dlg = new AlertDialog.Builder(this);  
  33.                 dlg.SetTitle("错误");  
  34.                 dlg.SetMessage(ex.Message);  
  35.                 dlg.SetPositiveButton("确定"delegate { });  
  36.                 dlg.Show();  
  37.             }  
  38.         }  
  39.     }  
  40.     public class ImageAdapter : BaseAdapter  
  41.     {  
  42.         Context context;  
  43.         public ImageAdapter(Context c)  
  44.         {  
  45.             context = c;  
  46.         }  
  47.         public override int Count  
  48.         {  
  49.             get { return thumbIds.Length; }  
  50.         }  
  51.         public override Java.Lang.Object GetItem(int position)  
  52.         {  
  53.             return null;  
  54.         }  
  55.         public override long GetItemId(int position)  
  56.         {  
  57.             return 0;  
  58.         }  
  59.         // create a new ImageView for each item referenced by the Adapter  
  60.         public override View GetView(int position, View convertView, ViewGroup parent)  
  61.         {  
  62.             ImageView imageView;  
  63.             if (convertView == null)  
  64.             {  // if it's not recycled, initialize some attributes  
  65.                 imageView = new ImageView(context);  
  66.                 imageView.LayoutParameters = new GridView.LayoutParams(85, 85);  
  67.                 imageView.SetScaleType(ImageView.ScaleType.CenterCrop);  
  68.                 imageView.SetPadding(8, 8, 8, 8);  
  69.             }  
  70.             else  
  71.             {  
  72.                 imageView = (ImageView)convertView;  
  73.             }  
  74.             imageView.SetImageResource(thumbIds[position]);  
  75.             return imageView;  
  76.         }  
  77.         // references to our images  
  78.         int[] thumbIds = {  
  79.         Resource.Drawable.btn1_b, Resource.Drawable.btn2,  
  80.         Resource.Drawable.btn2_b, Resource.Drawable.btn3,  
  81.         Resource.Drawable.btn3_b, Resource.Drawable.btn4,  
  82.         Resource.Drawable.btn4_b, Resource.Drawable.btn1,  
  83.         Resource.Drawable.btn1_b, Resource.Drawable.btn2,  
  84.         Resource.Drawable.btn2_b, Resource.Drawable.btn3,  
  85.         Resource.Drawable.btn3_b, Resource.Drawable.btn4,  
  86.         Resource.Drawable.btn4_b, Resource.Drawable.btn1,  
  87.         Resource.Drawable.btn1_b, Resource.Drawable.btn2,  
  88.         Resource.Drawable.btn2_b, Resource.Drawable.btn3,  
  89.         Resource.Drawable.btn3_b, Resource.Drawable.btn4,  
  90.     };  
  91.     }  
  92. }  

 

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Android.App;  
  6. using Android.Content;  
  7. using Android.OS;  
  8. using Android.Runtime;  
  9. using Android.Views;  
  10. using Android.Widget;  
  11. namespace MonoDroidTest.Tabs  
  12. {  
  13.     [Activity(Label = "My Activity")]  
  14.     public class Tab4 : ListActivity  
  15.     {  
  16.         protected override void OnCreate(Bundle bundle)  
  17.         {  
  18.             base.OnCreate(bundle);  
  19.             // Create your application here  
  20.             try  
  21.             {  
  22.                 ListAdapter = new ArrayAdapter<string>(this, Resource.Layout.Tab4, countries);  
  23.                 ListView.TextFilterEnabled = true;  
  24.                 ListView.ItemClick += delegate(object sender, ItemEventArgs args)  
  25.                 {  
  26.                     // When clicked, show a toast with the TextView text     
  27.                     Toast.MakeText(Application, ((TextView)args.View).Text, ToastLength.Short).Show();  
  28.                 };  
  29.             }  
  30.             catch (Exception ex)  
  31.             {  
  32.                 AlertDialog.Builder dlg = new AlertDialog.Builder(this);  
  33.                 dlg.SetTitle("错误");  
  34.                 dlg.SetMessage(ex.Message);  
  35.                 dlg.SetPositiveButton("确定"delegate { });  
  36.                 dlg.Show();  
  37.             }  
  38.         }  
  39.         static readonly string[] countries = new String[] {     
  40.     "Afghanistan","Albania","Algeria","American Samoa","Andorra",     
  41.     "Angola","Anguilla","Antarctica","Antigua and Barbuda","Argentina",     
  42.     "Armenia","Aruba","Australia","Austria","Azerbaijan",     
  43.     "Bahrain","Bangladesh","Barbados","Belarus","Belgium",     
  44.     "Belize","Benin","Bermuda","Bhutan","Bolivia",     
  45.     "Bosnia and Herzegovina","Botswana","Bouvet Island","Brazil","British Indian Ocean Territory",     
  46.     "British Virgin Islands","Brunei","Bulgaria","Burkina Faso","Burundi",     
  47.     "Cote d'Ivoire","Cambodia","Cameroon","Canada","Cape Verde",     
  48.     "Cayman Islands","Central African Republic","Chad","Chile","China",     
  49.     "Christmas Island","Cocos (Keeling) Islands","Colombia","Comoros","Congo",     
  50.     "Cook Islands","Costa Rica","Croatia","Cuba","Cyprus","Czech Republic",     
  51.     "Democratic Republic of the Congo","Denmark","Djibouti","Dominica","Dominican Republic",     
  52.     "East Timor","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea",     
  53.     "Estonia","Ethiopia","Faeroe Islands","Falkland Islands","Fiji","Finland",     
  54.     "Former Yugoslav Republic of Macedonia","France","French Guiana","French Polynesia",     
  55.     "French Southern Territories","Gabon","Georgia","Germany","Ghana","Gibraltar",     
  56.     "Greece","Greenland","Grenada","Guadeloupe","Guam","Guatemala","Guinea","Guinea-Bissau",     
  57.     "Guyana","Haiti","Heard Island and McDonald Islands","Honduras","Hong Kong","Hungary",     
  58.     "Iceland","India","Indonesia","Iran","Iraq","Ireland","Israel","Italy","Jamaica",     
  59.     "Japan","Jordan","Kazakhstan","Kenya","Kiribati","Kuwait","Kyrgyzstan","Laos",     
  60.     "Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg",     
  61.     "Macau","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands",     
  62.     "Martinique","Mauritania","Mauritius","Mayotte","Mexico","Micronesia","Moldova",     
  63.     "Monaco","Mongolia","Montserrat","Morocco","Mozambique","Myanmar","Namibia",     
  64.     "Nauru","Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand",     
  65.     "Nicaragua","Niger","Nigeria","Niue","Norfolk Island","North Korea","Northern Marianas",     
  66.     "Norway","Oman","Pakistan","Palau","Panama","Papua New Guinea","Paraguay","Peru",     
  67.     "Philippines","Pitcairn Islands","Poland","Portugal","Puerto Rico","Qatar",     
  68.     "Reunion","Romania","Russia","Rwanda","Sqo Tome and Principe","Saint Helena",     
  69.     "Saint Kitts and Nevis","Saint Lucia","Saint Pierre and Miquelon",     
  70.     "Saint Vincent and the Grenadines","Samoa","San Marino","Saudi Arabia","Senegal",     
  71.     "Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands",     
  72.     "Somalia","South Africa","South Georgia and the South Sandwich Islands","South Korea",     
  73.     "Spain","Sri Lanka","Sudan","Suriname","Svalbard and Jan Mayen","Swaziland","Sweden",     
  74.     "Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","The Bahamas",     
  75.     "The Gambia","Togo","Tokelau","Tonga","Trinidad and Tobago","Tunisia","Turkey",     
  76.     "Turkmenistan","Turks and Caicos Islands","Tuvalu","Virgin Islands","Uganda",     
  77.     "Ukraine","United Arab Emirates","United Kingdom",     
  78.     "United States","United States Minor Outlying Islands","Uruguay","Uzbekistan",     
  79.     "Vanuatu","Vatican City","Venezuela","Vietnam","Wallis and Futuna","Western Sahara",     
  80.     "Yemen","Yugoslavia","Zambia","Zimbabwe"     
  81.   };    
  82.     }  
  83. }  

 

    你要为每个标签页分别指定标签页被选中与没被选中两种状态时的图标,所以准备8张图片,放到Resources/Drawable文件夹下。再在这个文件夹下新建4个xml文件,用以标识每个标签页使用哪两张图片作为图标。这里只列举其中一个xml文件作为例子,其余三个类似:

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   <!-- When selected, use grey -->  
  4.   <item android:drawable="@drawable/btn1"  
  5.         android:state_selected="true"/>  
  6.   <!-- When not selected, use white-->  
  7.   <item android:drawable="@drawable/btn1_b"/>  
  8. </selector>  

 

最终的工程文件结构如下:

 

Activity1的代码如下,注意这次Activity1不再继承Activity,而是继承TabActivity:

[c-sharp]  view plain copy print ?
  1. using System;  
  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. using MonoDroidTest.Tabs;  
  9. using Android.Util;  
  10. using Java.IO;  
  11. using Android.Database;  
  12. namespace MonoDroidTest  
  13. {  
  14.     [Activity(Label = "MonoDroidTest", MainLauncher = true)]  
  15.     public class Activity1 : TabActivity  
  16.     {  
  17.         protected override void OnCreate(Bundle bundle)  
  18.         {  
  19.             base.OnCreate(bundle);  
  20.             SetContentView(Resource.Layout.Main);  
  21.             TabHost.TabSpec spec;     // Resusable TabSpec for each tab     
  22.             Intent intent;            // Reusable Intent for each tab     
  23.             // Create an Intent to launch an Activity for the tab (to be reused)     
  24.             intent = new Intent(thistypeof(Tab1));  
  25.             intent.AddFlags(ActivityFlags.NewTask);  
  26.             // Initialize a TabSpec for each tab and add it to the TabHost     
  27.             spec = TabHost.NewTabSpec("tab1");  
  28.             spec.SetIndicator("页签1", Resources.GetDrawable(Resource.Drawable.Tab1));  
  29.             spec.SetContent(intent);  
  30.             TabHost.AddTab(spec);  
  31.             // Do the same for the other tabs     
  32.             intent = new Intent(thistypeof(Tab2));  
  33.             intent.AddFlags(ActivityFlags.NewTask);  
  34.             spec = TabHost.NewTabSpec("tab2");  
  35.             spec.SetIndicator("页签2", Resources.GetDrawable(Resource.Drawable.Tab2));  
  36.             spec.SetContent(intent);  
  37.             TabHost.AddTab(spec);  
  38.             intent = new Intent(thistypeof(Tab3));  
  39.             intent.AddFlags(ActivityFlags.NewTask);  
  40.             spec = TabHost.NewTabSpec("tab3");  
  41.             spec.SetIndicator("页签3", Resources.GetDrawable(Resource.Drawable.Tab3));  
  42.             spec.SetContent(intent);  
  43.             TabHost.AddTab(spec);  
  44.             intent = new Intent(thistypeof(Tab4));  
  45.             intent.AddFlags(ActivityFlags.NewTask);  
  46.             spec = TabHost.NewTabSpec("tab4");  
  47.             spec.SetIndicator("页签4", Resources.GetDrawable(Resource.Drawable.Tab4));  
  48.             spec.SetContent(intent);  
  49.             TabHost.AddTab(spec);  
  50.             TabHost.CurrentTab = 0;  
  51.         }  
  52.     }  
  53. }  
 

运行程序,好了,我们设计的界面出来了~~

 

 

 

 

    我首先在Debug版本下生成了项目,运行起来一切正常,但是后来我改成了Release版本以后,在切换到第三和第四个页签时都会报An exception was thrown by the type initializer for Android.Widget.ItemClickImplementor的异常,但是如果把项目属性中的Linking 选为None,那么在Release版本下也不会出错了,但是生成的apk安装文件却有13M+之巨……具体原因现在不明,初步估计应该是Mono的一个Bug,在官网与技术人员沟通过后,他们也不知原因何在,让我关注他们的下一版本,如果下一版本还存在这个问题,到时给他们提一个Bug。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值