版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、<p> 中國地質(zhì)大學(xué)(北京)</p><p> 本科畢業(yè)設(shè)計外文資料翻譯</p><p> 院(系): 信息工程學(xué)院 </p><p> 專 業(yè): 計算機科學(xué)與技術(shù) </p><p> 姓 名:
2、 王文昊 </p><p> 學(xué) 號: 04108109 </p><p> 外文出處: Beginning Android </p><p> Mark Murphy. 2009.6 </p>
3、<p> 附 件: 1.外文資料翻譯譯文;2.外文原文。</p><p> 完成日期: 2012年 5月28日 </p><p><b> Android起航</b></p><p><b> 使用XML進行布局</b></p><p> 雖然純粹通
4、過Java代碼在activity上創(chuàng)建和添加部件,在技術(shù)上是可行的,我們在第4章中做的一樣,更常見的方法是使用一種基于XML的布局文件。動態(tài)的小部件實例保留更多,情況復(fù)雜,小工具在編譯時不為人所知。例如,在數(shù)據(jù)檢索了互聯(lián)網(wǎng)基礎(chǔ)上將單選按鈕填充柱。</p><p> 考慮到這一點,現(xiàn)在是時候來學(xué)習(xí)如何用此種方式來布置Android activities。</p><p> 1.1 什么
5、是基于XML的布局?</p><p> 正如其名稱所示,一個基于XML的布局是一個關(guān)系到每個規(guī)格的小部件,和他們的容器(更多關(guān)于此內(nèi)容的在第7章)編碼的XML格式。具體來說,Android認為基于XML的布局是資源,因此布局文件存儲在res /在你的Android項目布局目錄中。</p><p> 每個XML文件包含一個指定的部件和容器布局元素樹,一種意見認為構(gòu)成層次。對XML元素的屬
6、性,描述一個部件應(yīng)如何看或者一個容器應(yīng)如何運轉(zhuǎn)。例如,如果一個按鈕元素。</p><p> 有一個Android的屬性值:文字樣式=“bold”,這意味著該文本出現(xiàn)在按鈕的表面應(yīng)該是呈現(xiàn)一個粗體字體樣式.</p><p> Android的SDK中附帶一個使用的布局的工具(aapt)。這個工具應(yīng)自動調(diào)用你的Android工具鏈(例如,Eclipse中,Ant’s build.xml)。
7、作為一個開發(fā)人員,尤其重要的是,在您的項目中aapt生成R.java源文件,讓您能在那些布局中直接從Java代碼中獲取布局和部件。</p><p> 1.2 為什么基于XML的布局?</p><p> 使用XML布局文件做的大部分都可以通過Java代碼。例如,你可以使用setTypeface()命令一個按鈕使用粗體文本,而不是在一個XML布局中使用屬性。由于XML布局是為你跟蹤的另一
8、個文件,所以我們需要好的理由來使用這樣的文件。</p><p> 也許最大的原因是為了在視圖定義中協(xié)助工具的建立,如IDE中一個GUI創(chuàng)建者像Eclipse或者一個像DroidDraw1設(shè)計GUI圖形用戶界面建設(shè)者。這樣GUI建設(shè)者們,在原則上,生成Java代碼而不是XML。目前的挑戰(zhàn)是重新閱讀用戶界面的定義,以支持編輯,也就是說,如果是像XLM的結(jié)構(gòu)公式數(shù)據(jù)比一個程序語言中的數(shù)據(jù)簡單的多。此外,保持生成的XM
9、L定義從手寫的Java代碼中分離,使得某人定制的來源意外重新生成不太可能。</p><p> XML形成一個良好的中間立場,使工具作家使用更簡便,程序員需要時手工工作更簡易。</p><p> 此外,XML作為一個GUI定義格式是越來越普遍。微軟的XAML,Adobe的Flex,和Mozilla的XUL都采取Android類似的方法:把布局細節(jié)放在一個XML文件和把編程智慧資料放在源文
10、件(例如,XUL中的JavaScript)。許多不太知名的圖形用戶界面框架,如ZK,還使用視圖定義的XML。而“隨大流”并不一定是最好的政策,但他們有優(yōu)勢幫助從任何其他XML為中心的觀點描述語言輕松進入Android。</p><p> 好了,那么基于XML的布局是什么樣子的?下面是以前的章節(jié)的示例應(yīng)用程序按鈕,轉(zhuǎn)換成一個XML布局文件:</p><p> <?xml versi
11、on="1.0" encoding="utf-8"?></p><p> <Button xmlns:android="http://schemas.android.com/apk/res/android"</p><p> android:id="@+id/button"</p>
12、<p> android:text=""</p><p> android:layout_width="fill_parent"</p><p> android:layout_height="fill_parent"/></p><p> 部件,按鈕的類名稱形成XML元素的名稱。
13、因為按鈕是Android提供的部件,我們可以只使用裸類的名稱。如果您創(chuàng)建自己的部件作為android.view.View子小部件,您也將需要提供一個完整的包聲明(如com.commonsware.android.MyWidget)。</p><p> 因為我們要引用這個來自Java代碼的按鈕,我們需要通過android給它一個標(biāo)識符:id屬性。我們將在本章后面更詳細的介紹這個概念。</p><
14、;p> 其余的屬性是此按鈕實例屬性:</p><p> ?android:文字表示的初始文本將顯示在按鈕(這種情況顯示空字符串)</p><p> ?android:layout_width和Android:layout_height命令android有按鈕的</p><p> 寬度和高度填寫 “parent”,這種情況下,整個屏幕。將這些屬性將在第7
15、章中詳解。由于這個單一部件是activity的僅有內(nèi)容,我們只需要這一個因素。復(fù)雜的用戶界面將需要整個樹的元素,代表工具和容器,控制自己的定位。所有的這本書余下的章節(jié)將使用XML布局,所以還有數(shù)十種更復(fù)雜的其他布局實例,請前進到第七章仔細閱讀。</p><p> 1.3 @符號有什么用途?</p><p> 許多部件和容器只需要出現(xiàn)在XML布局文件,不須引用在Java代碼。例如,一個
16、靜態(tài)標(biāo)簽(TextView)只需要在布局文件中以表明它應(yīng)該出現(xiàn)在那里。在XML文件中各種元素文件不需要有android:id屬性給他們一個名稱。任何你想要在Java資源中使用的東西,都需要一個android:id 。</p><p> 該公約是使用@ +id...作為ID值,其中的...代表你locallyunique名稱有問題的部件。在上一節(jié)的XML布局的例子中,@ +id是按鈕控件的標(biāo)識符。</p&g
17、t;<p> android提供了一些特殊的android:ID值,形式@android:id/...我們將在這本書的不同章節(jié)中看到這些,例如第八章和第十章。</p><p> 我們將這些附加到Java…如何?</p><p> 既然你有意建立一個XML配置文件的工具和容器,名為main.xml存儲res/layout,所有你需要的是一個在您activity的OnCre
18、ate()回調(diào)以使用該版式:setContentView(R.layout.main);</p><p> 這是相同的setContentView(),我們前面使用,通過它的一個視圖子類的實例(在這種情況下,一個按鈕)。該android制造的觀點,來自我們的布局,是從訪問該代碼生成的R類。所有的布局都可以訪問R.layout,由基地鍵控布局文件的名稱- main.xml result in R.layout.m
19、ain.</p><p> 要訪問確定部件,使用findViewById(),在數(shù)字標(biāo)識符傳遞有問題的部件。這一數(shù)字標(biāo)識符生成的R類由android在R.id.something(其中一些是你正在尋找的具體部件)。這些部件是只是子類的視圖,就像我們在第四章中創(chuàng)建Button實例。</p><p> 剩下的部分在原始的Now演示中,按鈕的表面便會顯示當(dāng)前的時間,這將反映當(dāng)按鈕被最后按下時
20、顯示的時間(或者如果在按鈕尚未被按下時顯示)。這種邏輯仍然適用,即使在該修訂演示(NowRedux)中。盡管如此,在activity’s onCreate() callback中的實例,我們可以從XML的布局參考一個例子:</p><p> package com.commonsware.android.layouts;</p><p> import android.app.Acti
21、vity;</p><p> import android.os.Bundle;</p><p> import android.view.View;</p><p> import android.widget.Button;</p><p> import java.util.Date;</p><p>
22、 public class NowRedux extends Activity</p><p> implements View.OnClickListener {</p><p> Button btn;</p><p><b> @Override</b></p><p> public void onCre
23、ate(Bundle icicle) {</p><p> super.onCreate(icicle);</p><p> setContentView(R.layout.main);</p><p> btn=(Button)findViewById(R.id.button);</p><p> btn.setOnClickLis
24、tener(this);</p><p> updateTime();</p><p><b> }</b></p><p> public void onClick(View view) {</p><p> updateTime();</p><p><b> }</
25、b></p><p> private void updateTime() {</p><p> btn.setText(new Date().toString());}</p><p><b> }</b></p><p> 第一個區(qū)別是,在Java代碼中設(shè)置內(nèi)容視圖作為視圖,我們將它設(shè)置為引用的XML布
26、局(setContentView(R.layout.main))。該R.java源文件將被更新,當(dāng)我們重建這個項目,包括對我們布局參考文件(存儲在main.xml in our project’s res/layout directory )。</p><p> 另一個不同是,我們需要親手實驗按鈕實例,我們使用findViewById()調(diào)用。既然我們發(fā)現(xiàn)按鈕為@ +id/button,我們可以參考按鈕的標(biāo)識符
27、R.id.button?,F(xiàn)在,隨著手手頭上的實例,我們可以設(shè)置回調(diào)并根據(jù)需要設(shè)置標(biāo)簽。</p><p><b> 2 使用基本組件</b></p><p> 每一個GUI工具包都有一些基本的部件:字段,標(biāo)簽,按鈕等,Android的工具包在范圍內(nèi)沒有不同,其基本部件將提供一個良好的介紹,關(guān)于這些部件在Android activities中是如何運行的。</
28、p><p> 指派標(biāo)簽是最簡單的部件是標(biāo)簽,在Android提到的作為一個TextView。像大多數(shù)的GUI工具包,標(biāo)簽的文本是不可被用戶直接編輯的。通常情況下,它們被用來確定相鄰部件(例如,一個“姓名:”一個填充姓名前的標(biāo)簽)。</p><p> 在Java中,你可以通過創(chuàng)建一個TextView的實例l來創(chuàng)建一個標(biāo)簽。更常見的,雖然,</p><p> 你將通過
29、添加一個TextView元素到布局來在XML布局文件中創(chuàng)建標(biāo)簽,與一個Android:文本屬性來設(shè)置標(biāo)簽的本身價值。如果您需要交換基于某些標(biāo)準(zhǔn)的標(biāo)簽,例如國際化,你可能想使用XML中的資源參考代替,這些將在第9章敘述。例如,在Basic/Label項目中,你將找到下列布局文件:</p><p> <?xml version="1.0" encoding="utf-8"
30、;?></p><p> <TextView xmlns:android="http://schemas.android.com/apk/res/android"</p><p> android:layout_width="fill_parent"</p><p> android:layout_heigh
31、t="wrap_content"</p><p> android:text="You were expecting something profound?"</p><p> 只是單獨的布局,由android的項目生成器提供的Java源的(如activityCreator),生成應(yīng)用程序。</p><p> 2.1按鈕
32、,歸屬于誰?</p><p> 我們已經(jīng)在第4和第5章看到了按鈕部件用法。按鈕是文本視圖的一個子類,所以一切都在上一節(jié)討論了,按鈕格式所面臨的問題仍然成立。</p><p> Android有兩個部件,來幫助你將照片嵌入activities:ImageView和ImageButton。正如名稱所暗示的,他們是分別對于文本視圖和按鈕基于圖像的類似物。每個部件帶有一個android:src
33、屬性(在一個XML布局中),指明使用什么圖片。這些通常引用一個可繪制的資源,在講資源的這個章節(jié)中更詳細地描述了。您還可以通過setImageURI()從內(nèi)容提供商在Uri基礎(chǔ)上設(shè)置圖像。</p><p> ImageButton控件,一個ImageView子類,混合在標(biāo)準(zhǔn)按鈕行為中,應(yīng)對點擊和諸如此類的東西。例如,從Basic/ImageView樣本項目中看main.xml布局,這可以在http://apres
34、s.com以及所有其他代碼示例種找到。</p><p> <?xml version="1.0" encoding="utf-8"?></p><p> <ImageView xmlns:android="http://schemas.android.com/apk/res/android"</p>
35、;<p> android:id="@+id/icon"</p><p> android:layout_width="fill_parent"</p><p> android:layout_height="fill_parent"</p><p> android:adjustVi
36、ewBounds="true"</p><p> android:src="@drawable/molecule"</p><p><b> /></b></p><p> 2.2綠色字段或者其他色彩</p><p> 緊接著按鈕和標(biāo)簽,字段是大多數(shù)GUI工具包的第三
37、個“錨”。在Android中,他們通過EditText部件運行,它是標(biāo)簽的一個子類TextView。</p><p> 隨著標(biāo)準(zhǔn)TextView屬性(例如,Android:文本樣式),EditText有許多其他方面可以幫助你創(chuàng)建字段,包括:除了這些,你可以使用專門配置字段輸入方法,如android:僅數(shù)字輸入numeric,android:為籠罩密碼輸入密碼,還有Android:phoneNumber進入電話號
38、碼。如果你想創(chuàng)建自己的輸入法計劃(如郵政編碼,社會安全號碼),您需要創(chuàng)建自己的執(zhí)行情況InputMethod接口,然后通過android設(shè)定字段來使用:inputMethod。例如,從the Basic/Field項目,這里是一個XML布局文件顯示 EditText:</p><p> <?xml version="1.0" encoding="utf-8"?>
39、;</p><p> <EditText xmlns:android="http://schemas.android.com/apk/res/android"</p><p> android:id="@+id/field"</p><p> android:layout_width="fill_pare
40、nt"</p><p> android:layout_height="fill_parent"</p><p> android:singleLine="false"</p><p><b> /></b></p><p> 請注意android:sing
41、leLine是錯誤的,因此,用戶將能夠輸入幾行文字。對于這一項目,F(xiàn)ieldDemo.java文件填充了一些散文輸入欄:</p><p> package com.commonsware.android.basic;</p><p> import android.app.Activity;</p><p> import android.os.Bundle;
42、</p><p> import android.widget.EditText;</p><p> public class FieldDemo extends Activity {</p><p><b> @Override</b></p><p> public void onCreate(Bundle
43、icicle) {</p><p> super.onCreate(icicle);</p><p> setContentView(R.layout.main);</p><p> EditText fld=(EditText)findViewById(R.id.field);</p><p> fld.setText("
44、Licensed under the Apache License, Version 2.0 " +</p><p> "(the \"License\"); you may not use this file " +</p><p> "except in compliance with the License. You m
45、ay " +</p><p> "obtain a copy of the License at " +</p><p> "http://www.apache.org/licenses/LICENSE-2.0");}</p><p><b> }</b></p><p
46、> 注意:Android的模擬器只允許在每一個獨特的Java包發(fā)射器中應(yīng)發(fā)射用。由于本章中的所有演示共享com.commonsware.android.basic包,您將只能在你的模擬器發(fā)射的任何時候的看到這些演示之一。</p><p> 另一個字段的特色,提供自動完成,以幫助用戶在整個無文本輸入一個值。這是作為AutoCompleteTextView部件在Android中提供的并將在第8章討論。<
47、;/p><p> 經(jīng)典的復(fù)選框有兩種狀態(tài):選中的和未選中的。在這兩種狀態(tài)之間點擊復(fù)選框切換來</p><p> 指示選擇(例如,“添加快遞到我的命令”)。在Android中,還有一個CheckBox控件,以滿足這種需要。它作為一個TextView先驅(qū),因此您可以像Android使用TextView性能:格式部件添加文字顏色。</p><p> 此外,當(dāng)復(fù)選框的狀態(tài)
48、發(fā)生改變時,你可以注冊一個偵聽器(這種情況下,一個OnCheckedChangeListener實例)來提醒。例如,從the Basic/CheckBox的項目,這里是一個簡單的復(fù)選框的布局:</p><p> <?xml version="1.0" encoding="utf-8"?></p><p> <CheckBox x
49、mlns:android="http://schemas.android.com/apk/res/android"</p><p> android:id="@+id/check"</p><p> android:layout_width="wrap_content"</p><p> androi
50、d:layout_height="wrap_content"</p><p> android:text="This checkbox is: unchecked" /></p><p> 相應(yīng)的CheckBoxDemo.java檢索和配置checkbox的行為:</p><p> public class Che
51、ckBoxDemo extends Activity</p><p> implements CompoundButton.OnCheckedChangeListener {</p><p> CheckBox cb;</p><p><b> @Override</b></p><p> public void
52、 onCreate(Bundle icicle) {</p><p> super.onCreate(icicle);</p><p> setContentView(R.layout.main);</p><p> cb=(CheckBox)findViewById(R.id.check);</p><p> cb.setOnChe
53、ckedChangeListener(this);</p><p><b> }</b></p><p> public void onCheckedChanged(CompoundButton buttonView,</p><p> boolean isChecked) {</p><p> if (isCh
54、ecked) {</p><p> cb.setText("This checkbox is: checked");</p><p><b> }</b></p><p><b> else {</b></p><p> cb.setText("This che
55、ckbox is: unchecked");}</p><p><b> }</b></p><p> 請注意為復(fù)選框的狀態(tài)變化activity作為其自身監(jiān)聽器,因為它執(zhí)行OnCheckedChangeListener 分界面(通過cb.setOnCheckedChangeListener(this))。回調(diào)偵聽器在onCheckedChanged(),
56、它接收復(fù)選框的狀態(tài)有什么新的變化和狀態(tài)。在這種情況下,我們更新了文本的復(fù)選框來反映復(fù)選框中包含的實際內(nèi)容。</p><p> Beginning Android </p><p> Using XML-Based Layouts</p><p> While it is technically possible to create and attach wid
57、gets 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 scenarios, where
58、the widgets are not known at compile-time (e g., populating a column of radio buttons based on data retrieved off the Internet). </p><p> With that in mind, it’s time to break out the XML and learn how to l
59、ay out Android activities that way. </p><p> 1.1What Is an XML-Based Layout?</p><p> As the name suggests, an XML-based layout is a specification of widgets’ relationships to each other—and to
60、 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
61、project. </p><p> 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
62、 how a widget should look or how a container should behave. For example, if a Button element has an attribute value of android:textStyle = "bold", that means that the text appearing on the face of the button s
63、hould be rendered in a boldface font style. </p><p> Android’s SDK ships with a tool (aapt) which uses the layouts. This tool should be automatically invoked by your Android tool chain (e.g., Eclipse, Ant’s
64、 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.
65、</p><p> 1.2Why Use XML-Based Layouts?</p><p> Most everything you do using XML layout files can be achieved through Java code. For example, you could use setTypeface() to have a button render
66、 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. </p><p> Perhaps the biggest reas
67、on is to assist 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 in
68、stead of XML. The challenge is re-reading the UI definition to support edits—that is far simpler if the data is in a structured format like XML than in a programming language. Moreover, keeping generated XML definitions
69、separated from hand-written Java cod</p><p> Also, XML as a GUI definition format is becoming more commonplace. Microsoft’s XAML2, Adobe’s Flex3, and Mozilla’s XUL4 all take a similar approach to that of An
70、droid: 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 her
71、d” is not necessarily the best policy, it does have the advantage of helping to ease the transition into Android from a</p><p> layout file, found in the Layouts/NowRedux sample project. This code sample al
72、ong with all others in this chapter can be found in the Source Code area of http://apress.com. </p><p> <?xml version="1.0" encoding="utf-8"?> </p><p> <Button xml
73、ns:android="http://schemas.android.com/apk/res/android" </p><p> android:id="@+id/button" </p><p> android:text="" </p><p> android:layout_width=&qu
74、ot;fill_parent" </p><p> android:layout_height="fill_parent"/> </p><p> The class name of the widget—Button—forms the name of the XML element. Since Button is an Android-supp
75、lied 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 provide a full package declaration as well.</p><p> The root element ne
76、eds to declare the Android XML namespace: </p><p> xmlns:android="http://schemas.android.com/apk/res/android" </p><p> All other elements will be children of the root and will inheri
77、t that namespace declaration. </p><p> Because we want to reference this button from our Java code, we need to give it an identifier via the android:id attribute. We will cover this concept in greater deta
78、il later in this chapter. </p><p> The remaining attributes are properties of this Button instance: </p><p> ? android:text indicates the initial text to be displayed on the button face (in t
79、his case, an empty string) </p><p> ? android:layout_width and android:layout_height tell Android to have the button’s </p><p> width and height fill the “parent”, in this case the entire scr
80、een—these attributes will be covered in greater detail in Chapter 7. </p><p> Since this single widget is the only content in our activity, we only need this single element. Complex UIs will require a whole
81、 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
82、 more complex layouts for you to peruse from Chapter 7 onward.</p><p> 1.3 What’s with the @ Signs?</p><p> Many widgets and containers only need to appear in the XML layout file and do not ne
83、ed 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 do not need to have
84、 the android:id attribute to give them a name. </p><p> Anything you do want to use in your Java source, though, needs an android:id. </p><p> The convention is to use @+id/... as the id value
85、, 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. </p><p> Android provi
86、des 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. </p><p> We Attach These to the Java How?</p>
87、<p> Given that you have painstakingly set up the widgets and containers in an XML layout file named main.xml stored in res/layout, all you need is one statement in your activity’s onCreate() callback to use that l
88、ayout: </p><p> setContentView(R.layout.main); </p><p> This is the same setContentView() we used earlier, passing it an instance of a View subclass (in that case, a Button). The Android-built
89、 view, constructed from our layout, is accessed from that code-generated R class. All of the layouts are accessible under R.layout, keyed by the base name of the layout file—main.xml results in R.layout.main. </p>
90、<p> To access our identified widgets, use findViewById(), passing in the numeric identifier of the widget in question. That numeric identifier was generated by Android in the R class as R.id.something (where some
91、thing is the specific widget you are seeking). Those widgets are simply subclasses of View, just like the Button instance we created in Chapter 4. </p><p> The Rest of the Story In the original Now demo, th
92、e button’s face would show the current time, which would reflect when the button was last pushed (or when the activity was first shown, if the button had not yet been pushed). Most of that logic still works, even in this
93、 revised demo (NowRedux). However, rather than instantiating the Button in our activity’s onCreate() callback, we can reference the one from the XML layout: </p><p> package com.commonsware.android.layouts;
94、 </p><p> import android.app.Activity; </p><p> import android.os.Bundle; </p><p> import android.view.View; </p><p> import android.widget.Button; import java.util
95、.Date; </p><p> public class NowRedux extends Activity implements View.OnClickListener { Button btn; </p><p> @Override </p><p> public void onCreate(Bundle icicle) { super.onC
96、reate(icicle); </p><p> setContentView(R.layout.main); </p><p> btn=(Button)findViewById(R.id.button);btn.setOnClickListener(this);updateTime(); </p><p><b> } </b><
97、;/p><p> public void onClick(View view) { updateTime(); </p><p><b> } </b></p><p> private void updateTime() { </p><p> btn.setText(new Date().toString()
98、); } </p><p><b> } </b></p><p> The first difference is that rather than setting the content view to be a view we created in Java code, we set it to reference the XML layout (setCo
99、ntentView(R.layout.main)). The R.java source file will be updated when we rebuild this project to include a reference to our layout file (stored as main.xml in our project’s res/layout directory). </p><p>
100、The other difference is that we need to get our hands on our Button instance, for which we use the findViewById() call. Since we identified our button as @+id/button, we can reference the button’s identifier as R.id.butt
101、on. Now, with the Button instance in hand, we can set the callback and set the label as needed.</p><p> Employing Basic Widgets</p><p> Every GUI toolkit has some basic widgets: fields, labels
102、, buttons, etc. Android’s toolkit is no different in scope, and the basic widgets will provide a good introduction as to how widgets work in Android activities. </p><p> The simplest widget is the label, re
103、ferred to in Android as a TextView. Like in most GUI toolkits, labels are bits of text not editable directly by users. Typically, they are used to identify adjacent widgets (e.g., a “Name:” label before a field where one
104、 fills in a name). </p><p> In Java, you can create a label by creating a TextView instance. More commonly, though, you will create labels in XML layout files by adding a TextView element to the layout, wit
105、h an android:text property to set the value of the label itself. If you need to swap labels based on certain criteria, such as internationalization, you may wish to use a resource reference in the XML instead, as will be
106、 described in Chapter 9. TextView has numerous other properties of relevance for labels, For example, </p><p> <?xml version="1.0" encoding="utf-8"?> </p><p> <Tex
107、tView xmlns:android=http://schemas.android.com/apk/res/android</p><p> android:layout_width="fill_parent" </p><p> android:layout_height="wrap_content" </p><p>
108、; android:text="You were expecting something profound?" /></p><p> 2.1 Button, Who’s Got the Button?</p><p> We’ve already seen the use of the Button widget in Chapters 4 and 5.
109、As it turns out, Button is a subclass of TextView, so everything discussed in the preceding section in terms of formatting the face of the button still holds. </p><p> Android has two widgets to help you em
110、bed images in your activities: ImageView and ImageButton. As the names suggest, they are image-based analogues to TextView and Button, respectively. Each widget takes an android:src attribute (in an XML layout) to specif
111、y what picture to use. These usually reference a drawable resource, described in greater detail in the chapter on resources. You can also set the image content based on a Uri from a content provider via setImageURI(). &l
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- android起航畢業(yè)論文外文翻譯
- android系統(tǒng)外文翻譯
- android系統(tǒng)外文翻譯3
- android應(yīng)用架構(gòu)外文翻譯
- android手機外文翻譯---應(yīng)用程序基礎(chǔ)android developers
- android畢業(yè)設(shè)計外文資料翻譯
- 實現(xiàn)android手機音樂應(yīng)用-外文翻譯
- 外文翻譯---應(yīng)用程序基礎(chǔ)android developers
- android外文翻譯--外文翻譯--在安卓平臺的擊鍵動力學(xué)
- android外文翻譯--外文翻譯--在安卓平臺的擊鍵動力學(xué)
- android外文翻譯--外文翻譯--在安卓平臺的擊鍵動力學(xué)
- [雙語翻譯]--安卓外文翻譯--android與ios安全之戰(zhàn)
- 起航
- 安卓 英文 外文 文獻翻譯 android api級別
- 外文翻譯_mobile healthcare research based on jade agent and android platform
- android畢業(yè)論文--基于android短信平臺的管理與開發(fā)(含外文翻譯)
- 實現(xiàn)android手機音樂應(yīng)用-畢業(yè)論文外文翻譯
- android游戲畢業(yè)設(shè)計開題報告和外文翻譯
- 2014年--安卓外文翻譯--android與ios安全之戰(zhàn)
- [雙語翻譯]安卓外文翻譯--android應(yīng)用程序的安全性
評論
0/150
提交評論