码迷,mamicode.com
首页 > Windows程序 > 详细

API翻译 --- Supporting Different Devices 支持不同的设备

时间:2016-07-19 10:23:31      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:

 DEPENDENCIES AND PREREQUISITES  依赖和先决条件

  • Android 1.6 or higher

YOU SHOULD ALSO READ

  • Application Resources  应用资源

  • Designing for Multiple Screens 设计多屏幕

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设备,使用一个应用程序包。

Lessons


  • Supporting Different Languages 支持不同的语言

  • Learn how to support multiple languages with alternative string resources.  

学习如何支持多种语言替代字符串资源。

  • Supporting Different Screens 支持不同的屏幕

  • Learn how to optimize the user experience for different screen sizes and densities.

    学习如何优化用户体验不同的屏幕尺寸和密度。

  • Supporting Different Platform Versions 支持不同平台版本

  • Learn how to use APIs available in new versions of Android while continuing to support older versions of Android.

学习如何使用api提供新版本的Android,同时继续支持旧版本的Android



Supporting Different Languages 支持不同的语言

THIS CLASS TEACHES YOU TO  这节课教给你

  1. Create Locale Directories and String Files 创建本地目录和字符串文件

  2. Use the String Resources 使用字符串资源

YOU SHOULD ALSO READ

  • Localization Checklist  

  • Localization with Resources 本地化资源

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 a res/ directory in the top level of the project. Within this res/ directory are subdirectories for various resource types. There are also a few default files such as res/values/strings.xml, which holds your string values.

如果您创建了您的项目使用Android SDK工具(即创建一个Android项目),顶级的工具创建res/目录的项目。在这个res /目录是各种资源类型的子目录。也有一些默认的文件如res /价值/ strings.xml,持有你的字符串值。

Create Locale Directories and String Files


To add support for more languages, create additional valuesdirectories inside res/ 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, see Providing 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.xml

Add 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:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   
<stringname="title">My Application</string>
   
<stringname="hello_world">Hello World!</string>
</resources>

Spanish, /values-es/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   
<stringname="title">Mi Aplicación</string>
   
<stringname="hello_world">Hola Mundo!</string>
</resources>

French, /values-fr/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   
<stringname="title">Mon Application</string>
   
<stringname="hello_world">Bonjour le monde !</string>
</resources>

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, see Localization.

注意:您可以使用区域限定符(或任何配置我国)在任何资源类型,比如如果你想提供你的位图可拉的本地化版本。更多信息,see Localization。

Use the String Resources 使用字符串资源


You can reference your string resources in your source code and other XML files using the resource name defined by the <string> element‘s name attribute.

你可以引用字符串资源源代码和其他XML文件使用的资源名称定义的<字符串>元素的name属性。

In your source code, you can refer to a string resource with the syntax R.string.<string_name>. There are a variety of methods that accept a string resource this way.

在您的源代码,您可以引用一个字符串资源与语法R.string< string_name >。有各种各样的方法,它接受一个字符串资源。

For example:

// Get a string resource from your app‘s Resources
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/<string_name> whenever the XML attribute accepts a string value.

你可以引用字符串资源源代码和其他XML文件使用的资源名称定义的<字符串>元素的name属性。

For example:

<TextView
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:text="@string/hello_world"/>



Supporting Different Screens 支持不同的设备

THIS LESSON TEACHES YOU TO

  1. Create Different Layouts  创建不同的布局

  2. Create Different Bitmaps  创建不同的Bitmap对象

YOU SHOULD ALSO READ

  • Designing for Multiple Screens   多屏幕设计

  • Providing Resources 提供资源

  • Iconography design guide  图解设计指南

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)

  • 和四个广义密度:低(ldpi),中等(mdpi),高(hdpi)、额外的高(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.

还请注意,屏幕方向(美化或肖像)被认为是屏幕大小的变化,很多应用程序应该修改布局优化用户体验在每个方向。

Create Different Layouts 支持不同的布局


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 -<screen_size> suffix. For example, a unique layout for large screens should be saved under res/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 for large screens:

例如,这个项目包含一个默认布局和大屏幕的另一个布局:

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 Resources guide.

系统加载适当的布局文件目录根据屏幕大小的设备应用程序正在运行。更多的信息关于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, the layout/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 the large and land 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, read Designing for Multiple Screens.

注意:安卓3.2及以上支持定义屏幕尺寸的先进方法,允许您指定参考资料屏幕尺寸基于最低密度独立像素的宽度和高度。这节课不包括这项新技术。有关更多信息,readdesign多个屏幕。

Create Different Bitmaps 创建不同的位图


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)

  • ldpi: 0.75

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 the Iconography design guide.

更多关于创建图标的提示和指导方针为您的应用程序,请关注图解设计指南


Supporting Different Platform Versions支持不同的平台版本

THIS LESSON TEACHES YOU TO

  1. Specify Minimum and Target API Levels 最小和目标API版本

  2. Check System Version at Runtime 检查系统在运行时版本

  3. Use Platform Styles and Themes 使用平台风格和主题

YOU SHOULD ALSO READ

  • Android API Levels API版本

  • Android Support Library  支持库

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 for Platform Versions is 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 the Android Support Library in your app, which allows you to use several recent platform APIs on older versions.

小贴士:为了提供最好的特性和功能跨多个Android版本中,您应该使用Android支持图书馆在你的应用程序,它允许您使用最近的一些平台api在旧版本。

Specify Minimum and Target API Levels 

指定最小和目标API级别


The AndroidManifest.xml file describes details about your app and identifies which versions of Android it supports. Specifically, the minSdkVersion and targetSdkVersion attributes for the <uses-sdk element identify the lowest API level with which your app is compatible and the highest API level against which you’ve designed and tested your app.

AndroidManifest.xml文件描述应用程序的细节和标识版本的Android支持。具体来说,minSdkVersiontargetSdkVersion属性的< uses-sdk元素识别应用程序兼容的API级别最低和最高的API级别,你设计和测试应用程序。

For example:

<manifestxmlns:android="http://schemas.android.com/apk/res/android" ... >
   
<uses-sdkandroid:minSdkVersion="4"android:targetSdkVersion="15"/>
    ...
</manifest>

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 thetargetSdkVersion 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 the ActionBar by default on Android 3.0 and higher. To then add menu items to the action bar, you need to set android: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 the showAsAction attribute (that is, you do not need a separate version in res/menu-v11/).

注意:当解析XML资源,Android忽略XML属性不支持当前设备。所以你可以安全地使用XML属性仅支持的新版本,而不用担心旧版本打破当他们遇到代码。例如,如果你设置targetSdkVersion =“十一”,默认应用程序包括ActionBar Android 3.0和更高。然后添加菜单项操作栏,你需要setandroid:showAsAction = XML“ifRoom”在你的菜单资源。这样做是安全的在cross-version XML文件,因为旧版本的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 看起来像一个对话框:

<activityandroid:theme="@android:style/Theme.Dialog">

To make your activity have a transparent background:

让你的activity 有一个透明的背景:

<activityandroid:theme="@android:style/Theme.Translucent">

To apply your own custom theme defined in /res/values/styles.xml:

应用中定义自己的自定义主题/ res /values/ styles.xml:

<activityandroid:theme="@style/CustomTheme">

To apply a theme to your entire app (all activities), add the android:theme attribute to the <application> element:

将一个主题应用到您的整个应用程序(所有活动),添加android:主题<应用>元素属性:

<applicationandroid:theme="@style/CustomTheme">

For more about creating and using themes, read the Styles and Themes guide.

更多关于创建和使用主题,阅读风格和主题指南。

API翻译 --- Supporting Different Devices 支持不同的设备

标签:

原文地址:http://blog.csdn.net/mikky_android/article/details/51943712

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!