2023年全國(guó)碩士研究生考試考研英語一試題真題(含答案詳解+作文范文)_第1頁
已閱讀1頁,還剩34頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、<p><b>  Android起航</b></p><p><b>  譯</b></p><p><b>  使用XML進(jìn)行布局</b></p><p>  雖然純粹通過Java代碼在activity上創(chuàng)建和添加部件,在技術(shù)上是可行的,我們?cè)诘?章中做的一樣,更常見的方法是使用一種基于

2、XML的布局文件。動(dòng)態(tài)的小部件實(shí)例保留更多,情況復(fù)雜,小工具在編譯時(shí)不為人所知(例如,在數(shù)據(jù)檢索了互聯(lián)網(wǎng)基礎(chǔ)上將單選按鈕填充柱。</p><p>  考慮到這一點(diǎn),現(xiàn)在是時(shí)候打破XML來學(xué)習(xí)如何用此種方式來布置Android activities。</p><p>  什么是基于XML的布局?</p><p>  正如其名稱所示,一個(gè)基于XML的布局是一個(gè)關(guān)系到每個(gè)

3、規(guī)格的小部件,和他們的容器(更多關(guān)于此內(nèi)容的在第7章)編碼的XML格式。具體來說,Android認(rèn)為基于XML的布局是資源,因此布局文件存儲(chǔ)在res /在你的Android項(xiàng)目布局目錄中。</p><p>  每個(gè)XML文件包含一個(gè)指定的部件和容器布局元素樹,一種意見認(rèn)為構(gòu)成層次。對(duì)XML元素的屬性,描述一個(gè)部件應(yīng)如何看或者一個(gè)容器應(yīng)如何運(yùn)轉(zhuǎn)。例如,如果一個(gè)按鈕元素。</p><p>  

4、有一個(gè)Android的屬性值:文字樣式=“bold”,這意味著該文本出現(xiàn)在按鈕的表面應(yīng)該是呈現(xiàn)一個(gè)粗體字體樣式.</p><p>  Android的SDK中附帶一個(gè)使用的布局的工具(aapt)。這個(gè)工具應(yīng)自動(dòng)調(diào)用你的Android工具鏈(例如,Eclipse中,Ant’s build.xml)。作為一個(gè)開發(fā)人員,尤其重要的是,在您的項(xiàng)目中aapt生成R.java源文件,讓您能在那些布局中直接從Java代碼中獲取

5、布局和部件。</p><p>  為什么使用基于XML的布局?</p><p>  使用XML布局文件做的大部分都可以通過Java代碼。例如,你可以使用setTypeface()命令一個(gè)按鈕使用粗體文本,而不是在一個(gè)XML布局中使用屬性。由于XML布局是為你跟蹤的另一個(gè)文件,所以我們需要好的理由來使用這樣的文件。</p><p>  也許最大的原因是為了在視圖定義中

6、協(xié)助工具的建立,如IDE中一個(gè)GUI創(chuàng)建者像Eclipse或者一個(gè)像DroidDraw1設(shè)計(jì)GUI圖形用戶界面建設(shè)者。這樣GUI建設(shè)者們,在原則上,生成Java代碼而不是XML。目前的挑戰(zhàn)是重新閱讀用戶界面的定義,以支持編輯,也就是說,</p><p>  如果是像XLM的結(jié)構(gòu)公式數(shù)據(jù)比一個(gè)程序語言中的數(shù)據(jù)簡(jiǎn)單的多。此外,保持生成的XML定義從手寫的Java代碼中分離,使得某人定制的來源意外重新生成不太可能。&l

7、t;/p><p>  XML形成一個(gè)良好的中間立場(chǎng),使工具作家使用更簡(jiǎn)便,程序員需要時(shí)手工工作更簡(jiǎn)易。</p><p>  此外,XML作為一個(gè)GUI定義格式是越來越普遍。微軟的XAML,Adobe的Flex,和Mozilla的XUL都采取Android類似的方法:把布局細(xì)節(jié)放在一個(gè)XML文件和把編程智慧資料放在源文件(例如,XUL中的JavaScript)。許多不太知名的圖形用戶界面框架,如

8、ZK,還使用視圖定義的XML。而“隨大流”并不一定是最好的政策,但他們有優(yōu)勢(shì)幫助從任何其他XML為中心的觀點(diǎn)描述語言輕松進(jìn)入Android。</p><p>  好了,那么基于XML的布局是什么樣子的?</p><p>  下面是以前的章節(jié)的示例應(yīng)用程序按鈕,轉(zhuǎn)換成一個(gè)XML布局文件,布局/ NowRedux示例項(xiàng)目,在這一章中可以找到源代碼的http://apress.com領(lǐng)域。<

9、;/p><p>  <?xml version="1.0" encoding="utf-8"?></p><p>  <Button xmlns:android="http://schemas.android.com/apk/res/android"</p><p>  android:id=

10、"@+id/button"</p><p>  android:text=""</p><p>  android:layout_width="fill_parent"</p><p>  android:layout_height="fill_parent"/></p>

11、;<p>  部件,按鈕的類名稱形成XML元素的名稱。因?yàn)榘粹o是Android提供的部件,我們可以只使用裸類的名稱。如果您創(chuàng)建自己的部件作為android.view.View子小部件,您也將需要提供一個(gè)完整的包聲明(如com.commonsware.android.MyWidget)。</p><p>  根元素需要Android的XML命名空間聲明:</p><p>  x

12、mlns:android="http://schemas.android.com/apk/res/android"</p><p>  所有其他要素將成為子根并繼承該命名空間的聲明。</p><p>  因?yàn)槲覀円眠@個(gè)來自Java代碼的按鈕,我們需要通過android給它一個(gè)標(biāo)識(shí)符:id屬性。我們將在本章后面更詳細(xì)的介紹這個(gè)概念。</p><p&

13、gt;  其余的屬性是此按鈕實(shí)例屬性:</p><p>  ?android:文字表示的初始文本將顯示在按鈕(這種情況顯示空字符串)</p><p>  ?android:layout_width和Android:layout_height命令android有按鈕的</p><p>  寬度和高度填寫 “parent”,這種情況下,整個(gè)屏幕。將這些屬性將在第7章中詳

14、解。</p><p>  由于這個(gè)單一部件是activity的僅有內(nèi)容,我們只需要這一個(gè)因素。復(fù)雜的用戶界面將需要整個(gè)樹的元素,代表工具和容器,控制自己的定位。所有的這本書余下的章節(jié)將使用XML布局,所以還有數(shù)十種更復(fù)雜的其他布局實(shí)例,請(qǐng)前進(jìn)到第七章仔細(xì)閱讀。</p><p><b>  @符號(hào)有什么用途?</b></p><p>  許多部件

15、和容器只需要出現(xiàn)在XML布局文件,不須引用在Java代碼。例如,一個(gè)靜態(tài)標(biāo)簽(TextView)只需要在布局文件中以表明它應(yīng)該出現(xiàn)在那里。在XML文件中各種元素文件不需要有android:id屬性給他們一個(gè)名稱。</p><p>  任何你想要在Java資源中使用的東西,都需要一個(gè)android:id.</p><p>  該公約是使用@ +id...作為ID值,其中的...代表你loca

16、llyunique名稱有問題的部件。在上一節(jié)的XML布局的例子中,@ +id是按鈕控件的標(biāo)識(shí)符。</p><p>  android提供了一些特殊的android:ID值,形式@android:id/...我們將在這本書的不同章節(jié)中看到這些,例如第八章和第十章。</p><p>  我們將這些附加到Java…如何?</p><p>  既然你有意建立一個(gè)XML配置文

17、件的工具和容器,名為main.xml存儲(chǔ)res/layout,所有你需要的是一個(gè)在您activity的OnCreate()回調(diào)以使用該版式:</p><p>  setContentView(R.layout.main);</p><p>  這是相同的setContentView(),我們前面使用,通過它的一個(gè)視圖子類的實(shí)例(在這種情況下,一個(gè)按鈕)。該android制造的觀點(diǎn),來自我們

18、的布局,是從訪問該代碼生成的R類。所有的布局都可以訪問R.layout,由基地鍵控布局文件的名稱- main.xml result in R.layout.main.</p><p>  要訪問確定部件,使用findViewById(),在數(shù)字標(biāo)識(shí)符傳遞有問題的部件。這一數(shù)字標(biāo)識(shí)符生成的R類由android在R.id.something(其中一些是你正在尋找的具體部件)。這些部件是只是子類的視圖,就像我們?cè)诘谒恼?/p>

19、中創(chuàng)建Button實(shí)例。</p><p><b>  剩下的部分</b></p><p>  在原始的Now演示中,按鈕的表面便會(huì)顯示當(dāng)前的時(shí)間,這將反映當(dāng)按鈕被最后按下時(shí)顯示的時(shí)間(或者如果在按鈕尚未被按下時(shí)顯示)。</p><p>  這種邏輯仍然適用,即使在該修訂演示(NowRedux)中。盡管如此,在activity’s onCreat

20、e() callback中的實(shí)例,我們可以從XML的布局參考一個(gè)例子:</p><p>  package com.commonsware.android.layouts;</p><p>  import android.app.Activity;</p><p>  import android.os.Bundle;</p><p>  

21、import android.view.View;</p><p>  import android.widget.Button;</p><p>  import java.util.Date;</p><p>  public class NowRedux extends Activity</p><p>  implements Vie

22、w.OnClickListener {</p><p>  Button btn;</p><p><b>  @Override</b></p><p>  public void onCreate(Bundle icicle) {</p><p>  super.onCreate(icicle);</p>

23、<p>  setContentView(R.layout.main);</p><p>  btn=(Button)findViewById(R.id.button);</p><p>  btn.setOnClickListener(this);</p><p>  updateTime();</p><p><b&g

24、t;  }</b></p><p>  public void onClick(View view) {</p><p>  updateTime();</p><p><b>  }</b></p><p>  private void updateTime() {</p><p>

25、  btn.setText(new Date().toString());</p><p><b>  }</b></p><p><b>  }</b></p><p>  第一個(gè)區(qū)別是,在Java代碼中設(shè)置內(nèi)容視圖作為視圖,我們將它設(shè)置為引用的XML布局(setContentView(R.layout.main))。該

26、R.java源文件將被更新,當(dāng)我們重建這個(gè)項(xiàng)目,包括對(duì)我們布局參考文件(存儲(chǔ)在main.xml in our project’s res/layout directory )。</p><p>  另一個(gè)不同是,我們需要親手實(shí)驗(yàn)按鈕實(shí)例,我們使用findViewById()調(diào)用。既然我們發(fā)現(xiàn)按鈕為@ +id/button,我們可以參考按鈕的標(biāo)識(shí)符R.id.button?,F(xiàn)在,隨著手手頭上的</p>

27、<p>  實(shí)例,我們可以設(shè)置回調(diào)并根據(jù)需要設(shè)置標(biāo)簽。</p><p>  正如你可以看到如圖5-1,結(jié)果看起來與原始的Now演示一樣。</p><p>  圖5-1 樣本的NowRedux活動(dòng)</p><p><b>  使用基本部件</b></p><p>  每一個(gè)GUI工具包都有一些基本的部件:字段,

28、標(biāo)簽,按鈕等,Android的工具包在范圍內(nèi)沒有不同,其基本部件將提供一個(gè)良好的介紹,關(guān)于這些部件在Android activities中是如何運(yùn)行的。</p><p><b>  指派標(biāo)簽</b></p><p>  最簡(jiǎn)單的部件是標(biāo)簽,在Android提到的作為一個(gè)TextView。像大多數(shù)的GUI工具包,標(biāo)簽的文本是不可被用戶直接編輯的。通常情況下,它們被用來確

29、定相鄰部件(例如,一個(gè)“姓名:”一個(gè)填充姓名前的標(biāo)簽)。</p><p>  在Java中,你可以通過創(chuàng)建一個(gè)TextView的實(shí)例l來創(chuàng)建一個(gè)標(biāo)簽。更常見的,雖然,</p><p>  你將通過添加一個(gè)TextView元素到布局來在XML布局文件中創(chuàng)建標(biāo)簽,與一個(gè)Android:文本屬性來設(shè)置標(biāo)簽的本身價(jià)值。如果您需要交換基于某些標(biāo)準(zhǔn)的標(biāo)簽,例如國(guó)際化,你可能想使用XML中的資源參考代替

30、,這些將在第9章敘述。 TextView有許多相關(guān)的其他標(biāo)簽屬性,如:</p><p>  ? android:typeface to set the typeface to use for the label (e.g., monospace)</p><p>  ? android:textStyle to indicate that the typeface should be ma

31、de bold (bold), italic (italic),</p><p>  or bold and italic (bold_italic)</p><p>  ? android:textColor to set the color of the label’s text, in RGB hex format (e.g., #FF0000</p><p>

32、;<b>  for red)</b></p><p>  例如,在Basic/Label項(xiàng)目中,你將找到下列布局文件:</p><p>  <?xml version="1.0" encoding="utf-8"?></p><p>  <TextView xmlns:android=

33、"http://schemas.android.com/apk/res/android"</p><p>  android:layout_width="fill_parent"</p><p>  android:layout_height="wrap_content"</p><p>  android

34、:text="You were expecting something profound?"</p><p><b>  /></b></p><p>  正如你看到的圖6-1,</p><p>  圖6-1 示例應(yīng)用程序的LabelDemo</p><p>  只是單獨(dú)的布局,由androi

35、d的項(xiàng)目生成器提供的Java源的(如activityCreator),生成應(yīng)用程序。</p><p><b>  按鈕,歸屬于誰?</b></p><p>  我們已經(jīng)在第4和第5章看到了按鈕部件用法。按鈕是文本視圖的一個(gè)子類,所以一切都在上一節(jié)討論了,按鈕格式所面臨的問題仍然成立。</p><p><b>  短暫的圖像</b

36、></p><p>  Android有兩個(gè)部件,來幫助你將照片嵌入activities:ImageView和ImageButton。</p><p>  正如名稱所暗示的,他們是分別對(duì)于文本視圖和按鈕基于圖像的類似物。</p><p>  每個(gè)部件帶有一個(gè)android:src屬性(在一個(gè)XML布局中),指明使用什么圖片。這些通常引用一個(gè)可繪制的資源,在講資

37、源的這個(gè)章節(jié)中更詳細(xì)地描述了。您還可以通過setImageURI()從內(nèi)容提供商在Uri基礎(chǔ)上設(shè)置圖像。</p><p>  ImageButton控件,一個(gè)ImageView子類,混合在標(biāo)準(zhǔn)按鈕行為中,應(yīng)對(duì)點(diǎn)擊和諸如此類的東西。</p><p>  例如,從Basic/ImageView樣本項(xiàng)目中看main.xml布局,這可以在http://apress.com以及所有其他代碼示例種找到

38、。</p><p>  <?xml version="1.0" encoding="utf-8"?></p><p>  <ImageView xmlns:android="http://schemas.android.com/apk/res/android"</p><p>  andr

39、oid:id="@+id/icon"</p><p>  android:layout_width="fill_parent"</p><p>  android:layout_height="fill_parent"</p><p>  android:adjustViewBounds="tru

40、e"</p><p>  android:src="@drawable/molecule"</p><p><b>  /></b></p><p>  結(jié)果,只用代碼生成activity,如圖6-2所示。</p><p>  圖6-2 示例應(yīng)用程序的ImageViewDemo<

41、;/p><p>  綠色字段或者其他色彩</p><p>  緊接著按鈕和標(biāo)簽,字段是大多數(shù)GUI工具包的第三個(gè)“錨”。在Android中,他們通過EditText部件運(yùn)行,它是標(biāo)簽的一個(gè)子類TextView。</p><p>  隨著標(biāo)準(zhǔn)TextView屬性(例如,Android:文本樣式),EditText有許多其他方面可以幫助你創(chuàng)建字段,包括:</p>

42、<p>  ? android:autoText, to control if the field should provide automatic spelling assistance</p><p>  ? android:capitalize, to control if the field should automatically capitalize the first letter&l

43、t;/p><p>  of entered text (e.g., first name, city)</p><p>  ? android:digits, to configure the field to accept only certain digits</p><p>  ? android:singleLine, to control if the fie

44、ld is for single-line input or multiple-line input</p><p>  (e.g., does <Enter> move you to the next widget or add a newline?)</p><p>  除了這些,你可以使用專門配置字段輸入方法,如android:僅數(shù)字輸入numeric,android:為

45、籠罩密碼輸入密碼,還有Android:phoneNumber進(jìn)入電話號(hào)碼。如果你想創(chuàng)建自己的輸入</p><p>  法計(jì)劃(如郵政編碼,社會(huì)安全號(hào)碼),您需要?jiǎng)?chuàng)建自己的執(zhí)行情況InputMethod接口,然后通過android設(shè)定字段來使用:inputMethod。</p><p>  例如,從the Basic/Field項(xiàng)目,這里是一個(gè)XML布局文件顯示 EditText:</

46、p><p>  <?xml version="1.0" encoding="utf-8"?></p><p>  <EditText xmlns:android="http://schemas.android.com/apk/res/android"</p><p>  android:id=

47、"@+id/field"</p><p>  android:layout_width="fill_parent"</p><p>  android:layout_height="fill_parent"</p><p>  android:singleLine="false"<

48、/p><p><b>  /></b></p><p>  請(qǐng)注意android:singleLine是錯(cuò)誤的,因此,用戶將能夠輸入幾行文字。對(duì)于這一項(xiàng)目,F(xiàn)ieldDemo.java文件填充了一些散文輸入欄:</p><p>  package com.commonsware.android.basic;</p><p&

49、gt;  import android.app.Activity;</p><p>  import android.os.Bundle;</p><p>  import android.widget.EditText;</p><p>  public class FieldDemo extends Activity {</p><p>

50、<b>  @Override</b></p><p>  public void onCreate(Bundle icicle) {</p><p>  super.onCreate(icicle);</p><p>  setContentView(R.layout.main);</p><p>  EditText

51、 fld=(EditText)findViewById(R.id.field);</p><p>  fld.setText("Licensed under the Apache License, Version 2.0 " +</p><p>  "(the \"License\"); you may not use this file

52、" +</p><p>  "except in compliance with the License. You may " +</p><p>  "obtain a copy of the License at " +</p><p>  "http://www.apache.org/licenses

53、/LICENSE-2.0");</p><p><b>  }</b></p><p><b>  }</b></p><p>  結(jié)果,一旦建成并投入安裝成模擬器,如圖6-3所示。</p><p>  圖6-3 示例應(yīng)用程序的FieldDemo</p><p>

54、  注意:Android的模擬器只允許在每一個(gè)獨(dú)特的Java包發(fā)射器中應(yīng)發(fā)射用。由于本章中的所有演示共享com.commonsware.android.basic包,您將只能在你的模擬器發(fā)射的任何時(shí)候的看到這些演示之一。</p><p>  另一個(gè)字段的特色,提供自動(dòng)完成,以幫助用戶在整個(gè)無文本輸入一個(gè)值。這是作為AutoCompleteTextView部件在Android中提供的并將在第8章討論。</p&

55、gt;<p><b>  另一種復(fù)選框</b></p><p>  經(jīng)典的復(fù)選框有兩種狀態(tài):選中的和未選中的。在這兩種狀態(tài)之間點(diǎn)擊復(fù)選框切換來</p><p>  指示選擇(例如,“添加快遞到我的命令”)。</p><p>  在Android中,還有一個(gè)CheckBox控件,以滿足這種需要。它作為一個(gè)TextView先驅(qū),因此您

56、可以像Android使用TextView性能:格式部件添加文字顏色。</p><p>  在Java中,你可以調(diào)用:</p><p>  ? isChecked() to determine if the checkbox has been checked</p><p>  ? setChecked() to force the checkbox into a c

57、hecked or unchecked state</p><p>  ? toggle() to toggle the checkbox as if the user checked it</p><p>  此外,當(dāng)復(fù)選框的狀態(tài)發(fā)生改變時(shí),你可以注冊(cè)一個(gè)偵聽器(這種情況下,一個(gè)OnCheckedChangeListener實(shí)例)來提醒。</p><p>  例如

58、,從the Basic/CheckBox的項(xiàng)目,這里是一個(gè)簡(jiǎn)單的復(fù)選框的布局:</p><p>  <?xml version="1.0" encoding="utf-8"?></p><p>  <CheckBox xmlns:android="http://schemas.android.com/apk/res/and

59、roid"</p><p>  android:id="@+id/check"</p><p>  android:layout_width="wrap_content"</p><p>  android:layout_height="wrap_content"</p><p

60、>  android:text="This checkbox is: unchecked" /></p><p>  相應(yīng)的CheckBoxDemo.java檢索和配置checkbox的行為:</p><p>  public class CheckBoxDemo extends Activity</p><p>  implemen

61、ts CompoundButton.OnCheckedChangeListener {</p><p>  CheckBox cb;</p><p><b>  @Override</b></p><p>  public void onCreate(Bundle icicle) {</p><p>  super.on

62、Create(icicle);</p><p>  setContentView(R.layout.main);</p><p>  cb=(CheckBox)findViewById(R.id.check);</p><p>  cb.setOnCheckedChangeListener(this);</p><p><b>  

63、}</b></p><p>  public void onCheckedChanged(CompoundButton buttonView,</p><p>  boolean isChecked) {</p><p>  if (isChecked) {</p><p>  cb.setText("This chec

64、kbox is: checked");</p><p><b>  }</b></p><p><b>  else {</b></p><p>  cb.setText("This checkbox is: unchecked");</p><p><b>

65、  }</b></p><p><b>  }</b></p><p><b>  }</b></p><p>  請(qǐng)注意為復(fù)選框的狀態(tài)變化activity作為其自身監(jiān)聽器,因?yàn)樗鼒?zhí)行OnCheckedChangeListener 分界面(通過cb.setOnCheckedChangeListener(this

66、))?;卣{(diào)偵聽器在onCheckedChanged(),它接收復(fù)選框的狀態(tài)有什么新的變化和狀態(tài)。在這種情況下,我們更新了文本的復(fù)選框來反映復(fù)選框中包含的實(shí)際內(nèi)容。</p><p>  結(jié)果呢?點(diǎn)擊復(fù)選框立即更新其內(nèi)容,你可以看圖6-4和6-5。</p><p>  圖6-4 CheckBoxDemo示例應(yīng)用程序,未選中的復(fù)選框</p><p>  圖6-5 同樣

67、的應(yīng)用,選中的復(fù)選框</p><p><b>  打開收音機(jī)</b></p><p>  由于與其他單選按鈕在其他工具包執(zhí)行時(shí),Android的單選按鈕是兩種狀態(tài),如復(fù)選框,但可分為這樣,只有一組中的單選按鈕可以隨時(shí)選中。</p><p>  像復(fù)選框,RadioButton從CompoundButton中繼承,從而繼承了TextView。因此

68、,所有的標(biāo)準(zhǔn)TextView的字體,樣式,顏色等特性,可用于控制單選按鈕的外觀。同樣,您可以在一個(gè)單選按鈕上調(diào)用isChecked(),看看它是否被選中,切換()來選擇它,等等,就像你可以用一個(gè)復(fù)選框。</p><p>  大多數(shù)時(shí)候,你會(huì)想要在RadioGroup里面放進(jìn)一個(gè)RadioButton的小部件。該RadioGroup表明其狀態(tài)的單選按鈕設(shè)置聯(lián)系在一起,這意味著只有一個(gè)按鈕退出組可以在任何時(shí)間選擇。如

69、果您指派一個(gè)android:在您的XML布局中ID到你的RadioGroup,您可以訪問您的Java代碼和調(diào)用組:</p><p>  ? check() to check a specific radio button via its ID (e.g., group.check(R.id.radio1))</p><p>  ? clearCheck() to clear all rad

70、io buttons, so none in the group are checked</p><p>  ? getCheckedRadioButtonId() to get the ID of the currently-checked radio button (or -1 if</p><p>  none are checked)</p><p>  例

71、如,the Basic/ RadioButton的示例應(yīng)用程序,這里是一個(gè)XML布局顯示一個(gè)RadioGroup包裝的RadioButton的部件集合:</p><p>  <?xml version="1.0" encoding="utf-8"?></p><p>  <RadioGroup</p><p>

72、;  xmlns:android="http://schemas.android.com/apk/res/android"</p><p>  android:orientation="vertical"</p><p>  android:layout_width="fill_parent"</p><p&g

73、t;  android:layout_height="fill_parent"</p><p><b>  ></b></p><p>  <RadioButton android:id="@+id/radio1"</p><p>  android:layout_width="wr

74、ap_content"</p><p>  android:layout_height="wrap_content"</p><p>  android:text="Rock" /></p><p>  <RadioButton android:id="@+id/radio2"<

75、/p><p>  android:layout_width="wrap_content"</p><p>  android:layout_height="wrap_content"</p><p>  android:text="Scissors" /></p><p>  &l

76、t;RadioButton android:id="@+id/radio3"</p><p>  android:layout_width="wrap_content"</p><p>  android:layout_height="wrap_content"</p><p>  android:text

77、="Paper" /></p><p>  </RadioGroup></p><p>  圖6-6顯示了使用Android生成的Java項(xiàng)目和此布局的結(jié)果</p><p>  圖6-6 示例應(yīng)用程序的RadoiButtonDemo</p><p>  請(qǐng)注意,單選按鈕組最初設(shè)置為在一開始就完全暢通無阻。

78、預(yù)先設(shè)定的一個(gè)單選按鈕被選中,在RadioButton上使用setChecked()或從您的OnCreate()在activity中回調(diào),在RadioGroup上選中()。</p><p>  所有部件,包括以前的那些示例,擴(kuò)展視圖,這樣使所有部件一系列有用的性能,并超越那些已經(jīng)介紹的方法。</p><p>  視圖中最有可能被使用的某些屬性,包括:</p><p>

79、;  ? Controls the focus sequence:</p><p>  ? android:nextFocusDown</p><p>  ? android:nextFocusLeft</p><p>  ? android:nextFocusRight</p><p>  ? android:nextFocusUp<

80、/p><p>  ? android:visibility, which controls whether the widget is initially visible</p><p>  ? android:background, which typically provides an RGB color value (e.g., #00FF00 for green)</p>

81、<p>  to serve as the background for the widget</p><p>  您可以切換是否通過setEnabled()啟用了一個(gè)小裝置,看看它是否是通過isEnabled()啟用的。這是禁用一個(gè)復(fù)選框或單選按鈕選擇一些小部件的常用方式。</p><p>  你可以通過requestFocus()給出一個(gè)部件重點(diǎn),看看它是否是通過isFoc

82、used()為重點(diǎn)。</p><p>  你可能會(huì)如前所述用禁用插件來使用,以確保一旦您禁用操作完成,正確的部件具有焦點(diǎn)。</p><p>  為了幫助操縱部件樹和容器,組成一個(gè)activity的整體視圖,你可以使用:</p><p>  ? getParent() to find the parent widget or container</p>

83、<p>  ? findViewById() to find a child widget with a certain ID</p><p>  ? getRootView() to get the root of the tree (e.g., what you provided to the activity via</p><p>  setContentView())&

84、lt;/p><p>  Beginning Android</p><p>  Mark L. Murphy write</p><p>  Using XML-Based Layouts</p><p>  While it is technically possible to create and attach widgets to our

85、 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 the widgets

86、 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 lay out Andr

87、oid activities that way. </p><p>  What 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 their contai

88、ners (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 project. <

89、/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 how a widget

90、 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 should be rend

91、ered 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 build.xml).

92、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. </p>

93、<p>  Why 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 its text in b

94、old, 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 reason is to assis

95、t 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.

96、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 separated from

97、 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 Android: put lay

98、out 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 nece

99、ssarily the best policy, it does have the advantage of helping to ease the transition into Android from a</p><p>  OK, So What Does It Look Like? </p><p>  Here is the Button from the previous c

100、hapter’s sample application, converted into an XML </p><p>  layout file, found in the Layouts/NowRedux sample project. This code sample along with all others in this chapter can be found in the Source Code

101、area of http://apress.com. </p><p>  <?xml version="1.0" encoding="utf-8"?> </p><p>  <Button xmlns:android="http://schemas.android.com/apk/res/android"

102、android:id="@+id/button" </p><p>  android:text="" </p><p>  android:layout_width="fill_parent" </p><p>  android:layout_height="fill_parent"

103、/> </p><p>  The class name of the widget—Button—forms 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 subcl

104、asses of android.view.View, you would need to provide a full package declaration as well.</p><p>  The root element needs to declare the Android XML namespace: </p><p>  xmlns:android="http

105、://schemas.android.com/apk/res/android" </p><p>  All other elements will be children of the root and will inherit that namespace declaration. </p><p>  Because we want to reference this bu

106、tton from our Java code, we need to give it an identifier via the android:id attribute. We will cover this concept in greater detail later in this chapter. </p><p>  The remaining attributes are properties

107、of this Button instance: </p><p>  ? android:text indicates the initial text to be displayed on the button face (in this case, an empty string) </p><p>  ? android:layout_width and android:lay

108、out_height tell Android to have the button’s </p><p>  width and height fill the “parent”, in this case the entire screen—these attributes will be covered in greater detail in Chapter 7. </p><p>

109、;  Since this single widget is the only content in our activity, we only need this single element. Complex UIs will require a whole tree of elements, representing the widgets and containers that control their positioning

110、. 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. </p><p>  Wha

111、t’s with the @ Signs? </p><p>  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 on

112、ly 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 the android:id attribute to give them a name. </p><p>  Anything you do want

113、to use in your Java source, though, needs an android:id. </p><p>  The convention is to use @+id/... as the id value, where the ... represents your locallyunique name for the widget in question. In the XML l

114、ayout example in the preceding section, @+id/button is the identifier for the Button widget. </p><p>  Android provides a few special android:id values, of the form @android:id/.... We will see some of these

溫馨提示

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

評(píng)論

0/150

提交評(píng)論