当前位置:  编程技术>移动开发
本页文章导读:
    ▪xcode3跟xcode4的模板位置        xcode3和xcode4的模板位置 xcode3的模板位置: BASE_TEMPLATE_DIR=”/Library/Application Support/Developer/Shared/XcodeBASE_TEMPLATE_USER_DIR=”$HOME/Library/Application Support/Developer/Shared/Xcode” ===================================.........
    ▪ 【通译】(25)一般布局对象        【翻译】(25)一般布局对象   【翻译】(25)一般布局对象   see http://developer.android.com/guide/topics/ui/layout-objects.html   原文见 http://developer.android.com/guide/topics/ui/layout-objects.html   -------------------------.........
    ▪ 【通译】(20)状态栏通知       【翻译】(20)状态栏通知   【翻译】(20)状态栏通知   see http://developer.android.com/guide/topics/ui/notifiers/notifications.html   原文见 http://developer.android.com/guide/topics/ui/notifiers/notifications.html   -------------.........

[1]xcode3跟xcode4的模板位置
    来源: 互联网  发布时间: 2014-02-18
xcode3和xcode4的模板位置

xcode3的模板位置:

BASE_TEMPLATE_DIR=”/Library/Application Support/Developer/Shared/Xcode

BASE_TEMPLATE_USER_DIR=”$HOME/Library/Application Support/Developer/Shared/Xcode”

================================================

xcode4的模板位置:
~/Library/Developer/Xcode/Templates


    
[2] 【通译】(25)一般布局对象
    来源: 互联网  发布时间: 2014-02-18
【翻译】(25)一般布局对象

 

【翻译】(25)一般布局对象

 

see

http://developer.android.com/guide/topics/ui/layout-objects.html

 

原文见

http://developer.android.com/guide/topics/ui/layout-objects.html

 

-------------------------------

 

Common Layout Objects

 

一般布局对象

 

-------------------------------

 

In this document

 

本文目录

 

* FrameLayout

* LinearLayout

* TableLayout

* RelativeLayout

* Summary of Important View Groups 重要视图组的总结

 

-------------------------------

 

This section describes some of the more common types of layout objects to use in your applications. Like all layouts, they are subclasses of ViewGroup.

 

本节描述一些在你的应用程序中使用的更一般的布局对象类型。像所有布局那样,它们是ViewGroup的子类。

 

Also see the Hello Views tutorials for some guidance on using more Android View layouts.

 

另见你好View教程以获取使用更多Android视图布局的一些指引。

 

-------------------------------

 

FrameLayout

 

FrameLayout is the simplest type of layout object. It's basically a blank space on your screen that you can later fill with a single object — for example, a picture that you'll swap in and out. All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot specify a different location for a child view. Subsequent child views will simply be drawn over previous ones, partially or totally obscuring them (unless the newer object is transparent).

 

FrameLayout是最简单的布局对象类型。它基本上在屏幕上是一个空白空间,稍后你可以用一个单一对象填充——例如,你将交互进出的一张图片。FrameLayout的所有子元素被定在屏幕的左上角;你不能指定一个子视图的不同位置。随后的子视图将简单地绘画在前一个的上方,部分地或整体地模糊它们(除非较新的对象是透明的)。

 

-------------------------------

 

LinearLayout

 

LinearLayout aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute. All children are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). A LinearLayout respects margins between children and the gravity (right, center, or left alignment) of each child.

 

LinearLayout以一个单一方向对齐所有子对象——垂直地或水平地,依赖于你如何定义orientation属性。所有子对象被一个接一个地堆叠,所以一个垂直列表每一行将只有一个子对象,不管它们有多宽,而水平列表将只有一行的高度(最高子对象的高度,加上内边距)。一个LinearLayout注重子对象之间的间距以及每个子对象的重力(右,中,或左对齐)。

 

LinearLayout also supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Child views can specify an integer weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero. For example, if there are three text boxes and two of them declare a weight of 1, while the other is given no weight (0), the third text box without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three boxes are measured. If the third box is then given a weight of 2 (instead of 0), then it is now declared "more important" than both the others, so it gets half the total remaining space, while the first two share the rest equally.

 

LinearLayout也支持赋予权重给单独的子对象。这个属性赋予一个“重要度”值给一个视图,并且允许它展开以填充在父视图中任意剩余的空间。子视图可以指定一个整型的权重值,然后在视图组中的任意剩余空间以它们声明的权重的比例赋予给子对象。默认权重为零。例如,如果有三个文本框而它们其中有两个声明权重为1,而其它没有赋予权重(0),第三个没有权重的文本框将不会增长并且将只占据它的内容所需的区域。另两个在所有三个框被度量后将同时展开以填充剩余的空间。如果第三个框之后赋予了2的权重(而非0),那么它现在被声明为比其它两个“更重要”,所以它获得全部剩余空间的一半,而前两个同时共享剩余空间。

 

-------------------------------

 

Tip: To create a proportionate size layout on the screen, create a container view group object with the layout_width and layout_height attributes set to fill_parent; assign the children height or width to 0 (zero); then assign relative weight values to each child, depending on what proportion of the screen each should have.

 

提示:为了在屏幕上创建一个比例大小的布局,创建一个容器视图组对象,其layout_width和layout_height设置为fill_parent;赋予子对象的height和width为0(零);然后赋予相应的weight值到各自的子对象,依赖于每个子对象应该拥有的屏幕比例。

 

-------------------------------

 

The following two forms represent a LinearLayout with a set of elements: a button, some labels and text boxes. The text boxes have their width set to fill_parent; other elements are set to wrap_content. The gravity, by default, is left. The difference between the two versions of the form is that the form on the left has weight values unset (0 by default), while the form on the right has the comments text box weight set to 1. If the Name textbox had also been set to 1, the Name and Comments text boxes would be the same height.

 

以下两个表单表现一个带有一组元素的LinearLayout:一个按钮,一些标签和文本框。文本框把它们的宽度设置为fill_parent;其它元素被设置为wrap_content。默认的重力是left。两种版本的表单的不同之处在于左边的表单拥有复位的权重(默认为0),而右边的表单把评论文本框的权重设置为1。如果Name文本框同时已经被设置为1,那么Name和Comments文本框将是相同的高度。

 

(图略:

左图未填满,右图填满

 

Within a horizontal LinearLayout, items are aligned by the position of their text base line (the first line of the first list element — topmost or leftmost — is considered the reference line). This is so that people scanning elements in a form shouldn't have to jump up and down to read element text in neighboring elements. This can be turned off by setting android:baselineAligned="false" in the layout XML.

 

在一个水平LinearLayout中,条目通过它们的文本基线来对齐(第一个列表元素的第一行——最高或最左——被认为是参考线)。因此扫描表单中的元素的人不必跳起和跳下以阅读邻近元素中的元素文本。可以通过设置布局XML中android:baselineAligned="false"来关闭它。

 

To view other sample code, see the Hello LinearLayout tutorial.

 

要查看其它示例代码,请参见你好LinearLayout教程。

 

-------------------------------

 

TableLayout

 

TableLayout positions its children into rows and columns. TableLayout containers do not display border lines for their rows, columns, or cells. The table will have as many columns as the row with the most cells. A table can leave cells empty, but cells cannot span columns, as they can in HTML.

 

TableLayout把它的子对象放进几个行和列。TableLayout容器不显示它们的行,列,或格子的边线。表格将跟行一样有相同多的列,带有最多的格子。一个表格可以让格子为空,但格子不能横跨多列,但它们在HTML中则可以。

 

TableRow objects are the child views of a TableLayout (each TableRow defines a single row in the table). Each row has zero or more cells, each of which is defined by any kind of other View. So, the cells of a row may be composed of a variety of View objects, like ImageView or TextView objects. A cell may also be a ViewGroup object (for example, you can nest another TableLayout as a cell).

 

TableRow对象是TableLayout的子视图(每个TableRow定义表中的单一行)。每行有零个或更多格子,它们中每个用其它类型的View定义。所以,一行的格子可以由各种View对象组成,像ImageView或TextView对象。一个格子也可以是一个ViewGroup对象(例如,你可以嵌套另一个TableLayout作为一个格子)。

 

The following sample layout has two rows and two cells in each. The accompanying screenshot shows the result, with cell borders displayed as dotted lines (added for visual effect).

 

以下示例布局分别拥有两行和两格。伴随的截屏显示结果,格子边线显示为点线(增加以获取可视效果)。

 

(图略:

Views/Layouts/TableLayout/04.Stretchable

打开 Ctrl-O

另存为 Ctrl-Shift-S

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:stretchColumns="1">

    <TableRow>

        <TextView

            android:text="@string/table_layout_4_open"

            android:padding="3dip" />

        <TextView

            android:text="@string/table_layout_4_open_shortcut"

            android:gravity="right"

            android:padding="3dip" />

    </TableRow>

 

    <TableRow>

        <TextView

            android:text="@string/table_layout_4_save"

            android:padding="3dip" />

        <TextView

            android:text="@string/table_layout_4_save_shortcut"

            android:gravity="right"

            android:padding="3dip" />

    </TableRow>

</TableLayout>

 

-------------------------------

 

Columns can be hidden, marked to stretch and fill the available screen space, or can be marked as shrinkable to force the column to shrink until the table fits the screen. See the TableLayout reference documentation for more details.

 

列可以是隐藏的,标记为拉伸和填充可用的屏幕空间,或可以被标记为可收缩以强制列收缩直至表适合屏幕。参见TableLayout的参考文档以获取更多细节。

 

To view sample code, see the Hello TableLayout tutorial.

 

要查看示例代码,请参见你好TableLayout教程

 

-------------------------------

 

RelativeLayout

 

RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. Elements are rendered in the order given, so if the first element is centered in the screen, other elements aligning themselves to that element will be aligned relative to screen center. Also, because of this ordering, if using XML to specify this layout, the element that you will reference (in order to position other view objects) must be listed in the XML file before you refer to it from the other views via its reference ID.

 

RelativeLayout让子视图指定它们的位置相对于父视图或相对于各自(用ID指定)。所以你可以通过右边界对齐两个元素,或者让其中一个在另一个的下面,在屏幕中居中,中间偏左,等等。元素以所给的顺序被渲染,所以如果第一个元素在屏幕中居中,其它对齐自己到那个元素的元素将相对于屏幕正中对齐。同样,因为这个顺序,如果使用XML指定这个布局,你将要引用的元素(为了定位其它视图对象)必须在你从其它视图中通过其引用ID引用它之前列举在XML文件。

 

The example below shows an XML file and the resulting screen in the UI. Note that the attributes that refer to relative elements (e.g., layout_toLeft) refer to the ID using the syntax of a relative resource (@id/id).

 

下面的示例展示一个XML文件和用户界面中的结果屏幕。注意引用相对元素的属性(例如layout_toLeft)用相关资源的语法(@id/id)来引用ID。

 

(图略

这里输入:

取消 确定

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android

                android:layout_width="fill_parent" 

                android:layout_height="wrap_content"

                android:background="@drawable/blue"

                android:padding="10px" >

 

    <TextView android:id="@+id/label" 

              android:layout_width="fill_parent" 

              android:layout_height="wrap_content" 

              android:text="Type here:" />

 

    <EditText android:id="@+id/entry" 

              android:layout_width="fill_parent" 

              android:layout_height="wrap_content" 

              android:background="@android:drawable/editbox_background"

              android:layout_below="@id/label" />

 

    <Button android:id="@+id/ok" 

            android:layout_width="wrap_content" 

            android:layout_height="wrap_content" 

            android:layout_below="@id/entry"

            android:layout_alignParentRight="true"

            android:layout_marginLeft="10px"

            android:text="OK" />

 

    <Button android:layout_width="wrap_content" 

            android:layout_height="wrap_content"

            android:layout_toLeftOf="@id/ok"

            android:layout_alignTop="@id/ok"

            android:text="Cancel" />

</RelativeLayout>

 

-------------------------------

 

Some of these properties are supported directly by the element, and some are supported by its LayoutParams member (subclass RelativeLayout for all the elements in this screen, because all elements are children of a RelativeLayout parent object). The defined RelativeLayout parameters are: width, height, below, alignTop, toLeft, padding[Bottom|Left|Right|Top], and margin[Bottom|Left|Right|Top]. Note that some of these parameters specifically support relative layout positions — their values must be the ID of the element to which you'd like this view laid relative. For example, assigning the parameter toLeft="my_button" to a TextView would place the TextView to the left of the View with the ID my_button (which must be written in the XML before the TextView).

 

这些属性有一些被元素直接支持,而有一些被它的LayoutParams成员支持(为这个屏幕中所有元素子类化RelativeLayout,因为所有元素都是一个RelativeLayout父对象的子对象)。被定义的RelativeLayout参数有:width,height,below,alignTop,toLeft,padding[Bottom|Left|Right|Top],以及margin[Bottom|Left|Right|Top]。注意这些参数有一些特别地支持相对布局位置——它们的值必须是你想让这个视图放置所相对的元素的ID。例如,赋予参数toLeft="my_button"给TextView将放置TextView到ID为my_button的View的左边。(它必须书写在XML中的TextView之前)。

 

To view this sample code, see the Hello RelativeLayout tutorial.

 

要查看这个示例代码,请参见你好RelativeLayout教程。

 

-------------------------------

 

Summary of Important View Groups

 

重要视图组的总结

 

These objects all hold child UI elements. Some provide their own form of a visible UI, while others are invisible structures that only manage the layout of their child views.

 

这些对象全都持有子用户界面元素。有一些提供它们自己的可视用户界面的形式,然而其他是不可见结构,只管理它们的子视图的布局。

 

-------------------------------

 

Class Description

 

类 描述

 

* FrameLayout Layout that acts as a view frame to display a single object.

 

* FrameLayout:行为如同一个视图帧的Layout,用于显示单一对象。

 

* Gallery A horizontal scrolling display of images, from a bound list.

 

* Gallery:来自一个被绑定列表的图片的水平滚动显示。

 

* GridView Displays a scrolling grid of m columns and n rows.

 

* GridView:显示一个m列n行的滚动格子。

 

* LinearLayout A layout that organizes its children into a single horizontal or vertical row. It creates a scrollbar if the length of the window exceeds the length of the screen.

 

* LinearLayout:一种布局,它组织它的子对象进一个单一水平或垂直的行。如果窗口的长度超过屏幕的长度,它创建一个滚动条。

 

* ListView Displays a scrolling single column list.

 

* ListView:显示一个滚动的单列列表。

 

* RelativeLayout Enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent).

 

* RelativeLayout:使你能指定子对象相对于各自(子对象A在子对象B的左边)或相对于父对象(相对于父对象的顶部对齐)的位置。

 

* ScrollView A vertically scrolling column of elements.

 

* ScrollView:一个垂直滚动的元素列。

 

* Spinner Displays a single item at a time from a bound list, inside a one-row textbox. Rather like a one-row listbox that can scroll either horizontally or vertically.

 

* Spinner:在某个时刻从一个被绑定列表中在一个单行文本框内显示单一条目。有点像一个可以水平或垂直滚动的单行列表框。

 

* SurfaceView Provides direct access to a dedicated drawing surface. It can hold child views layered on top of the surface, but is intended for applications that need to draw pixels, rather than using widgets.

 

* SurfaceView:提供对一个专用绘画表面的直接访问。它可以持有放置在表面上的子视图,但为需要绘画像素,而非使用部件的应用程序而设计。

 

* TabHost Provides a tab selection list that monitors clicks and enables the application to change the screen whenever a tab is clicked.

 

* TabHost:提供一个标签页选择列表,它监视点击并且使应用程序每当标签页被点击时改变屏幕。

 

* TableLayout A tabular layout with an arbitrary number of rows and columns, each cell holding the widget of your choice. The rows resize to fit the largest column. The cell borders are not visible.

 

* TableLayout:一个表格布局,带有一个任意数量的行和列,每格持有你选择的部件。行会改变大小以适合最大的列。格子边线是不可见的。

 

* ViewFlipper A list that displays one item at a time, inside a one-row textbox. It can be set to swap items at timed intervals, like a slide show.

 

* ViewFlipper:一个列表,在一个单行文本框中一次显示一个条目。它可以在定时的间隔时间上设置为交换条目,像一个幻灯片演示。

 

* ViewSwitcher Same as ViewFlipper.

 

* ViewSwitcher:同ViewFlipper。

 

-------------------------------

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 16 Dec 2011 21:54

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)



    
[3] 【通译】(20)状态栏通知
    来源: 互联网  发布时间: 2014-02-18
【翻译】(20)状态栏通知

 

【翻译】(20)状态栏通知

 

see

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

 

原文见

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

 

-------------------------------

 

Status Bar Notifications

 

状态栏通知

 

-------------------------------

 

Quickview

 

快速概览

 

* A status bar notification allows your application to notify the user of an event without interupting their current activity

 

* 状态栏通知允许你的应用程序通知事件的用户而不打断他们的当前活动

 

* You can attach an intent to your notification that the system will initiate when the user clicks it

 

* 你可以依附一个意图到你的通知,系统将在用户点击它时初始化它。

 

In this document

 

本文目录

 

* The Basics 基础

* Managing your Notifications 管理你的Notification

* Creating a Notification 创建一个Notification

* Updating the notification 更新通知

* Adding a sound 添加声音

* Adding vibration 添加振动

* Adding flashing lights 添加闪灯

* More features 更多特性

* Creating a Custom Notification Layout 创建一个自定义通知布局

 

Key classes

 

关键类

 

Notification

NotificationManager

 

-------------------------------

 

A status bar notification adds an icon to the system's status bar (with an optional ticker-text message) and a notification message in the notifications window. When the user selects the notification, Android fires an Intent that is defined by the Notification (usually to launch an Activity). You can also configure the notification to alert the user with a sound, a vibration, and flashing lights on the device.

 

状态栏通知添加一个图标到系统的状态栏(带有一个可选的纸带文本消息)以及在通知窗口中的一个通知消息。当用户选择通知时,Android发送一个被Notification定义的Intent(通常会启动一个Activity)。你还可以配置通知以便使用设备上的声音,振动和闪灯警告用户。

 

A status bar notification should be used for any case in which a background service needs to alert the user about an event that requires a response. A background service should never launch an activity on its own in order to receive user interaction. The service should instead create a status bar notification that will launch the activity when selected by the user.

 

状态栏通知应该用于后台Service需要警告用户需响应的事件的任何情况下。后台Service应该从不自己启动一个Activity以接收用户的交互。取而代之,Service应该创建一个状态栏通知,它将在被用户选中时启动Activity。

 

Figure 1 shows the status bar with a notification icon on the left side.

 

图1展示左侧带有一个通知图标的状态栏。

 

(图略:

3:40 PM

 

Figure 1. Status bar with a notification.

 

图1. 带有一个通知的状态栏。

 

Figure 2 shows the notification's message in the notifications window.

 

图2展示在通知窗口中通知的消息。

 

(图略:

2009年4月25日 下午3:41 

Android 清空通知

通知

特拉维斯

你在哪里? 3:39 PM

 

Figure 2. The notifications window.

 

图2. 通知窗口。

 

-------------------------------

 

The Basics

 

基础

 

An Activity or Service can initiate a status bar notification. Because an activity can perform actions only while it is running in the foreground and its window has focus, you will usually create status bar notifications from a service. This way, the notification can be created from the background, while the user is using another application or while the device is asleep. To create a notification, you must use two classes: Notification and NotificationManager.

 

一个Activity或Service可以初始化一个状态栏通知。因为一个活动可以执行动作仅当它正在运行在前台中并且它的窗口拥有焦点,所以通常你将从一个服务中创建状态栏通知。这样的话,通知可以在用户正在使用另一个应用程序或当设备正在休眠的时候,从后台中被创建。为了创建一个通知,你必须使用两个类:Notification和NotificationManager。

 

Use an instance of the Notification class to define the properties of your status bar notification, such as the status bar icon, the notification message, and extra settings such as a sound to play. The NotificationManager is an Android system service that executes and manages all status bar notifications. You do not instantiate the NotificationManager directly. In order to give it your Notification, you must retrieve a reference to the NotificationManager with getSystemService() and then, when you want to notify the user, pass it your Notification with notify().

 

使用一个Notification类的实例以定义你的状态栏通知的属性,诸如状态栏图标,通知消息,以及额外设置诸如要播放的声音。NotificationManager是一个Android系统服务,它执行并管理所有状态栏通知。你并不是直接实例化NotificationManager。为了把你的Notification传给它,你必须用getSystemService()获取指向NotificationManager的引用,然后当你希望通知用户时,用notify()把你的Notification对象传给它。

 

To create a status bar notification:

 

为了创建一个状态栏通知:

 

1. Get a reference to the NotificationManager:

 

1. 获取指向NotificationManager的引用:

 

-------------------------------

 

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

 

-------------------------------

 

2. Instantiate the Notification:

 

2. 实例化Notification:

 

-------------------------------

 

int icon = R.drawable.notification_icon;

CharSequence tickerText = "Hello";

long when = System.currentTimeMillis();

 

Notification notification = new Notification(icon, tickerText, when);

 

-------------------------------

 

3. Define the notification's message and PendingIntent:

 

3. 定义通知的消息和PendingIntent:

 

-------------------------------

 

Context context = getApplicationContext();

CharSequence contentTitle = "My notification";

CharSequence contentText = "Hello World!";

Intent notificationIntent = new Intent(this, MyClass.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

 

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

 

-------------------------------

 

4. Pass the Notification to the NotificationManager:

 

4. 传递Notification给NotificationManager:

 

-------------------------------

 

private static final int HELLO_ID = 1;

 

mNotificationManager.notify(HELLO_ID, notification);

 

-------------------------------

 

That's it. Your user has now been notified.

 

就这样。你的用户现在已经被通知了。

 

-------------------------------

 

Managing your Notifications

 

管理你的Notification

 

The NotificationManager is a system service that manages all notifications. You must retrieve a reference to it with the getSystemService() method. For example:

 

NotificationManager是一个管理所有通知的系统服务。你必须用getSystemService()获取指向它的引用。例如:

 

-------------------------------

 

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

 

-------------------------------

 

When you want to deliver your status bar notification, pass the Notification to the NotificationManager with notify(int, Notification). The first parameter is the unique ID for the notification and the second is the Notification object. The ID uniquely identifies the notification from within your application. The ID is necessary if you need to update the notification or (if your application manages different kinds of notifications) select the appropriate action when the user returns to your application via the intent defined in the notification.

 

当你希望传送你的状态栏通知时,用notify(int, Notification)传递Notification给NotificationManager。第一参数是通知的唯一ID而第二参数是Notification对象。ID在你的应用程序中唯一地标识通知。ID是必需的,如果你需要更新通知或者(如果你的应用程序管理不同类型的通知)在用户通过定义在通知内的意图返回到你的应用程序时,选择合适的动作。

 

To clear the status bar notification when the user selects it from the notifications window, add the "FLAG_AUTO_CANCEL" flag to your Notification. You can also clear it manually with cancel(int), passing it the notification ID, or clear all your notifications with cancelAll().

 

为了在用户从通知窗口中选择它时清空该状态栏通知,添加FLAG_AUTO_CANCEL标志到你的Notification对象。你还可以用cancel(int)手动清空它,把通知ID传给它,或者用cancelAll()清空你的所有通知。

 

-------------------------------

 

Creating a Notification

 

创建一个Notification

 

A Notification object defines the details of the notification message that is displayed in the status bar and notifications window, and any other alert settings, such as sounds and blinking lights.

 

一个Notification对象定义显示在状态栏和通知窗口中的通知消息的细节,以及其它任意警告设置,诸如声音和闪灯。

 

A status bar notification requires all of the following:

 

状态栏通知需要以下所有东西:

 

* An icon for the status bar

 

* 一个用于状态栏的图标

 

* A title and message, unless you define a custom notification layout

 

* 标题和消息,除非你定义一个自定义通知布局

 

* A PendingIntent, to be fired when the notification is selected

 

* 一个PendingIntent,在通知被选中时发送

 

Optional settings for the status bar notification include:

 

状态栏通知的可选设置包括:

 

* A ticker-text message for the status bar

 

* 用于状态栏的纸带文本消息

 

* An alert sound

 

* 警告声音

 

* A vibrate setting

 

* 振动设置

 

* A flashing LED setting

 

* 闪烁LED设置

 

The starter-kit for a new notification includes the Notification(int, CharSequence, long) constructor and the setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent) method. These define all the required settings for a notification. The following snippet demonstrates a basic notification setup:

 

用于创建新通知的初级(注:启动器?)工具箱包含Notification(int, CharSequence, long)构造函数以及setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent)方法。这些方法定义一个通知的所有所需的设置。以下代码片段演示一个基本通知配置:

 

-------------------------------

 

int icon = R.drawable.notification_icon;        // icon from resources 来自资源的图标

CharSequence tickerText = "Hello";              // ticker-text 纸带文本

long when = System.currentTimeMillis();         // notification time 通知时间

Context context = getApplicationContext();      // application Context 应用程序Context

CharSequence contentTitle = "My notification";  // message title 消息标题

CharSequence contentText = "Hello World!";      // message text 消息文本

 

Intent notificationIntent = new Intent(this, MyClass.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

 

// the next two lines initialize the Notification, using the configurations above

// 下面两行代码用上面的配置初始化Notification

Notification notification = new Notification(icon, tickerText, when);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

 

-------------------------------

 

Updating the notification

 

更新通知

 

You can update the information in your status bar notification as events continue to occur in your application. For example, when a new SMS text message arrives before previous messages have been read, the Messaging application updates the existing notification to display the total number of new messages received. This practice of updating an existing notification is much better than adding new notifications, because it avoids clutter in the notifications window.

 

你可以跟随继续发生在你的应用程序内的事件而更新你的状态栏通知的信息。例如,当一个新的SMS(注:短信服务)文本消息在前一个消息被阅读之前到达时,消息应用程序更新现存通知以显示接收新消息的总数。这条更新现存Notification的实践(注:经验)要远优于添加新的通知,因为避免了通知窗口内的杂乱。

 

Because each notification is uniquely identified by the NotificationManager with an integer ID, you can revise the notification by calling setLatestEventInfo() with new values, change some field values of the notification, and then call notify() again.

 

因为每个通知被NotificationManager用一个整型ID唯一标识,所以你可以通过用新的值调用setLatestEventInfo()来修正通知,改变通知的一些域的值,然后再次调用notify()。

 

You can revise each property with the object member fields (except for the Context and the notification title and text). You should always revise the text message when you update the notification by calling setLatestEventInfo() with new values for contentTitle and contentText. Then call notify() to update the notification. (Of course, if you've created a custom notification layout, then updating these title and text values has no effect.)

 

你可以用对象的成员域来修正每个属性(除Context和通知标题和文本以外)。你应该总是在你更新通知时修正文本消息,通过用contentTitle和contentText的新值调用setLatestEventInfo()。(当然,如果你已经创建好一个自定义的通知布局,那么更新这些标题和文本值会没有任何效果。)

 

Adding a sound

 

添加声音

 

You can alert the user with the default notification sound (which is defined by the user) or with a sound specified by your application.

 

你可以用默认的通知声音(由用户定义)或者用你的应用程序指定的声音警告用户。

 

To use the user's default sound, add "DEFAULT_SOUND" to the defaults field:

 

为了使用用户的默认声音,添加DEFAULT_SOUND到defaults域

 

-------------------------------

 

notification.defaults |= Notification.DEFAULT_SOUND;

 

-------------------------------

 

To use a different sound with your notifications, pass a Uri reference to the sound field. The following example uses a known audio file saved to the device SD card:

 

为了随你的通知使用不同的声音,传递一个Uri引用到sound域。以下示例使用保存在设备SD卡中的一个已知音频文件。

 

-------------------------------

 

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

 

-------------------------------

 

In the next example, the audio file is chosen from the internal MediaStore's ContentProvider:

 

在以下示例中,音频文件从内部MediaStore的ContentProvider中选择。

 

-------------------------------

 

notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

 

-------------------------------

 

In this case, the exact ID of the media file ("6") is known and appended to the content Uri. If you don't know the exact ID, you must query all the media available in the MediaStore with a ContentResolver. See the Content Providers documentation for more information on using a ContentResolver.

 

在这种情况下,媒体文件的精确ID(“6”)是已知的并且被尾加到内容Uri。如果你不知道精确ID,那么你必须用一个ContentResolver查询MediaStore中的所有可用媒体。参见内容提供者文档以获取关于使用ContentResolver的更多信息。

 

If you want the sound to continuously repeat until the user responds to the notification or the notification is cancelled, add FLAG_INSISTENT to the flags field.

 

如果你希望声音持续地重复直至用户响应通知或通知被取消,添加FLAG_INSISTENT到flags域。

 

-------------------------------

 

Note: If the defaults field includes DEFAULT_SOUND, then the default sound overrides any sound defined by the sound field.

 

注意:如果defaults域包含DEFAULT_SOUND,那么默认声音覆盖被sound域定义的任意声音。

 

-------------------------------

 

Adding vibration

 

添加振动

 

You can alert the user with the the default vibration pattern or with a vibration pattern defined by your application.

 

你可以用默认的振动模式或用一个被你的应用程序定义的振动模式警告用户。

 

To use the default pattern, add DEFAULT_VIBRATE to the defaults field:

 

为了使用默认模式,添加DEFAULT_VIBRATE到defaults域:

 

-------------------------------

 

notification.defaults |= Notification.DEFAULT_VIBRATE;

 

-------------------------------

 

To define your own vibration pattern, pass an array of long values to the vibrate field:

 

为了定义你自己的振动模式,传递一个long型数组值到vibrate域:

 

-------------------------------

 

long[] vibrate = {0,100,200,300};

notification.vibrate = vibrate;

 

-------------------------------

 

The long array defines the alternating pattern for the length of vibration off and on (in milliseconds). The first value is how long to wait (off) before beginning, the second value is the length of the first vibration, the third is the next length off, and so on. The pattern can be as long as you like, but it can't be set to repeat.

 

long型数组定义振动关与开长度(以毫秒为单位)的交替模式。最开始的值是从开始算起等待(关闭)的时长,第二个值是第一次振动的长度,第三个值是下一个关闭的长度,如此类推。该模式可以如你喜欢地长,但它不能被设置为重复。

 

-------------------------------

 

Note: If the defaults field includes DEFAULT_VIBRATE, then the default vibration overrides any vibration defined by the vibrate field.

 

注意:如果defaults域包含DEFAULT_VIBRATE,那么默认的振动覆盖任意被vibrate域定义的振动。

 

-------------------------------

 

Adding flashing lights

 

添加闪灯

 

To alert the user by flashing LED lights, you can implement the default light pattern (if available), or define your own color and pattern for the lights.

 

为了用闪烁的LED灯警告用户,你可以实现默认的灯光模式(如果可用),或者定义你自己的颜色和模式用于灯光。

 

To use the default light setting, add DEFAULT_LIGHTS to the defaults field:

 

为了使用默认的灯光设置,添加DEFAULT_LIGHTS到defaults域:

 

-------------------------------

 

notification.defaults |= Notification.DEFAULT_LIGHTS;

 

-------------------------------

 

To define your own color and pattern, define a value for the ledARGB field (for the color), the ledOffMS field (length of time, in milliseconds, to keep the light off), the ledOnMS (length of time, in milliseconds, to keep the light on), and also add FLAG_SHOW_LIGHTS to the flags field:

 

为了定义你自己的颜色和模式,为ledARGB域(对于颜色),ledOffMS域(时间长度,以毫秒为单位,以保持灯光关闭),ledOnMS域(时间长度,以毫秒为单位,以保持灯光开启)定义一个值,并且还要添加FLAG_SHOW_LIGHTS到flags域:

 

-------------------------------

 

notification.ledARGB = 0xff00ff00;

notification.ledOnMS = 300;

notification.ledOffMS = 1000;

notification.flags |= Notification.FLAG_SHOW_LIGHTS;

 

-------------------------------

 

In this example, the green light repeatedly flashes on for 300 milliseconds and turns off for one second. Not every color in the spectrum is supported by the device LEDs, and not every device supports the same colors, so the hardware estimates to the best of its ability. Green is the most common notification color.

 

在这个示例中,绿灯重复地持续闪烁300毫秒并关闭一秒。在光谱中不是每种颜色都被设备的LED支持,也不是所有设备支持相同的颜色,所以硬件会评估它的最佳能力。绿色是最一般的通知颜色。

 

More features

 

更多特性

 

You can add several more features to your notifications using Notification fields and flags. Some useful features include the following:

 

你可以使用Notification的域和标志添加更多的几个特性到你的通知。几个有用的特性包括如下:

 

* FLAG_AUTO_CANCEL flag

 

* FLAG_AUTO_CANCEL标志

 

Add this to the flags field to automatically cancel the notification after it is selected from the notifications window.

 

添加它到flags域以自动地在它从通知窗口中被选择时取消通知。

 

* FLAG_INSISTENT flag

 

* FLAG_INSISTENT标志

 

Add this to the flags field to repeat the audio until the user responds.

 

添加它到flags域以重复音频直至用户响应。

 

* FLAG_ONGOING_EVENT flag

 

* FLAG_ONGOING_EVENT标志

 

Add this to the flags field to group the notification under the "Ongoing" title in the notifications window. This indicates that the application is on-going — its processes are still running in the background, even when the application is not visible (such as with music or a phone call).

 

添加它到flags域以分组通知到通知窗口中的“正在执行”标题下。它指示应用程序正在执行——它的进程仍然运行在后台中,即便当应用程序不可见(诸如正在播放音乐或进行打电话)。

 

* FLAG_NO_CLEAR flag

 

* FLAG_NO_CLEAR标志

 

Add this to the flags field to indicate that the notification should not be cleared by the "Clear notifications" button. This is particularly useful if your notification is on-going.

 

添加它到flags域以指示通知不应该被“清空通知”按钮清空。它特别有用,如果你的通知正在执行。

 

* number field

 

* number域

 

This value indicates the current number of events represented by the notification. The appropriate number is overlaid on top of the status bar icon. If you intend to use this field, then you must start with "1" when the Notification is first created. (If you change the value from zero to anything greater during an update, the number is not shown.)

 

这个值指示由通知代表的事件的当前数量。合适的数量被叠加在状态栏图标的上方。如果你打算使用这个域,那么你必须在Notification最先被创建时必须从1开始。(如果在一次更新期间你从0开始改变该值到任意的更大值时,数字不会显示。)

 

* iconLevel field

 

* iconLevel域

 

This value indicates the current level of a LevelListDrawable that is used for the notification icon. You can animate the icon in the status bar by changing this value to correlate with the drawable's defined in a LevelListDrawable. See the LevelListDrawable reference for more information.

 

这个值指示一个用于通知图标的LevelListDrawable的当前级别。你可以通过改变这个值以关联到定义在LevelListDrawable中定义的可绘画对象的值,使该图标在状态栏中动画化。参见LevelListDrawable的参考文档以获取更多信息。

 

See the Notification class reference for more information about additional features that you can customize for your application.

 

参见Notification类参考文档以获取更多关于你可以为你的应用程序自定义的附加特性的信息。

 

-------------------------------

 

Creating a Custom Notification Layout

 

创建一个自定义通知布局

 

(图略:

2011年9月16日 3:53

Android 清空

通知

自定义通知

这是一个自定义布局

 

Figure 3. Notification with a custom layout.

 

图3. 带有自定义布局的通知。

 

By default, the notification that appears in the notifications window includes a title and the message text. These are defined by the contentTitle and contentText parameters of the setLatestEventInfo() method. However, you can also define a custom layout for the notification using RemoteViews. Figure 3 shows an example of a custom notification layout. It looks similar to the default notification, but is actually created with a custom XML layout.

 

默认,用在通知窗口中的通知包含一个标题和消息文本。这些东西由setLatestEventInfo()方法的contentTitle和contentText参数定义。然而,你还可以用RemoteViews为展开视图定义一个自定义布局。图3展示一个自定义通知布局的示例,它看上去类似于默认的通知,但实际上是用一个自定义的XML布局创建。

 

To define your own layout for the notification, instantiate a RemoteViews object that inflates a custom layout file, then pass the RemoteViews to the contentView field of your Notification.

 

为了为通知定义你自己的布局,实例化一个解压自定义布局文件的RemoteViews对象,然后传递RemoteViews给你的Notification的contentView域。

 

Creating a custom notification layout is best understood with an example:

 

用一个示例来更好地理解如何创建一个自定义通知布局。

 

1. Create the XML layout for the notification. For example, the following layout is called custom_notification.xml:

 

1. 创建通知的XML布局。例如,创建下面被称为custom_notification_layout.xml的布局:

 

-------------------------------

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/layout"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:padding="10dp" >

    <ImageView android:id="@+id/image"

        android:layout_width="wrap_content"

        android:layout_height="fill_parent"

        android:layout_alignParentLeft="true"

        android:layout_marginRight="10dp" />

    <TextView android:id="@+id/title"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_toRightOf="@id/image"

        />

    <TextView android:id="@+id/text"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_toRightOf="@id/image"

        android:layout_below="@id/title"

        />

</RelativeLayout>

 

-------------------------------

 

Notice that the two TextView elements include the style attribute. It's important that you use style resources for the text in your custom notifications, because the background color of the notification can vary across different devices and platform versions. Beginning with Android 2.3 (API level 9), the system defines a style for the text it uses for the default notification layouts. Thus, you should apply that style when running on Android 2.3 or higher to ensure that your text is visible against the background.

 

注意两个TextView元素包含style属性。它对于你对你的自定义通知的文本使用风格资源很重要,因为通知的背景色可能在不同设备和平台版本之间有差异。从Android 2.3(API级别9)开始,系统定义一个文本样式,它被用于默认的通知布局。这样,当运行于Android 2.3或更高版本上时你应该应用那个样式以确保你的文本对比背景而言是可见的。

 

For example, to use the standard text colors on versions of Android lower than 2.3, you should use the following styles for res/values/styles.xml:

 

例如,为了在低于2.3的Android版本上使用标准文本颜色,你应该使用以下res/values/styles.xml的样式:

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <style name="NotificationText">

      <item name="android:textColor">?android:attr/textColorPrimary</item>

    </style>

    <style name="NotificationTitle">

      <item name="android:textColor">?android:attr/textColorPrimary</item>

      <item name="android:textStyle">bold</item>

    </style>

    <!-- If you want a slightly different color for some text,

         consider using ?android:attr/textColorSecondary -->

    <!-- 如果你希望一些文本的颜色稍微不同,

         请考虑使用?android:attr/textColorSecondary -->

</resources>

 

-------------------------------

 

Then, to apply the system's default colors for notifications on Android 2.3 and higher, use the following styles for res/values-v9/styles.xml:

 

然后,对于Android 2.3和更高的通知应用系统的默认颜色,对res/values-v9/styles.xml使用以下样式:

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />

    <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />

</resources>

 

-------------------------------

 

Now, when running on Android 2.3 (API level 9) or higher, the text in your custom view will use the same colors that the system does for default notifications. This is important because later versions of Android actually change the background color of the notifications to be dark. Inheriting the system's styles ensures that your text will be light in such cases, but also if the background is some other unexpected color, your text will also change as appropriate.

 

现在,当运行于Android 2.3(API级别9)或更高时,你的自定义视图中的文本将使用与系统提供给默认通知的相同颜色。这很重要,因为较新版本的Android实际上把通知的背景色修改为变暗。继承系统的样式确保你的文本将在这种情况下被加亮,但同时如果背景是一些其它非预期的颜色,那么你的文本将同样在合适的时候改变。

 

2. Now, in the application code, use the RemoveViews methods to define the image and text. Then pass the RemoteViews object to the contentView field of the Notification, as shown in this example:

 

2. 现在,在应用程序代码中,使用RemoveViews的方法定义图片和文本。然后传递RemoteViews对象到Notification的contentView域,正如这个示例所示:

 

-------------------------------

 

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);

contentView.setImageViewResource(R.id.image, R.drawable.notification_image);

contentView.setTextViewText(R.id.title, "Custom notification");

contentView.setTextViewText(R.id.text, "This is a custom layout");

notification.contentView = contentView;

 

-------------------------------

 

As shown here, pass the application's package name and the layout resource ID to the RemoteViews constructor. Then, define the content for the ImageView and TextView, using the setImageViewResource() and setTextViewText(). In each case, pass the reference ID of the appropriate View object that you want to set, along with the value for that View. Finally, the RemoteViews object is passed to the Notification in the contentView field.

 

正如这里所示,传递应用程序的包名和布局资源ID到RemoteViews构造函数。然后,使用setImageViewResource()和setTextViewText(),定义ImageView和TextView的内容。在每种情况下,传递你希望设置的合适View对象的引用ID,以及用于那个View的值。最后,RemoteViews对象被传递给contentView域中的Notification。

 

3. Because you don't need the setLatestEventInfo() method when using a custom view, you must define the Intent for the Notification with the contentIntent field, as in this example:

 

3. 因为当使用一个自定义视图时你不需要setLatestEventInfo()方法,所以你必须用contentIntent域为Notification定义Intent,正如这个示例所示:

 

-------------------------------

 

Intent notificationIntent = new Intent(this, MyClass.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.contentIntent = contentIntent;

 

-------------------------------

 

4. The notification can now be sent as usual:

 

4. 现在通知可以像平常那样被发送:

 

-------------------------------

 

mNotificationManager.notify(CUSTOM_VIEW_ID, notification);

 

-------------------------------

 

The RemoteViews class also includes methods that you can use to easily add a Chronometer or ProgressBar in your notification's layout. For more information about creating custom layouts for your notification, refer to the RemoteViews class reference.

 

RemoteViews类还包含一些方法,你可以用它们轻易地在你的通知布局中添加一个Chronometer或ProgressBar。想获取关于用RemoteViews创建自定视图的更多信息,请参考RemoteViews类的参考文档。

 

-------------------------------

 

Caution: When creating a custom notification layout, you must take special care to ensure that your custom layout functions properly in different device orientations and resolutions. While this advice applies to all View layouts created on Android, it is especially important in this case because your layout real estate is very restricted. So don't make your custom layout too complex and be sure to test it in various configurations.

 

警告:当创建一个自定义通知布局时,你必须特别小心地确保你的自定义布局功能(注:函数?)在不同的设备方向和分辨率下是正确的。当这个建议应用被创建在Android上的所有View布局时,它在这种情况下特别地重要,因为你的布局真正的空间是非常有限的。所以不要让你的自定义布局太复杂并确保在不同的配置中测试它。

 

-------------------------------

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 09 Dec 2011 11:54

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)



    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android消息处理机制Looper和Handler详解 iis7站长之家
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3