編輯:關於Android編程
DEPENDENCIES AND PREREQUISITES 依賴和先決條件
Android 1.6 or higher
Application Resources 應用資源
Android devices come in many shapes and sizes all around the world. With a wide range of device types, you have an opportunity to reach a huge audience with your app. In order to be as successful as possible on Android, your app needs to adapt to various device configurations. Some of the important variations that you should consider include different languages, screen sizes, and versions of the Android platform.
Android設備有許多各式各樣的形狀和大小。廣泛的設備類型,為了盡可能成功在Android上,您的應用程序需要適應不同的設備配置。一些重要的變化,你應該考慮包括不同的語言,屏幕尺寸和版本的Android平台。
This class teaches you how to use basic platform features that leverage alternative resources and other features so your app can provide an optimized user experience on a variety of Android-compatible devices, using a single application package (APK).
這門課教您如何使用基本的平台特性,利用替代資源和其他特性,應用程序可以提供一個優化用戶體驗各種各樣的android設備,使用一個應用程序包。
Supporting Different Languages支持不同的語言
學習如何支持多種語言替代字符串資源。
Supporting Different Screens支持不同的屏幕
學習如何優化用戶體驗不同的屏幕尺寸和密度。
Supporting Different Platform Versions支持不同平台版本
學習如何使用api提供新版本的Android,同時繼續支持舊版本的Android。
Create Locale Directories and String Files創建本地目錄和字符串文件
Localization Checklist
It’s always a good practice to extract UI strings from your app code and keep them in an external file. Android makes this easy with a resources directory in each Android project.
它總是一個很好的實踐從應用程序代碼中提取UI字符串和一個外部文件。Android使這項任務變得很容易資源目錄中每個Android項目。
If you created your project using the Android SDK Tools (readCreating an Android Project), the tools create ares/
directory in the top level of the project. Within thisres/
directory are subdirectories for various resource types. There are also a few default files such asres/values/strings.xml
, which holds your string values.
如果您創建了您的項目使用Android SDK工具(即創建一個Android項目),頂級的工具創建res/目錄的項目。在這個res /目錄是各種資源類型的子目錄。也有一些默認的文件如res /價值/ strings.xml,持有你的字符串值。
To add support for more languages, create additionalvalues
directories insideres/
that include a hyphen and the ISO language code at the end of the directory name. For example,values-es/
is the directory containing simple resourcess for the Locales with the language code "es". Android loads the appropriate resources according to the locale settings of the device at run time. For more information, seeProviding Alternative Resources.
添加支持更多的語言,創造額外的價值在res目錄,包括連字符和ISO國家代碼的目錄名稱。例如,values-es /目錄包含簡單的對企業的地區語言代碼“西文”。Android加載合適的資源根據語言環境設置的設備在運行時。
Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:
一旦你決定你將支持的語言,創建資源目錄和字符串資源文件。例如:
MyProject/
res/ values/ strings.xml values-es/ strings.xml values-fr/ strings.xmlAdd the string values for each locale into the appropriate file.
每個地區的字符串值添加到適當的文件。
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
For example, the following are some different string resource files for different languages.
在運行時,Android系統使用字符串資源的適當設置基於地區目前設置為用戶的設備。
例如,下面是一些不同的字符串資源文件不同的語言。
English (default locale),/values/strings.xml
:
My Application Hello World!
Spanish,/values-es/strings.xml
:
Mi Aplicación Hola Mundo!
French,/values-fr/strings.xml
:
Mon Application Bonjour le monde !
Note:You can use the locale qualifier (or any configuration qualifer) on any resource type, such as if you want to provide localized versions of your bitmap drawable. For more information, seeLocalization.
注意:您可以使用區域限定符(或任何配置我國)在任何資源類型,比如如果你想提供你的位圖可拉的本地化版本。更多信息,see Localization。
You can reference your string resources in your source code and other XML files using the resource name defined by the
element'sname
attribute.
你可以引用字符串資源源代碼和其他XML文件使用的資源名稱定義的<字符串>元素的name屬性。
In your source code, you can refer to a string resource with the syntaxR.string.
. There are a variety of methods that accept a string resource this way.
在您的源代碼,您可以引用一個字符串資源與語法R.string。
For example:
// Get a string resource from your app'sResources
String hello =getResources()
.getString(R.string.hello_world); // Or supply a string resource to a method that requires a string TextView textView =newTextView(this); textView.setText(R.string.hello_world);
In other XML files, you can refer to a string resource with the syntax@string/
whenever the XML attribute accepts a string value.
你可以引用字符串資源源代碼和其他XML文件使用的資源名稱定義的<字符串>元素的name屬性。
For example:
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world"/>
Create Different Layouts 創建不同的布局
Designing for Multiple Screens 多屏幕設計
Providing Resources提供資源
Android categorizes device screens using two general properties: size and density. You should expect that your app will be installed on devices with screens that range in both size and density. As such, you should include some alternative resources that optimize your app’s appearance for different screen sizes and densities.
Android使用兩個通用屬性分類設備屏幕:大小和密度。你應該希望你的應用程序將被安裝在設備屏幕大小和密度范圍。因此,你應該包括一些替代資源,優化你的應用程序的外觀不同的屏幕尺寸和密度。
There are four generalized sizes: small, normal, large, xlarge
·有四種廣義大小:小,正常的,大,超大
And four generalized densities: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)
To declare different layouts and bitmaps you'd like to use for different screens, you must place these alternative resources in separate directories, similar to how you do for different language strings.
聲明不同的布局和位圖你想使用不同的屏幕,你必須將這些可替代資源在不同的目錄,類似於你如何為不同的語言字符串。
Also be aware that the screens orientation (landscape or portrait) is considered a variation of screen size, so many apps should revise the layout to optimize the user experience in each orientation.
還請注意,屏幕方向(美化或肖像)被認為是屏幕大小的變化,很多應用程序應該修改布局優化用戶體驗在每個方向。
To optimize your user experience on different screen sizes, you should create a unique layout XML file for each screen size you want to support. Each layout should be saved into the appropriate resources directory, named with a-
suffix. For example, a unique layout for large screens should be saved underres/layout-large/
.
優化你的用戶體驗在不同的屏幕尺寸,您應該創建一個獨特的XML布局文件為每個屏幕尺寸你想要支持的。每個布局應該保存到相應的資源目錄,命名——< screen_size >後綴。例如,大屏幕的一個獨特的布局應該被保存在res / layout-large /。
Note:Android automatically scales your layout in order to properly fit the screen. Thus, your layouts for different screen sizes don't need to worry about the absolute size of UI elements but instead focus on the layout structure that affects the user experience (such as the size or position of important views relative to sibling views).
注意:Android自動平衡你為了正確地適應屏幕布局。因此,你的布局不同的屏幕尺寸不需要擔心UI元素的絕對大小,而是集中在布局結構,影響用戶體驗(如重要的視圖的大小或位置相對於兄弟視圖)。
For example, this project includes a default layout and an alternative layout forlargescreens:
例如,這個項目包含一個默認布局和大屏幕的另一個布局:
MyProject/ res/ layout/ main.xml layout-large/ main.xml
The file names must be exactly the same, but their contents are different in order to provide an optimized UI for the corresponding screen size.
文件名必須是完全相同的,但是他們的內容是不同的,以提供一個優化的用戶界面對應的屏幕大小。
Simply reference the layout file in your app as usual:
只是參考布局文件在您的應用程序像往常一樣:大小。
@Override protectedvoid onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); }
The system loads the layout file from the appropriate layout directory based on screen size of the device on which your app is running. More information about how Android selects the appropriate resource is available in theProviding Resourcesguide.
系統加載適當的布局文件目錄根據屏幕大小的設備應用程序正在運行。更多的信息關於Android如何選擇適當的資源可以在提供資源指南。
As another example, here's a project with an alternative layout for landscape orientation:
另外一個例子,這裡有一個項目與橫向的另一個布局:
MyProject/ res/ layout/ main.xml layout-land/ main.xml
By default, thelayout/main.xml
file is used for portrait orientation.
默認情況下,layout/main.xml文件是用來描繪方向
If you want to provide a special layout for landscape, including while on large screens, then you need to use both thelarge
andland
qualifier:
如果你想提供一個特殊的美化布局,包括在大屏幕上,那麼您需要使用本地權限。
MyProject/ res/ layout/ # default (portrait) main.xml layout-land/ # landscape main.xml layout-large/ # large (portrait) main.xml layout-large-land/ # large landscape main.xml
Note:Android 3.2 and above supports an advanced method of defining screen sizes that allows you to specify resources for screen sizes based on the minimum width and height in terms of density-independent pixels. This lesson does not cover this new technique. For more information, readDesigning for Multiple Screens.
注意:安卓3.2及以上支持定義屏幕尺寸的先進方法,允許您指定參考資料屏幕尺寸基於最低密度獨立像素的寬度和高度。這節課不包括這項新技術。有關更多信息,readdesign多個屏幕。
You should always provide bitmap resources that are properly scaled to each of the generalized density buckets: low, medium, high and extra-high density. This helps you achieve good graphical quality and performance on all screen densities.
你應該提供適當比例的位圖資源:低、中、高、特高的密度。這有助於你實現良好的圖像質量和性能在所有屏幕密度
To generate these images, you should start with your raw resource in vector format and generate the images for each density using the following size scale:
生成這些圖片,你應該開始你的原始資源在每個密度格式和生成圖像中使用以下尺度:
xhdpi: 2.0
hdpi: 1.5
mdpi: 1.0 (baseline)
This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 100x100 for mdpi, and 75x75 for ldpi devices.
這意味著如果你為xhdpi生成一個200 x200型圖像設備,你應該在150 x150 hdpi生成相同的資源,為mdpi 100 x100,75 x75 ldpi設備
Then, place the files in the appropriate drawable resource directory:
然後,將適當的可拉的資源目錄中的文件:
MyProject/ res/ drawable-xhdpi/ awesomeimage.png drawable-hdpi/ awesomeimage.png drawable-mdpi/ awesomeimage.png drawable-ldpi/ awesomeimage.png
Any time you reference@drawable/awesomeimage
, the system selects the appropriate bitmap based on the screen's density.
任何時候你參考@drawable/awesomeimage,系統選擇適當的位圖基於屏幕的密度。
Note:Low-density (ldpi) resources aren’t always necessary. When you provide hdpi assets, the system scales them down by one half to properly fit ldpi screens.
注意:低密度(ldpi)資源並不總是必要的。提供hdpi資產時,系統規模下降了一半正確適合ldpi屏幕。
For more tips and guidelines about creating icon assets for your app, see theIconography design guide.
更多關於創建圖標的提示和指導方針為您的應用程序,請關注圖解設計指南
Specify Minimum and Target API Levels最小和目標API版本
Check System Version at Runtime檢查系統在運行時版本
Android API LevelsAPI版本
While the latest versions of Android often provide great APIs for your app, you should continue to support older versions of Android until more devices get updated. This lesson shows you how to take advantage of the latest APIs while continuing to support older versions as well.
雖然Android經常提供的最新版本的api應用程序,你應該繼續支持舊版本的Android直到更多的設備得到更新。這節課向你展示了如何利用最新的api,同時繼續支持舊版本。
The dashboard forPlatform Versionsis updated regularly to show the distribution of active devices running each version of Android, based on the number of devices that visit the Google Play Store. Generally, it’s a good practice to support about 90% of the active devices, while targeting your app to the latest version.
平台版本的儀表板是定期更新顯示活躍的分布運行每個版本的Android設備,根據設備的數量訪問谷歌商店。一般來說,它是一個很好的實踐支持大約90%的活躍的設備,同時針對應用程序到最新版本。
Tip:In order to provide the best features and functionality across several Android versions, you should use theAndroid Support Libraryin your app, which allows you to use several recent platform APIs on older versions.
小貼士:為了提供最好的特性和功能跨多個Android版本中,您應該使用Android支持圖書館在你的應用程序,它允許您使用最近的一些平台api在舊版本。
指定最小和目標API級別
TheAndroidManifest.xmlfile describes details about your app and identifies which versions of Android it supports. Specifically, theminSdkVersion
andtargetSdkVersion
attributes for the
AndroidManifest.xml文件描述應用程序的細節和標識版本的Android支持。具體來說,minSdkVersion和targetSdkVersion屬性的
For example:
As new versions of Android are released, some style and behaviors may change. To allow your app to take advantage of these changes and ensure that your app fits the style of each user's device, you should set the
targetSdkVersion
value to match the latest Android version available.
新版本的Android被發布,一些風格和行為可能會改變。讓你的應用程序利用這些變化,並確保您的應用程序符合風格的每個用戶的設備,你應該設定targetSdkVersion值以匹配最新的Android版本。
Check System Version at Runtime
在運行時檢查系統版本
Android provides a unique code for each platform version in the
Build
constants class. Use these codes within your app to build conditions that ensure the code that depends on higher API levels is executed only when those APIs are available on the system.
Android提供了一個獨特的代碼為每個平台版本構建常量類。使用這些代碼在您的應用程序來構建條件,確保API的代碼依賴於高水平只有當這些API執行系統上可用。
privatevoid setUpActionBar(){
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.HONEYCOMB){
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Note:When parsing XML resources, Android ignores XML attributes that aren’t supported by the current device. So you can safely use XML attributes that are only supported by newer versions without worrying about older versions breaking when they encounter that code. For example, if you set the
targetSdkVersion="11"
, your app includes theActionBar
by default on Android 3.0 and higher. To then add menu items to the action bar, you need to setandroid:showAsAction="ifRoom"
in your menu resource XML. It's safe to do this in a cross-version XML file, because the older versions of Android simply ignore theshowAsAction
attribute (that is, youdo notneed a separate version inres/menu-v11/
).
注意:當解析XML資源,Android忽略XML屬性不支持當前設備。所以你可以安全地使用XML屬性僅支持的新版本,而不用擔心舊版本打破當他們遇到代碼。例如,如果你設置targetSdkVersion=“十一”,默認應用程序包括ActionBarAndroid3.0和更高。然後添加菜單項操作欄,你需要setandroid:showAsAction=XML“ifRoom”在你的菜單資源。這樣做是安全的在cross-versionXML文件,因為舊版本的Android完全無視showAsAction屬性(也就是說,您不需要一個單獨的版本在res/menu-v11/)。
Use Platform Styles and Themes
使用平台的風格和主題
Android provides user experience themes that give apps the look and feel of the underlying operating system. These themes can be applied to your app within the manifest file. By using these built in styles and themes, your app will naturally follow the latest look and feel of Android with each new release.
Android提供了用戶體驗主題,讓應用程序的外觀和感覺的底層操作系統。這些主題中可以應用到你的應用程序清單文件。通過使用這些建於風格和主題,應用自然會追隨最新的外觀和感覺每個新版本的Android系統。
To make your activity look like a dialog box:
讓你的activity看起來像一個對話框:
To make your activity have a transparent background:
讓你的activity有一個透明的背景:
To apply your own custom theme defined in
/res/values/styles.xml
:
應用中定義自己的自定義主題/res/values/styles.xml:
To apply a theme to your entire app (all activities), add the
android:theme
attribute to theelement:
將一個主題應用到您的整個應用程序(所有活動),添加android:主題<應用>元素屬性:
For more about creating and using themes, read theStyles and Themesguide.
更多關於創建和使用主題,閱讀風格和主題指南。
本文實例講述了Android開發之自定義控件用法。分享給大家供大家參考,具體如下:今天和大家分享下組合控件的使用。很多時候android自定義控件並不能滿足需求,如何做呢
最近學習了一個視頻公開課,講到了利用HorizontalScrollView仿ViewPager設計的一個簡單相冊,其實主要用了ViewPager緩存的思想。此篇文章參考
要求 Tab 標簽可以橫向滾動,標簽可選擇,並且在選擇的時候有標線下劃線。 分析 可繼承HorizontalScrollView 實現,然後裡面標簽ITem可可以是Tex
monkeyrunner腳本使用Python語法編寫,但它實際上是通過Jython來解釋執行。 Jython是Python的Java實現,它將Python代碼解釋成Jav