1. 程式人生 > >android的api文件裡Fragments翻譯(未完)

android的api文件裡Fragments翻譯(未完)

2012/01/25

首先,感覺自己要有一個突破。Android的版本更新那麼快,你根上它的步伐了嗎?顯然沒有。

那現在開始吧,從androidapi文件裡就能學到最新的技術。

學會看DevGuide!

學習本來就是一個漫長的過程,不要怕慢。

哪怕一個單詞一個單詞的查字典,也有意義!加油!

下面邊學邊翻譯這個英文文件:

Fragments:

AFragment represents a behavior or a portion of user interface in an Activity.You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running.

翻譯:

Fragment描述一個行為或是一個Activity的使用者介面的一部分。你可以在單個activity裡結合多個fragments來建立一個多格(multi-paneUI介面並可以在多個activity之間複用一個fragment。你可以把fragment想成一個acitivity的組合部件,它有自己的生命週期,接收自己的輸入事件,並且你能在activity執行時,新增,刪除它。

A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in theresumed

), you can manipulate each fragment independently, such as add or remove them. When you perform such a fragment transaction, you can also add it to a back stack that's managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the BACK key.

翻譯:

fragment必須常常嵌入在一個activity並且它的生命週期被”host activity”(宿主activity)的生命週期直接影響。例如,當這個activitypaused,其中的所有fragment也被paused,當這個activitydestroyed,其中的所有fragment也被destroyed。然而,當activity將要執行時(be running(它在生命週期狀態的resumed),你可以獨立操作每個fragment,例如新增,刪除它們。當你執行這個fragmenttransaction(處理),你能把它新增到由主activity管理的backstack(返回棧)中--每個backstack(返回棧)記錄了被觸發的fragmenttransactionback stack(返回棧)允許使用者倒退fragment transaction,通過按後退鍵。

When you add a fragment as a part of your activity layout, it lives in ainside the activity's view hierarchy and defines its own layout of views. You can insert a fragment into your activity layout by declaring the fragment in the activity's layout file, as a<fragment>element, or from your application code by adding it to an existing.However, a fragment is not required to be a part of the activity layout; you may also use a fragment as an invisible worker for the activity.

翻譯:

當你新增一個fragment作為你activity佈局的一部分時,它存活於在activityview層的ViewGroup裡並且可以定義它自己的view佈局。你能插入一個fragment,通過在activitylayout檔案裡宣告一個fragment,如一個<fragment>元素,或在程式程式碼裡把它新增到一個存在的ViewGroup裡。然而,一個fragment不被要求是activitylayout的一部分;你也可以用一個不可見的fragment

This document describes how to build your application to use fragments,including how fragments can maintain their state when added to the activity's back stack, share events with the activity and other fragments in the activity, contribute to the activity's action bar,and more.

翻譯:

這個文件描述如何建立你的程式來應用fragments,包括當新增到activitybackstack(返回棧)裡時,fragments怎麼維護(maintain)它們的狀態,如何與activity,還有其它fragments,共享事件。在actionbarcontribute(貢獻),還有其它一些內容。

DesignPhilosophy

Android introduced fragments in Android 3.0 (API Level "Honeycomb"), primarily to support more dynamic and flexible UI designs on large screens, such as tablets. Because a tablet's screen is much larger than that of a mobile phone, there's more room to combine and interchange UI components. Fragments allow such designs without the need for you to manage complex changes to the view hierarchy. By dividing the layout of an activity into fragments, you become able to modify the activity's appearance at runtime and preserve those changes in a backstack that's managed by the activity.

翻譯:

設計思想(哲學):

AndroidAndroid3.0API版本“蜂巢”)裡介紹過fragments,主要是在大屏上支援最多的動態靈活的UI設計,如tablets。因為一個talbet屏比手機屏更大,有足夠的空間來組合並交換UI元件。Fragments充許沒有管理複雜view層變換的需要的設計。通過把activity的佈局分成多個fragment,在執行時,你可以修改activity的外觀,並且在backstack(由activity管理的)裡儲存變化。

For example, a news application can use one fragment to show a list of articles on the left and another fragment to display an article on the right—both fragments appear in one activity, side by side, and each fragment has its own set of lifecycle callback methods and handle their own user input events. Thus, instead of using one activity to select an article and another activity to read the article, the user can select an article and read it all within the same activity, as illustrated in figure 1.

翻譯:

例如,一個程式可以在左邊用一個fragment顯示一個商品表,在右邊顯示這個商品的詳情--兩個fragment在一個activity裡顯示,並排,每個fragment有自己的生命週期回撥方法並處理自己的輸入事件。這樣,使用者可以在同一個activity裡選擇一個商品並檢視詳情的方法,來代替在一個activity裡選擇商品,在另一個activity裡檢視詳情,如下圖figure1


Figure 1. An example of how two UI modules that are typically separated into two activities can be combined into one activity, using fragments.

翻譯:

Figure1.這個例子是用fragments來把兩個典型的分離在兩個activity裡的UI模組組合在一個activity裡。

2012/02/15

A fragment should be a modular and reusable component in your application. That is, because the fragment defines its own layout and its own behavior using its own lifecycle callbacks, you can include one fragment in multiple activities. This is especially important because it allows you to adapt your user experience to different screen sizes. For instance, you might include multiple fragments in an activity only when the screen size is sufficiently large, and, when it is not,launch separate activities that use different fragments.

翻譯:

fragment應該是一個標準和可重用的元件。那是因為fragment定義它們自己的layout,自己的行為,用自己的生命週期回撥,你能在多個activity裡包含一個fragment.這點特別重要,因為它能在不同螢幕上有好的使用者體驗。例如,你可以在一個activity上包含多個fragment,僅當螢幕尺寸足夠大時,並當螢幕不大時,用不同的fragment分離activitys

For example—to continue with the news application example—the application can embed two fragments inActivity A, when running on an extra large screen (a tablet, for example).However, on a normal-sized screen (a phone, for example),there's not be enough room for both fragments, soActivity A includes only the fragment for the list of articles, and when the user selects an article, it startsActivity B, which includes the fragment to read the article. Thus, the application supports both design patterns suggested in figure 1.

翻譯:
例如--繼續以上面那個圖的應用為例--這個應用能嵌入兩個fragment在Activity A,當執行在一個特大螢幕時(如在tablet 平板),然而,當執行在一個普通大小的螢幕時,沒有足夠的空間放下兩個fragment,所以Activity A只包含商品列表的fragment,當用戶選擇一個商品時,它start 顯示商品詳情的Activity B,因而,應用支援兩種設計模式。

2012/02/20

Creating a Fragment

To create a fragment, you must create a subclass of  (or an existing subclass of it). The class has code that looks a lot like an . It contains callback methods similar to an activity, such as, and . In fact, if you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.

翻譯:

建立一個fragment,你必須建立一個fragment的子類(或者它的已存在的子類)。這個framgent類程式碼上很像activity。包括和activity很相似的回撥方法,例如, 和 onStop()事實上,如果你正用fragment轉換一個現存的android應用,你可能僅僅是複製程式碼,從你的activity的回撥到你fragment的回撥。

Usually, you should implement at least the following lifecycle methods:

The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.
The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
翻譯:

通常,你至少需要implement下面的生命週期方法:

      當建立這個fragment時,系統呼叫這個方法。在你的實現中,你需要初始化基本的元件--你想保留的,當fragment在生命週期的paused or stopped,然後又resumed

    當fragment第一次畫UI時,系統呼叫這個方法。你必須從這個方法裡返回一個View 作為你的fragment的根佈局(root layout).也可以返回null ,如果這個fragment不提供UI(使用者介面)。

    當用戶離開這個fragment時,系統調這個方法(儘管通常不是說這個fragment正在被destroyed).在這裡你通常要提示一些改變狀態值(因為使用者也許不會再回到這個介面)

2012/03/18

Most applications should implement at least these three methods for every fragment, but there are several other callback methods you should also use to handle various stages of the fragment lifecycle. All the lifecycle callback methods are discussed more later, in the section about Handling the Fragment Lifecycle.

翻譯:
大多數應用至少需要為每個fragment,implement這三個方法,但是有幾個其它回撥方法,你也應該處理fragment生命週期的各種不同階段。全部生命週期的回撥方法在稍後討論,在Handling the Fragment Lifecycle.

There are also a few subclasses that you might want to extend, instead of the base  class:

Displays a floating dialog. Using this class to create a dialog is a good alternative to using the dialog helper methods in the  class, because you can incorporate a fragment dialog into the back stack of fragments managed by the activity, allowing the user to return to a dismissed fragment.
Displays a list of items that are managed by an adapter (such as a ), similar to. It provides several methods for managing a list view, such as the callback to handle click events.
Displays a hierarchy of  objects as a list, similar to . This is useful when creating a "settings" activity for your application.
翻譯:
DialogFragment

       顯示一個懸浮的對話方塊。應用這個類來建立一個對話方塊對於用dialog helper 方法是比較好的,因為你可以把一個fragment對話方塊入一個fragment back stack,充許使用者返回一個消失的fragment.

      顯示一個被adapter管理的列表,類似ListActivity。它提供一些方法來管理list view,例如onListItemClick()回撥來處理點選事件。

      顯示一個層級的Preference物件作為list,類似PreferenceActivity。這是一個有益於建立一個"settings"似的activity。