android 设置资源,Android 资源
Android 資源Android Resources
02/01/2018
本文內容
本文介紹了 Xamarin 中 Android 資源的概念,并介紹了如何使用這些資源。其中介紹了如何使用 Android 應用程序中的資源來支持應用程序本地化和多個設備,包括不同的屏幕大小和密度。This article introduces the concept of Android resources in Xamarin.Android and will document how to use them. It covers how to use resources in your Android application to support application localization, and multiple devices including varying screen sizes and densities.
概述Overview
Android 應用程序很少只是源代碼。An Android application is seldom just source code. 通常有許多其他文件組成應用程序:視頻、圖像、字體和音頻文件。There are often many other files that make up an application: video, images, fonts, and audio files just to name a few. 這些非源代碼文件統稱為資源,并在生成過程中進行編譯,并打包為 APK 以便分發和安裝到設備上:Collectively, these non-source code files are referred to as resources and are compiled (along with the source code) during the build process and packaged as an APK for distribution and installation onto devices:
資源為 Android 應用程序提供了若干優點:Resources offer several advantages to an Android application:
代碼分離– 將源代碼與圖像、字符串、菜單、動畫、顏色等分隔開來。當本地化時,此類資源可以很大的幫助。Code-Separation – Separates source code from images, strings, menus, animations, colors, etc. As such resources can help considerably when localizing.
面向多個設備– 提供對不同設備配置的更簡單支持,而無需更改代碼。Target multiple devices – Provides simpler support of different device configurations without code changes.
編譯時檢查– 資源是靜態的,并編譯到應用程序中。Compile-time Checking – Resources are static and compiled into the application. 這允許在編譯時檢查資源的使用情況,在這種情況下,可以輕松地捕獲和更正錯誤,而不是在更難找到且更昂貴的時候運行。This allows the usage of the resources to be checked at compile time, when it will be easy to catch and correct the mistakes, as opposed to run-time when it is more difficult to locate and costly to correct.
當啟動新的 Xamarin Android 項目時,將創建一個名為 "資源" 的特殊目錄以及一些子目錄:When a new Xamarin.Android project is started, a special directory called Resources is created, along with some subdirectories:
在上面的圖像中,應用程序資源根據其類型組織到這些子目錄中:圖像將進入可繪制目錄;視圖位于布局子目錄中,等等。In the image above, the application resources are organized according to their type into these subdirectories: images will go in the drawable directory; views go in the layout subdirectory, etc.
在上面的圖像中,應用程序資源根據其類型組織到這些子目錄中:圖像將進入mipmap目錄;視圖位于布局子目錄中,等等。In the image above, the application resources are organized according to their type into these subdirectories: images will go in the mipmap directory; views go in the layout subdirectory, etc.
可以通過兩種方法在 Xamarin Android 應用程序中訪問這些資源:以編程方式在代碼中,并使用特殊的 xml 語法在 XML 中以聲明方式。There are two ways to access these resources in a Xamarin.Android application: programmatically in code and declaratively in XML using a special XML syntax.
這些資源稱為 "默認資源",并由所有設備使用,除非指定了更具體的匹配項。These resources are called Default Resources and are used by all devices unless a more specific match is specified. 此外,每種類型的資源可能有可選的備用資源,Android 可能會使用這些資源來定位特定設備。Additionally, every type of resource may optionally have Alternate Resources that Android may use to target specific devices. 例如,可能會提供資源以面向用戶的區域設置、屏幕大小,或者設備是否旋轉了90度(從縱向到橫向)等。在上述每種情況下,Android 都將加載用于應用程序的資源,而無需開發人員進行任何額外的編碼工作。For example, resources may be provided to target the user's locale, the screen size, or if the device is rotated 90 degrees from portrait to landscape, etc. In each of these cases, Android will load the resources for use by the application without any extra coding effort by the developer.
通過向包含給定資源類型的目錄的末尾添加一個名為限定符的短字符串來指定備用資源。Alternate resources are specified by adding a short string, called a qualifier, to the end of the directory holding a given type of resources.
例如,資源/可繪制的 de將為設置為德語區域設置的設備指定映像,而資源/可繪制的將為設置為法語區域設置的設備保留圖像。For example, resources/drawable-de will specify the images for devices that are set to a German locale, while resources/drawable-fr would hold images for devices set to a French locale. 在下圖中,提供備用資源的示例如下所示:在運行同一應用程序時,只需更改設備區域設置:An example of providing alternate resources can be seen in the image below where the same application is being run with just the locale of the device changing:
本文全面介紹了如何使用資源,并涵蓋了以下主題:This article will take a comprehensive look at using resources and cover the following topics:
Android 資源基礎知識– 以編程方式使用默認資源,并以聲明方式向應用程序添加資源類型(如圖像和字體)。Android Resource Basics – Using default resources programmatically and declaratively, adding resource types such as images and fonts to an application.
設備特定配置– 支持應用程序中的不同屏幕分辨率和密度。Device Specific Configurations – Supporting the different screen resolutions and densities in an application.
本地化– 使用資源來支持應用程序可使用的不同區域。Localization – Using resources to support the different regions an application may be used.
相關鏈接Related Links
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的android 设置资源,Android 资源的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rizomuv焊接快捷键(rizomuv
- 下一篇: android封装oauth2,Andr