android多个屏幕显示,了解 Android 多屏显示:深入了解

了解 Android 多屏显示:深入了解Hello, Android Multiscreen: Deep Dive

10/05/2018

本文内容

在这个两部分的指南中,会扩展基本 Phoneword 应用程序(在“Hello,Android”指南中创建)以便处理第二个屏幕。在此过程中,引入了基本 Android 应用程序构建基块。包括对 Android 体系结构的更深入了解,以帮助你更好地了解 Android 应用程序结构和功能。In this two-part guide, the basic Phoneword application (created in the Hello, Android guide) is expanded to handle a second screen. Along the way, the basic Android application building blocks are introduced. A deeper dive into Android architecture is included to help you develop a better understanding of Android application structure and functionality.

在 Hello,Android 多屏幕快速入门中,你生成并运行了第一个多屏幕 Xamarin.Android 应用程序。In the Hello, Android Multiscreen Quickstart, you built and ran your first multi-screen Xamarin.Android application.

本指南将探讨更高级的 Android 体系结构。In this guide you will explore more advanced Android architecture. 说明了具有意向 的 Android 导航,并探讨了 Android 硬件导航选项。Android navigation with Intents is explained, and Android hardware navigation options are explored. 剖析了对 Phoneword 应用添加的新功能,同时你会对应用程序与操作系统和其他应用程序之间的关系形成更全面的观点。New additions to the Phoneword app are dissected as you develop a more holistic view of the application's relationship with the operating system and other applications.

Android 体系结构基础知识Android architecture basics

在 Hello,Android 深入了解中,你了解到 Android 应用程序是独有的程序,因为它们缺少单一入口点。In the Hello, Android Deep Dive, you learned that Android applications are unique programs because they lack a single entry point. 相反,操作系统(或其他应用程序)可启动应用程序的任何一个已注册活动,这进而会启动应用程序的进程。Instead, the operating system (or another application) starts any one of the application's registered Activities, which in turn starts the process for the application. 此 Android 体系结构深入了解通过介绍 Android 应用程序构建基块及其功能,扩展了你对 Android 应用程序的构造原理的了解。This deep dive into Android architecture expands your understanding of how Android applications are constructed by introducing the Android Application Building Blocks and their functions.

Android 应用程序构建基块Android application building blocks

Android 应用程序由特殊 Android 类的集合组成,这些类称为应用程序块 ,与任何数量的应用资源(图像、主题、帮助程序类等)捆绑在一起。– 这些类通过称为 Android 清单 的 XML 文件进行协调。An Android application consists of a collection of special Android classes called Application Blocks bundled together with any number of app resources - images, themes, helper classes, etc. – these are coordinated by an XML file called the Android Manifest.

应用程序块组成 Android 应用程序的主干,因为它们使你可以执行使用常规类通常无法完成的操作。Application Blocks form the backbone of Android applications because they allow you to do things you couldn't normally accomplish with a regular class. 两个最重要的块是 活动 和 服务 :The two most important ones are Activities and Services:

活动 – 活动与具有用户界面的屏幕对应,在概念上类似于 Web 应用程序中的网页。Activity – An Activity corresponds to a screen with a user interface, and it is conceptually similar to a web page in a web application. 例如,在新闻源应用程序中,登录屏幕会是第一个活动,新闻项的可滚动列表会是另一个活动,而每个项的详细信息页面会是第三个活动。For example, in a newsfeed application, the login screen would be the first Activity, the scrollable list of news items would be another Activity, and the details page for each item would be a third. 可以在活动生命周期指南中了解有关活动的详细信息。You can learn more about Activities in the Activity Lifecycle guide.

服务 – Android 服务通过接管长时间运行的任务并在后台运行它们来支持活动。Service – Android Services support Activities by taking over long-running tasks and running them in the background. 服务没有用户界面,用于处理未绑定到屏幕的任务 – 例如,在后台播放歌曲或将照片上传到服务器。Services don't have a user interface and are used to handle tasks that aren't tied to screens – for example, playing a song in the background or uploading photos to a server. 有关服务的详细信息,请参阅创建服务和 Android 服务指南。For more information about Services, see the Creating Services and Android Services guides.

Android 应用程序可能不会使用所有类型的块,通常具有一种类型的多个块。An Android application may not use all types of Blocks, and often has several Blocks of one type. 例如,来自 Hello,Android 快速入门的 Phoneword 应用程序只由一个活动(屏幕)和一些资源文件组成。For example, the Phoneword application from the Hello, Android Quickstart was composed of just one Activity (screen) and some resource files. 简单音乐播放器应用可能具有多个活动以及一个用于在应用处于后台时播放音乐的服务。A simple music player app might have several Activities and a Service for playing music when the app is in the background.

意向Intents

Android 应用程序中的另一个基本概念是意向 。Another fundamental concept in Android applications is the Intent.

Android 围绕最小特权原则 进行设计 – 应用程序只能访问它们正常工作所需的块,它们对组成操作系统或其他应用程序的块具有有限的访问权限。Android is designed around the principle of least privilege – applications have access only to the Blocks they require to work, and they have limited access to the Blocks that make up the operating system or other applications. 同样,块是松散耦合 的 – 它们设计为对其他块(甚至是属于同一应用程序的块)知之甚少且仅有有限的访问权限。Similarly, Blocks are loosely-coupled – they are designed to have little knowledge of and limited access to other Blocks (even blocks that are part of the same application).

为了进行通信,应用程序块会来回发送异步消息(称为意向 )。To communicate, Application Blocks send asynchronous messages called Intents back and forth. 意向包含有关接收块的信息,有时还包含一些数据。Intents contain information about the receiving Block and sometimes some data. 从一个应用组件发送的意向会触发某个事件在其他应用组件中发生,从而将两个应用组件绑定在一起并允许它们进行通信。An Intent sent from one App component triggers something to happen in another App component, binding the two App components and allowing them to communicate. 通过来回发送意向,你可以使块来协调复杂操作(如启动相机应用以进行拍摄和保存、收集位置的信息或从一个屏幕导航到下一个屏幕)。By sending Intents back and forth, you can get Blocks to coordinate complex actions such as launching the camera app to take and save, gathering location information, or navigating from one screen to the next.

AndroidManifest.XMLAndroidManifest.XML

将一个块添加到应用程序时,它会向称为 Android 清单 的特殊 XML 文件进行注册。When you add a Block to the application, it is registered with a special XML file called the Android Manifest. 清单会跟踪应用程序中的所有应用程序块,以及版本要求、权限和链接库 – 操作系统为使应用程序正常运行而需要了解的所有内容。The Manifest keeps track of all Application Blocks in an application, as well as version requirements, permissions, and linked libraries – everything that the operating system needs to know for your application to run. Android 清单 也可与活动和意向配合工作,以控制适合于给定活动的操作。The Android Manifest also works with Activities and Intents to control what actions are appropriate for a given Activity. 使用 Android 清单指南中介绍了 Android 清单 的这些高级功能。These advanced features of the Android Manifest are covered in the Working with the Android Manifest guide.

在单屏幕版本的 Phoneword 应用程序中,只使用了一个活动、一个意向和 AndroidManifest.xml,以及其他资源(如图标)。In the single-screen version of the Phoneword application, only one Activity, one Intent, and the AndroidManifest.xml were used, alongside additional resources like icons. 在多屏幕版本的 Phoneword 中,添加了一个其他活动;它使用意向从第一个活动启动。In the multi-screen version of Phoneword, an additional Activity was added; it was launched from the first Activity using an Intent. 下一部分探讨意向如何帮助在 Android 应用程序中创建导航。The next section explores how Intents help to create navigation in Android applications.

Android 导航Android navigation

意向过去用于在屏幕之间进行导航。Intents were used to navigate between screens. 现在可以深入了解此代码,以了解意向的工作原理并了解它们在 Android 导航窗格中的角色。It's time to dive into this code to see how Intents work and understand their role in Android navigation.

使用意向启动第二个活动Launching a second activity with an intent

在 Phoneword 应用程序中,意向用于启动第二个屏幕(活动)。In the Phoneword application, an Intent was used to launch a second screen (Activity). 首先创建一个意向,传入当前上下文 (this,用于引用当前上下文)以及你所查找的应用程序块的类型 (TranslationHistoryActivity):Start by creating an Intent, passing in the current Context (this, referring to the current Context) and the type of Application Block that you're looking for (TranslationHistoryActivity):

Intent intent = new Intent(this, typeof(TranslationHistoryActivity));

上下文是与有关应用程序环境的全局信息之间的接口 – 它使新创建的对象可以了解应用程序的进展情况。The Context is an interface to global information about the application environment – it lets newly-created objects know what's going on with the application. 如果将意向视为消息,则要提供消息收件人的名称 (TranslationHistoryActivity) 和接收方的地址 (Context)。If you think of an Intent as a message, you are providing the name of the message recipient (TranslationHistoryActivity) and the receiver's address (Context).

Android 提供一个选项,用于将简单数据附加到意向(复杂数据以不同方式进行处理)。Android provides an option to attach simple data to an Intent (complex data is handled differently). 在 Phoneword 示例中,PutStringArrayExtra 用于将电话号码列表附加到意向,而 StartActivity 会对意向的收件人进行调用。In the Phoneword example, PutStringArrayExtra is used to attach a list of phone numbers to the Intent and StartActivity is called on the recipient of the Intent. 完整代码如下所示:The completed code looks like this:

translationHistoryButton.Click += (sender, e) =>

{

var intent = new Intent(this, typeof(TranslationHistoryActivity));

intent.PutStringArrayListExtra("phone_numbers", _phoneNumbers);

StartActivity(intent);

};

Phoneword 中引入的其他概念Additional concepts introduced in phoneword

Phoneword 应用程序引入了多个本指南中未提及的概念。The Phoneword application introduced several concepts not covered in this guide. 这些概念包括:These concepts include:

字符串资源 – 在 Phoneword 应用程序中,TranslationHistoryButton 的文本设置为 "@string/translationHistory"。String Resources – In the Phoneword application, the text of the TranslationHistoryButton was set to "@string/translationHistory". @string 语法表示字符串的值存储在 字符串资源文件 (Strings.xml) 中。The @string syntax means that the string's value is stored in the string resources file, Strings.xml. translationHistory 字符串的以下值已添加到 Strings.xml 中:The following value for the translationHistory string was added to Strings.xml:

Call History

有关字符串资源和其他 Android 资源的详细信息,请参阅 Android 资源指南。For more information on string resources and other Android resources, refer to the Android Resources guide.

ListView 和 ArrayAdapter – ListView 是一个 UI 组件,它提供了显示行的滚动列表的简单方法。ListView and ArrayAdapter – A ListView is a UI component that provides a simple way to present a scrolling list of rows. ListView 实例需要 适配器 ,以向它馈送行视图中包含的数据。A ListView instance requires an Adapter to feed it with data contained in row views. 下面的代码行用于填充 TranslationHistoryActivity 的用户界面:The following line of code was used to populate the user interface of TranslationHistoryActivity:

this.ListAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, phoneNumbers);

Listview 和适配器不在此文档的讨论范围内,但它们在非常全面的 Listview 和适配器 指南中进行了介绍。ListViews and Adapters are beyond the scope of this document, but they are covered in the very comprehensive ListViews and Adapters guide.

使用数据填充 ListView 专门处理使用内置 ListActivity 和 ArrayAdapter 类来创建并填充 ListView,而无需定义自定义布局(如 Phoneword 示例中所进行的那样)。Populating a ListView With Data deals specifically with using built-in ListActivity and ArrayAdapter classes to create and populate a ListView without defining a custom layout, as was done in the Phoneword example.

总结Summary

祝贺,你已完成第一个多屏 Android 应用程序!Congratulations, you've completed your first multi-screen Android application! 本指南介绍了 Android 应用程序构建基块 和 意向,并使用它们生成了一个多屏幕 Android 应用程序。This guide introduced Android Application Building Blocks and Intents and used them to build a multi-screened Android application. 现在,你已具有坚实的基础,可开始开发自己的 Xamarin.Android 应用程序。You now have the solid foundation you need to start developing your own Xamarin.Android applications.

接下来,你会在生成跨平台应用程序指南中了解如何使用 Xamarin 生成跨平台应用程序。Next, you'll learn to build cross-platform applications with Xamarin in the Building Cross-Platform Applications guides.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值