外文翻譯Android(安桌)_第1頁
外文翻譯Android(安桌)_第2頁
外文翻譯Android(安桌)_第3頁
外文翻譯Android(安桌)_第4頁
外文翻譯Android(安桌)_第5頁
已閱讀5頁,還剩30頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、android起航譯使用xml進行布局雖然純粹通過java代碼在activity上創(chuàng)建和添加部件,在技術上是可行的,我們在第4章中做的一樣,更常見的方法是使用一種基于xml的布局文件。動態(tài)的小部件實例保留更多,情況復雜,小工具在編譯時不為人所知(例如,在數(shù)據(jù)檢索了互聯(lián)網(wǎng)基礎上將單選按鈕填充柱??紤]到這一點,現(xiàn)在是時候打破xml來學習如何用此種方式來布置android activities。什么是基于xml的布局?正如其名稱所示,一個基于xml的布局是一個關系到每個規(guī)格的小部件,和他們的容器(更多關于此內(nèi)容的在第7章)編碼的xml格式。具體來說,android認為基于xml的布局是資源,因此布局

2、文件存儲在res /在你的android項目布局目錄中。每個xml文件包含一個指定的部件和容器布局元素樹,一種意見認為構成層次。對xml元素的屬性,描述一個部件應如何看或者一個容器應如何運轉。例如,如果一個按鈕元素。有一個android的屬性值:文字樣式=“bold”,這意味著該文本出現(xiàn)在按鈕的表面應該是呈現(xiàn)一個粗體字體樣式.android的sdk中附帶一個使用的布局的工具(aapt)。這個工具應自動調(diào)用你的android工具鏈(例如,eclipse中,ants build.xml)。作為一個開發(fā)人員,尤其重要的是,在您的項目中aapt生成r.java源文件,讓您能在那些布局中直接從java代

3、碼中獲取布局和部件。為什么使用基于xml的布局?使用xml布局文件做的大部分都可以通過java代碼。例如,你可以使用settypeface()命令一個按鈕使用粗體文本,而不是在一個xml布局中使用屬性。由于xml布局是為你跟蹤的另一個文件,所以我們需要好的理由來使用這樣的文件。也許最大的原因是為了在視圖定義中協(xié)助工具的建立,如ide中一個gui創(chuàng)建者像eclipse或者一個像droiddraw1設計gui圖形用戶界面建設者。這樣gui建設者們,在原則上,生成java代碼而不是xml。目前的挑戰(zhàn)是重新閱讀用戶界面的定義,以支持編輯,也就是說,如果是像xlm的結構公式數(shù)據(jù)比一個程序語言中的數(shù)據(jù)簡單

4、的多。此外,保持生成的xml定義從手寫的java代碼中分離,使得某人定制的來源意外重新生成不太可能。xml形成一個良好的中間立場,使工具作家使用更簡便,程序員需要時手工工作更簡易。此外,xml作為一個gui定義格式是越來越普遍。微軟的xaml,adobe的flex,和mozilla的xul都采取android類似的方法:把布局細節(jié)放在一個xml文件和把編程智慧資料放在源文件(例如,xul中的javascript)。許多不太知名的圖形用戶界面框架,如zk,還使用視圖定義的xml。而“隨大流”并不一定是最好的政策,但他們有優(yōu)勢幫助從任何其他xml為中心的觀點描述語言輕松進入android。好了,那

5、么基于xml的布局是什么樣子的?下面是以前的章節(jié)的示例應用程序按鈕,轉換成一個xml布局文件,布局/ nowredux示例項目,在這一章中可以找到源代碼的領域。 部件,按鈕的類名稱形成xml元素的名稱。因為按鈕是android提供的部件,我們可以只使用裸類的名稱。如果您創(chuàng)建自己的部件作為android.view.view子小部件,您也將需要提供一個完整的包聲明(如monsware.android.mywidget)。根元素需要android的xml命名空間聲明:xmlns:android=所有其他要素將成為子根并繼承該命名空間的聲明。因為我們要引用這個來自java代碼的按鈕,我們需要通過and

6、roid給它一個標識符:id屬性。我們將在本章后面更詳細的介紹這個概念。其余的屬性是此按鈕實例屬性:android:文字表示的初始文本將顯示在按鈕(這種情況顯示空字符串)android:layout_width和android:layout_height命令android有按鈕的寬度和高度填寫 “parent”,這種情況下,整個屏幕。將這些屬性將在第7章中詳解。由于這個單一部件是activity的僅有內(nèi)容,我們只需要這一個因素。復雜的用戶界面將需要整個樹的元素,代表工具和容器,控制自己的定位。所有的這本書余下的章節(jié)將使用xml布局,所以還有數(shù)十種更復雜的其他布局實例,請前進到第七章仔細閱讀。符

7、號有什么用途?許多部件和容器只需要出現(xiàn)在xml布局文件,不須引用在java代碼。例如,一個靜態(tài)標簽(textview)只需要在布局文件中以表明它應該出現(xiàn)在那里。在xml文件中各種元素文件不需要有android:id屬性給他們一個名稱。任何你想要在java資源中使用的東西,都需要一個android:id.該公約是使用 +id.作為id值,其中的.代表你locallyunique名稱有問題的部件。在上一節(jié)的xml布局的例子中, +id是按鈕控件的標識符。android提供了一些特殊的android:id值,形式android:id/.我們將在這本書的不同章節(jié)中看到這些,例如第八章和第十章。我們將這

8、些附加到java如何?既然你有意建立一個xml配置文件的工具和容器,名為main.xml存儲res/layout,所有你需要的是一個在您activity的oncreate()回調(diào)以使用該版式:setcontentview(r.layout.main);這是相同的setcontentview(),我們前面使用,通過它的一個視圖子類的實例(在這種情況下,一個按鈕)。該android制造的觀點,來自我們的布局,是從訪問該代碼生成的r類。所有的布局都可以訪問r.layout,由基地鍵控布局文件的名稱- main.xml result in r.layout.main.要訪問確定部件,使用findvie

9、wbyid(),在數(shù)字標識符傳遞有問題的部件。這一數(shù)字標識符生成的r類由android在r.id.something(其中一些是你正在尋找的具體部件)。這些部件是只是子類的視圖,就像我們在第四章中創(chuàng)建button實例。剩下的部分在原始的now演示中,按鈕的表面便會顯示當前的時間,這將反映當按鈕被最后按下時顯示的時間(或者如果在按鈕尚未被按下時顯示)。這種邏輯仍然適用,即使在該修訂演示(nowredux)中。盡管如此,在activitys oncreate() callback中的實例,我們可以從xml的布局參考一個例子:package monsware.android.layouts;impo

10、rt android.app.activity;import android.os.bundle;import android.view.view;import android.widget.button;import java.util.date;public class nowredux extends activityimplements view.onclicklistener button btn;overridepublic void oncreate(bundle icicle) super.oncreate(icicle);setcontentview(r.layout.mai

11、n);btn=(button)findviewbyid(r.id.button);btn.setonclicklistener(this);updatetime();public void onclick(view view) updatetime();private void updatetime() btn.settext(new date().tostring();第一個區(qū)別是,在java代碼中設置內(nèi)容視圖作為視圖,我們將它設置為引用的xml布局(setcontentview(r.layout.main)。該r.java源文件將被更新,當我們重建這個項目,包括對我們布局參考文件(存儲在m

12、ain.xml in our projects res/layout directory )。另一個不同是,我們需要親手實驗按鈕實例,我們使用findviewbyid()調(diào)用。既然我們發(fā)現(xiàn)按鈕為 +id/button,我們可以參考按鈕的標識符r.id.button。現(xiàn)在,隨著手手頭上的實例,我們可以設置回調(diào)并根據(jù)需要設置標簽。正如你可以看到如圖5-1,結果看起來與原始的now演示一樣。圖5-1 樣本的nowredux活動使用基本部件每一個gui工具包都有一些基本的部件:字段,標簽,按鈕等,android的工具包在范圍內(nèi)沒有不同,其基本部件將提供一個良好的介紹,關于這些部件在android ac

13、tivities中是如何運行的。指派標簽最簡單的部件是標簽,在android提到的作為一個textview。像大多數(shù)的gui工具包,標簽的文本是不可被用戶直接編輯的。通常情況下,它們被用來確定相鄰部件(例如,一個“姓名:”一個填充姓名前的標簽)。在java中,你可以通過創(chuàng)建一個textview的實例l來創(chuàng)建一個標簽。更常見的,雖然,你將通過添加一個textview元素到布局來在xml布局文件中創(chuàng)建標簽,與一個android:文本屬性來設置標簽的本身價值。如果您需要交換基于某些標準的標簽,例如國際化,你可能想使用xml中的資源參考代替,這些將在第9章敘述。 textview有許多相關的其他標簽屬

14、性,如: android:typeface to set the typeface to use for the label (e.g., monospace) android:textstyle to indicate that the typeface should be made bold (bold), italic (italic),or bold and italic (bold_italic) android:textcolor to set the color of the labels text, in rgb hex format (e.g., #ff0000for red

15、)例如,在basic/label項目中,你將找到下列布局文件:正如你看到的圖6-1,圖6-1 示例應用程序的labeldemo只是單獨的布局,由android的項目生成器提供的java源的(如activitycreator),生成應用程序。按鈕,歸屬于誰?我們已經(jīng)在第4和第5章看到了按鈕部件用法。按鈕是文本視圖的一個子類,所以一切都在上一節(jié)討論了,按鈕格式所面臨的問題仍然成立。短暫的圖像 android有兩個部件,來幫助你將照片嵌入activities:imageview和imagebutton。正如名稱所暗示的,他們是分別對于文本視圖和按鈕基于圖像的類似物。每個部件帶有一個android:s

16、rc屬性(在一個xml布局中),指明使用什么圖片。這些通常引用一個可繪制的資源,在講資源的這個章節(jié)中更詳細地描述了。您還可以通過setimageuri()從內(nèi)容提供商在uri基礎上設置圖像。 imagebutton控件,一個imageview子類,混合在標準按鈕行為中,應對點擊和諸如此類的東西。例如,從basic/imageview樣本項目中看main.xml布局,這可以在以及所有其他代碼示例種找到。結果,只用代碼生成activity,如圖6-2所示。圖6-2 示例應用程序的imageviewdemo綠色字段或者其他色彩緊接著按鈕和標簽,字段是大多數(shù)gui工具包的第三個“錨”。在android

17、中,他們通過edittext部件運行,它是標簽的一個子類textview。隨著標準textview屬性(例如,android:文本樣式),edittext有許多其他方面可以幫助你創(chuàng)建字段,包括: android:autotext, to control if the field should provide automatic spelling assistance android:capitalize, to control if the field should automatically capitalize the first letterof entered text (e.g.,

18、first name, city) android:digits, to configure the field to accept only certain digits android:singleline, to control if the field is for single-line input or multiple-line input(e.g., does move you to the next widget or add a newline?)除了這些,你可以使用專門配置字段輸入方法,如android:僅數(shù)字輸入numeric,android:為籠罩密碼輸入密碼,還有a

19、ndroid:phonenumber進入電話號碼。如果你想創(chuàng)建自己的輸入法計劃(如郵政編碼,社會安全號碼),您需要創(chuàng)建自己的執(zhí)行情況inputmethod接口,然后通過android設定字段來使用:inputmethod。例如,從the basic/field項目,這里是一個xml布局文件顯示 edittext:請注意android:singleline是錯誤的,因此,用戶將能夠輸入幾行文字。對于這一項目,fielddemo.java文件填充了一些散文輸入欄:package monsware.android.basic;import android.app.activity;import an

20、droid.os.bundle;import android.widget.edittext;public class fielddemo extends activity overridepublic void oncreate(bundle icicle) super.oncreate(icicle);setcontentview(r.layout.main);edittext fld=(edittext)findviewbyid(r.id.field);fld.settext(licensed under the apache license, version 2.0 +(the lic

21、ense); you may not use this file +except in compliance with the license. you may +obtain a copy of the license at +/licenses/license-2.0);結果,一旦建成并投入安裝成模擬器,如圖6-3所示。圖6-3 示例應用程序的fielddemo注意:android的模擬器只允許在每一個獨特的java包發(fā)射器中應發(fā)射用。由于本章中的所有演示共享monsware.android.basic包,您將只能在你的模擬器發(fā)射的任何時候的看到這些

22、演示之一。另一個字段的特色,提供自動完成,以幫助用戶在整個無文本輸入一個值。這是作為autocompletetextview部件在android中提供的并將在第8章討論。另一種復選框經(jīng)典的復選框有兩種狀態(tài):選中的和未選中的。在這兩種狀態(tài)之間點擊復選框切換來指示選擇(例如,“添加快遞到我的命令”)。在android中,還有一個checkbox控件,以滿足這種需要。它作為一個textview先驅,因此您可以像android使用textview性能:格式部件添加文字顏色。在java中,你可以調(diào)用: ischecked() to determine if the checkbox has been c

23、hecked setchecked() to force the checkbox into a checked or unchecked state toggle() to toggle the checkbox as if the user checked it此外,當復選框的狀態(tài)發(fā)生改變時,你可以注冊一個偵聽器(這種情況下,一個oncheckedchangelistener實例)來提醒。例如,從the basic/checkbox的項目,這里是一個簡單的復選框的布局:相應的checkboxdemo.java檢索和配置checkbox的行為:public class checkboxdem

24、o extends activityimplements compoundbutton.oncheckedchangelistener checkbox cb;overridepublic void oncreate(bundle icicle) super.oncreate(icicle);setcontentview(r.layout.main);cb=(checkbox)findviewbyid(r.id.check);cb.setoncheckedchangelistener(this);public void oncheckedchanged(compoundbutton butto

25、nview,boolean ischecked) if (ischecked) cb.settext(this checkbox is: checked);else cb.settext(this checkbox is: unchecked);請注意為復選框的狀態(tài)變化activity作為其自身監(jiān)聽器,因為它執(zhí)行oncheckedchangelistener 分界面(通過cb.setoncheckedchangelistener(this))?;卣{(diào)偵聽器在oncheckedchanged(),它接收復選框的狀態(tài)有什么新的變化和狀態(tài)。在這種情況下,我們更新了文本的復選框來反映復選框中包含的實際內(nèi)

26、容。結果呢?點擊復選框立即更新其內(nèi)容,你可以看圖6-4和6-5。 圖6-4 checkboxdemo示例應用程序,未選中的復選框 圖6-5 同樣的應用,選中的復選框打開收音機由于與其他單選按鈕在其他工具包執(zhí)行時,android的單選按鈕是兩種狀態(tài),如復選框,但可分為這樣,只有一組中的單選按鈕可以隨時選中。像復選框,radiobutton從compoundbutton中繼承,從而繼承了textview。因此,所有的標準textview的字體,樣式,顏色等特性,可用于控制單選按鈕的外觀。同樣,您可以在一個單選按鈕上調(diào)用ischecked(),看看它是否被選中,切換()來選擇它,等等,就像你可以用一

27、個復選框。大多數(shù)時候,你會想要在radiogroup里面放進一個radiobutton的小部件。該radiogroup表明其狀態(tài)的單選按鈕設置聯(lián)系在一起,這意味著只有一個按鈕退出組可以在任何時間選擇。如果您指派一個android:在您的xml布局中id到你的radiogroup,您可以訪問您的java代碼和調(diào)用組: check() to check a specific radio button via its id (e.g., group.check(r.id.radio1) clearcheck() to clear all radio buttons, so none in the g

28、roup are checked getcheckedradiobuttonid() to get the id of the currently-checked radio button (or -1 ifnone are checked)例如,the basic/ radiobutton的示例應用程序,這里是一個xml布局顯示一個radiogroup包裝的radiobutton的部件集合:圖6-6顯示了使用android生成的java項目和此布局的結果 圖6-6 示例應用程序的radoibuttondemo請注意,單選按鈕組最初設置為在一開始就完全暢通無阻。預先設定的一個單選按鈕被選中,在

29、radiobutton上使用setchecked()或從您的oncreate()在activity中回調(diào),在radiogroup上選中()。所有部件,包括以前的那些示例,擴展視圖,這樣使所有部件一系列有用的性能,并超越那些已經(jīng)介紹的方法。視圖中最有可能被使用的某些屬性,包括: controls the focus sequence: android:nextfocusdown android:nextfocusleft android:nextfocusright android:nextfocusup android:visibility, which controls whether th

30、e widget is initially visible android:background, which typically provides an rgb color value (e.g., #00ff00 for green)to serve as the background for the widget您可以切換是否通過setenabled()啟用了一個小裝置,看看它是否是通過isenabled()啟用的。這是禁用一個復選框或單選按鈕選擇一些小部件的常用方式。你可以通過requestfocus()給出一個部件重點,看看它是否是通過isfocused()為重點。你可能會如前所述用

31、禁用插件來使用,以確保一旦您禁用操作完成,正確的部件具有焦點。為了幫助操縱部件樹和容器,組成一個activity的整體視圖,你可以使用: getparent() to find the parent widget or container findviewbyid() to find a child widget with a certain id getrootview() to get the root of the tree (e.g., what you provided to the activity viasetcontentview()beginning androidmark

32、l. murphy writeusing xml-based layoutswhile it is technically possible to create and attach widgets to our activity purely through java code, the way we did in chapter 4, the more common approach is to use an xml-based layout file. dynamic instantiation of widgets is reserved for more complicated sc

33、enarios, where the widgets are not known at compile-time (e g., populating a column of radio buttons based on data retrieved off the internet). with that in mind, its time to break out the xml and learn how to lay out android activities that way. what is an xml-based layout? as the name suggests, an

34、 xml-based layout is a specification of widgets relationships to each otherand to their containers (more on this in chapter 7)encoded in xml format. specifically, android considers xml-based layouts to be resources, and as such layout files are stored in the res/layout directory inside your android

35、project. each xml file contains a tree of elements specifying a layout of widgets and their containers that make up one view hierarchy. the attributes of the xml elements are properties, describing how a widget should look or how a container should behave. for example, if a button element has an att

36、ribute value of android:textstyle = bold, that means that the text appearing on the face of the button should be rendered in a boldface font style. androids sdk ships with a tool (aapt) which uses the layouts. this tool should be automatically invoked by your android tool chain (e.g., eclipse, ants

37、build.xml). of particular importance to you as a developer is that aapt generates the r.java source file within your project, allowing you to access layouts and widgets within those layouts directly from your java code. why use xml-based layouts? most everything you do using xml layout files can be

38、achieved through java code. for example, you could use settypeface() to have a button render its text in bold, instead of using a property in an xml layout. since xml layouts are yet another file for you to keep track of, we need good reasons for using such files. perhaps the biggest reason is to as

39、sist in the creation of tools for view definition, such as a gui builder in an ide like eclipse or a dedicated android gui designer like droiddraw1. such gui builders could, in principle, generate java code instead of xml. the challenge is re-reading the ui definition to support editsthat is far sim

40、pler if the data is in a structured format like xml than in a programming language. moreover, keeping generated xml definitions separated from hand-written java code makes it less likely that somebodys custom-crafted source will get clobbered by accident when the generated bits get re-generated. xml

41、 forms a nice middle ground between something that is easy for tool-writers to use and easy for programmers to work with by hand as needed. also, xml as a gui definition format is becoming more commonplace. microsofts xaml2, adobes flex3, and mozillas xul4 all take a similar approach to that of andr

42、oid: put layout details in an xml file and put programming smarts in source files (e.g., javascript for xul). many less-well-known gui frameworks, such as zk5, also use xml for view definition. while “following the herd” is not necessarily the best policy, it does have the advantage of helping to ea

43、se the transition into android from any other xml-centered view description language. ok, so what does it look like? here is the button from the previous chapters sample application, converted into an xml layout file, found in the layouts/nowredux sample project. this code sample along with all othe

44、rs in this chapter can be found in the source code area of . the class name of the widgetbuttonforms the name of the xml element. since button is an android-supplied widget, we can just use the bare class name. if you create your own widgets as subclasses of android.view.view, you would need to prov

45、ide a full package declaration as well.the root element needs to declare the android xml namespace: xmlns:android= all other elements will be children of the root and will inherit that namespace declaration. because we want to reference this button from our java code, we need to give it an identifie

46、r via the android:id attribute. we will cover this concept in greater detail later in this chapter. the remaining attributes are properties of this button instance: android:text indicates the initial text to be displayed on the button face (in this case, an empty string) android:layout_width and and

47、roid:layout_height tell android to have the buttons width and height fill the “parent”, in this case the entire screenthese attributes will be covered in greater detail in chapter 7. since this single widget is the only content in our activity, we only need this single element. complex uis will requ

48、ire a whole tree of elements, representing the widgets and containers that control their positioning. all the remaining chapters of this book will use the xml layout form whenever practical, so there are dozens of other examples of more complex layouts for you to peruse from chapter 7 onward. whats

49、with the signs? many widgets and containers only need to appear in the xml layout file and do not need to be referenced in your java code. for example, a static label (textview) frequently only needs to be in the layout file to indicate where it should appear. these sorts of elements in the xml file

50、 do not need to have the android:id attribute to give them a name. anything you do want to use in your java source, though, needs an android:id. the convention is to use +id/. as the id value, where the . represents your locallyunique name for the widget in question. in the xml layout example in the preceding section, +id/button is the identifier for the button widget. android provides a few special android:id values, of the form android:id/. we will see some of these in various chapters of this book, such as chapters 8 and 10. we attach these to the java how?given that

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論