分类:C#、Android、VS2015; 创建日期:2016-02-09 一、在AssemblyInfo.cs文件中配置应用程序清单 前面的章节我们说过,除了在AndroidManifest.xml文件中配置应用程序清单外,还可以在AssemblyInfo.cs文件中配置应用程序清单。 在AssemblyInfo.cs文件中配置
分类:C#、Android、VS2015;
创建日期:2016-02-09
前面的章节我们说过,除了在AndroidManifest.xml文件中配置应用程序清单外,还可以在AssemblyInfo.cs文件中配置应用程序清单。
在AssemblyInfo.cs文件中配置应用程序清单是用C#编写应用程序建议的做法。
在AssemblyInfo.cs文件中配置应用程序清单的优点是:由于键入C#代码时有方便的智能提示,添加、修改清单配置都比直接在AndroidManifest.xml文件中配置容易多了。
实际上,用C#编写Android应用程序时,可以完全不去考虑AndroidManifest.xml文件,这些内部处理的过程都让编译器替你去管理好了,反正最终生成的清单配置结果都是一样的。但是,这种带智能提示的配置方式要比你直接修改AndroidManifest.xml文件方便得多。
当然,这只是VS2015编译器给你提供的智能配置Android应用程序清单的另一种途径而已,也是C#编程常用的配置清单的方式。不过,如果你仍然习惯于像java编程那样在项目中直接修改AndroidManifest.xml文件,而且也能容忍配置不一致时导致的莫名其妙的错误,也可以继续使用直接修改AndroidManifest.xml文件的办法,反正Java编程人员都已经习惯改过来改过去那种最原始的配置办法了,也对这种方式麻木了,可对C#编程来说,这种弱智的到处让编程人员自己去配置这配置那的实现思路实在是让人无法容忍。
总之,本人还是更喜欢在AssemblyInfo.cs文件中配置它,这种方式既能让你利用智能提示很直观地看到到底有哪些可配置的选项,也能让你一眼就能看出配置是否有错。
从这一章开始,我们将采用这种办法配置在整个应用程序范围内都起作用的清单,而不再去直接修改AndroidManifest.xml文件了。
在前面章节的主界面中,我们使用的都是最简单的办法:直接创建一个字符串数组来列出示例导航。这种方式虽然简单,但却是最没有灵活性的笨办法。
这一节我们将学习如何让主界面显示的内容更灵活,这也是实际项目中常用的办法。
1、在哪里定义适配器
一般情况下,适配器针对哪个类,就在包含这个类的文件中去定义它。例如,要编写针对MainActivity类中使用的适配器,就在MainActivity.cs文件中去定义它。
2、创建自己的列表项类
到底创建那些列表项,就看你的需要了。下面的代码演示了如何创建MyItems类:
public class MyItems
{
public string Title { get; set; }
public string Desc { get; set; }
}
3、创建自定义适配器的技巧
一旦有了自己的列表项(MyItems类),就可以在自定义的适配器中去指定它了。
下面的截图演示了如何快速创建自定义适配器:
单击黄色灯泡右方的小三角符号,在弹出的下拉框中选择【实现抽象类】,它就会自动添加所有需要重写的方法,不需要你自己去一个一个地敲代码。
注意:这里提示的“不实现……”实际上应该是“没有实现…..”,这是中文翻译的问题,自己明白它的实际含义就行了。
本章示例的所有源程序都在ch07demos项目中。
项目名:ch07demos
项目模板:Blank App(Android)
1、运行截图
主界面运行截图如下:
2、修改发布的目标版本
在解决方案资源管理器中,鼠标右击【ch07demos】项目,选择【属性】,将【Compile using Android version】的选项改为“API Level 19”,如下图所示:
3、修改清单文件(AssemblyInfo.cs)
在该文件中添加应用程序级别的主题以及其他配置。添加后的AssemblyInfo.cs的完整内容如下:
<span style="color: #0000ff">using</span><span style="color: #000000"> System.Reflection; </span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Runtime.CompilerServices; </span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Runtime.InteropServices; </span><span style="color: #0000ff">using</span><span style="color: #000000"> Android.App; [assembly: AssemblyTitle(</span><span style="color: #800000">"</span><span style="color: #800000">ch07demos</span><span style="color: #800000">"</span><span style="color: #000000">)] [assembly: AssemblyDescription(</span><span style="color: #800000">"</span><span style="color: #800000">布局控件的基本用法</span><span style="color: #800000">"</span><span style="color: #000000">)] [assembly: AssemblyConfiguration(</span><span style="color: #800000">""</span><span style="color: #000000">)] [assembly: AssemblyCompany(</span><span style="color: #800000">"</span><span style="color: #800000">毛毛雨的博客乐园(http://www.cnblogs/rainmj/)</span><span style="color: #800000">"</span><span style="color: #000000">)] [assembly: AssemblyPRoduct(</span><span style="color: #800000">"</span><span style="color: #800000">rainmjAndroidDemos</span><span style="color: #800000">"</span><span style="color: #000000">)] [assembly: AssemblyCopyright(</span><span style="color: #800000">"</span><span style="color: #800000">Copyright ? 2016</span><span style="color: #800000">"</span><span style="color: #000000">)] [assembly: AssemblyTrademark(</span><span style="color: #800000">""</span><span style="color: #000000">)] [assembly: AssemblyCulture(</span><span style="color: #800000">"</span><span style="color: #800000">zh-CN</span><span style="color: #800000">"</span><span style="color: #000000">)] [assembly: ComVisible(</span><span style="color: #0000ff">false</span><span style="color: #000000">)] [assembly:application(Theme </span>= <span style="color: #800000">"</span><span style="color: #800000">@android:style/Theme.DeviceDefault.Light</span><span style="color: #800000">"</span><span style="color: #000000">)] [assembly: AssemblyVersion(</span><span style="color: #800000">"</span><span style="color: #800000">1.0.*</span><span style="color: #800000">"</span>)]
4、修改主界面(Main.axml)
将Main.axml改为下面的代码:
<span style="color: #0000ff"></span><span style="color: #ff00ff">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff">?></span> <span style="color: #0000ff"><span style="color: #800000">LinearLayout </span><span style="color: #ff0000">xmlns:android</span><span style="color: #0000ff">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000"> android:orientation</span><span style="color: #0000ff">="vertical"</span><span style="color: #ff0000"> android:layout_width</span><span style="color: #0000ff">="fill_parent"</span><span style="color: #ff0000"> android:layout_height</span><span style="color: #0000ff">="fill_parent"</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><span style="color: #800000">ListView </span><span style="color: #ff0000">android:minWidth</span><span style="color: #0000ff">="25px"</span><span style="color: #ff0000"> android:minHeight</span><span style="color: #0000ff">="25px"</span><span style="color: #ff0000"> android:layout_width</span><span style="color: #0000ff">="match_parent"</span><span style="color: #ff0000"> android:layout_height</span><span style="color: #0000ff">="match_parent"</span><span style="color: #ff0000"> android:id</span><span style="color: #0000ff">="@+id/listView1"</span> <span style="color: #0000ff">/></span> <span style="color: #0000ff"></span><span style="color: #800000">LinearLayout</span><span style="color: #0000ff">></span></span></span>
5、修改主活动文件(MainActivity.cs)
本章示例全部完成后MainActivity.cs的代码如下:
<span style="color: #0000ff">using</span><span style="color: #000000"> Android.App; </span><span style="color: #0000ff">using</span><span style="color: #000000"> Android.Views; </span><span style="color: #0000ff">using</span><span style="color: #000000"> Android.Widget; </span><span style="color: #0000ff">using</span><span style="color: #000000"> Android.OS; </span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Collections.Generic; </span><span style="color: #0000ff">using</span><span style="color: #000000"> ch07demos.SrcDemos; </span><span style="color: #0000ff">namespace</span><span style="color: #000000"> ch07demos { [Activity(Label </span>= <span style="color: #800000">"</span><span style="color: #800000">ch07demos</span><span style="color: #800000">"</span>, MainLauncher = <span style="color: #0000ff">true</span>, Icon = <span style="color: #800000">"</span><span style="color: #800000">@drawable/icon</span><span style="color: #800000">"</span><span style="color: #000000">)] </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> MainActivity : Activity { </span><span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span><span style="color: #000000"> OnCreate(Bundle bundle) { </span><span style="color: #0000ff">base</span><span style="color: #000000">.OnCreate(bundle); SetContentView(Resource.Layout.Main); List</span><myitems> items = <span style="color: #0000ff">new</span> List<myitems><span style="color: #000000">() { </span><span style="color: #0000ff">new</span> MyItems {Title=<span style="color: #800000">"</span><span style="color: #800000">Demo01--LinearLayout</span><span style="color: #800000">"</span>,Desc=<span style="color: #800000">"</span><span style="color: #800000">演示线性布局的基本用法</span><span style="color: #800000">"</span><span style="color: #000000"> }, </span><span style="color: #0000ff">new</span> MyItems {Title=<span style="color: #800000">"</span><span style="color: #800000">Demo02--GridLayout</span><span style="color: #800000">"</span>,Desc=<span style="color: #800000">"</span><span style="color: #800000">演示网格布局的基本用法</span><span style="color: #800000">"</span><span style="color: #000000"> }, </span><span style="color: #0000ff">new</span> MyItems {Title=<span style="color: #800000">"</span><span style="color: #800000">Demo03--TableLayout</span><span style="color: #800000">"</span>,Desc=<span style="color: #800000">"</span><span style="color: #800000">演示表格布局的基本用法</span><span style="color: #800000">"</span><span style="color: #000000"> }, </span><span style="color: #0000ff">new</span> MyItems {Title=<span style="color: #800000">"</span><span style="color: #800000">Demo04--RelativeLayout</span><span style="color: #800000">"</span>,Desc=<span style="color: #800000">"</span><span style="color: #800000">演示相对布局的基本用法</span><span style="color: #800000">"</span><span style="color: #000000"> }, </span><span style="color: #0000ff">new</span> MyItems {Title=<span style="color: #800000">"</span><span style="color: #800000">Demo05--FrameLayout</span><span style="color: #800000">"</span>,Desc=<span style="color: #800000">"</span><span style="color: #800000">演示帧布局的基本用法</span><span style="color: #800000">"</span><span style="color: #000000"> } }; ListView listView1 </span>= FindViewById<listview><span style="color: #000000">(Resource.Id.listView1); listView1.Adapter </span>= <span style="color: #0000ff">new</span> MyAdapter(<span style="color: #0000ff">this</span><span style="color: #000000">, items); listView1.ItemClick </span>+= (s, e) =><span style="color: #000000"> { </span><span style="color: #0000ff">int</span> index = e.Position + <span style="color: #800080">1</span><span style="color: #000000">; </span><span style="color: #0000ff">switch</span><span style="color: #000000"> (index) { </span><span style="color: #0000ff">case</span> <span style="color: #800080">1</span><span style="color: #000000">: StartActivity(</span><span style="color: #0000ff">typeof</span><span style="color: #000000">(Demo01LinearLayout)); </span><span style="color: #0000ff">break</span><span style="color: #000000">; </span><span style="color: #0000ff">case</span> <span style="color: #800080">2</span><span style="color: #000000">: StartActivity(</span><span style="color: #0000ff">typeof</span><span style="color: #000000">(Demo02GridLayout)); </span><span style="color: #0000ff">break</span><span style="color: #000000">; </span><span style="color: #0000ff">case</span> <span style="color: #800080">3</span><span style="color: #000000">: StartActivity(</span><span style="color: #0000ff">typeof</span><span style="color: #000000">(Demo03TableLayout)); </span><span style="color: #0000ff">break</span><span style="color: #000000">; </span><span style="color: #0000ff">case</span> <span style="color: #800080">4</span><span style="color: #000000">: StartActivity(</span><span style="color: #0000ff">typeof</span><span style="color: #000000">(Demo04RelativeLayout)); </span><span style="color: #0000ff">break</span><span style="color: #000000">; </span><span style="color: #0000ff">case</span> <span style="color: #800080">5</span><span style="color: #000000">: StartActivity(</span><span style="color: #0000ff">typeof</span><span style="color: #000000">(Demo05FrameLayout)); </span><span style="color: #0000ff">break</span><span style="color: #000000">; } }; } } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> MyItems { </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Title { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span><span style="color: #000000">; } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Desc { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span><span style="color: #000000">; } } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyAdapter : BaseAdapter<myitems><span style="color: #000000"> { </span><span style="color: #0000ff">private</span> List<myitems><span style="color: #000000"> items; </span><span style="color: #0000ff">private</span><span style="color: #000000"> Activity context; </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span><span style="color: #000000"> Count { </span><span style="color: #0000ff">get</span><span style="color: #000000"> { </span><span style="color: #0000ff">return</span><span style="color: #000000"> items.Count; } } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> MyItems <span style="color: #0000ff">this</span>[<span style="color: #0000ff">int</span><span style="color: #000000"> position] { </span><span style="color: #0000ff">get</span><span style="color: #000000"> { </span><span style="color: #0000ff">return</span><span style="color: #000000"> items[position]; } } </span><span style="color: #0000ff">public</span> MyAdapter(Activity context, List<myitems><span style="color: #000000"> items) { </span><span style="color: #0000ff">this</span>.context =<span style="color: #000000"> context; </span><span style="color: #0000ff">this</span>.items =<span style="color: #000000"> items; } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">long</span> GetItemId(<span style="color: #0000ff">int</span><span style="color: #000000"> position) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> position; } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> View GetView(<span style="color: #0000ff">int</span><span style="color: #000000"> position, View convertView, ViewGroup parent) { </span><span style="color: #0000ff">var</span> item =<span style="color: #000000"> items[position]; View view </span>= <span style="color: #0000ff">null</span><span style="color: #000000">; view </span>= context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem2, <span style="color: #0000ff">null</span><span style="color: #000000">); view.FindViewById</span><textview>(Android.Resource.Id.Text1).Text =<span style="color: #000000"> item.Title; view.FindViewById</span><textview>(Android.Resource.Id.Text2).Text =<span style="color: #000000"> item.Desc; </span><span style="color: #0000ff">return</span><span style="color: #000000"> view; } } }</span></textview></textview></myitems></myitems></myitems></listview></myitems></myitems>
OK,这一章的第1讲就到这里了。