- Android編程權(quán)威指南(第4版)
- (美)克莉絲汀·馬西卡諾 布賴(lài)恩·加德納 比爾·菲利普斯 克里斯·斯圖爾特
- 848字
- 2021-06-15 15:29:09
2.3 更新視圖層
了解了MVC設(shè)計(jì)模式后,現(xiàn)在來(lái)更新GeoQuiz應(yīng)用的視圖層,為其添加一個(gè)NEXT按鈕。
在Android的世界里,視圖對(duì)象通常由XML布局文件生成。GeoQuiz應(yīng)用唯一的布局定義在activity_main.xml文件中。布局定義文件需要更新的地方如圖2-5所示。(注意,為節(jié)約版面,不變的部件屬性就不再列出了。)

圖2-5 新增按鈕
應(yīng)用視圖層所需的改動(dòng)如下。
- 為T(mén)extView新增android:id屬性。TextView部件需要資源ID,以便在MainActivity代碼中為它設(shè)置要顯示的文字。借助android:gravity="center",在TextView視圖上居中顯示文字。
- 刪除TextView的android:text屬性定義。用戶(hù)點(diǎn)擊問(wèn)題時(shí),代碼會(huì)動(dòng)態(tài)設(shè)置問(wèn)題文本,而不再需要硬編碼地理知識(shí)問(wèn)題了。
指定顯示在TextView視圖上的默認(rèn)文字,這樣布局預(yù)覽時(shí)就可以看到。實(shí)現(xiàn)方式是為T(mén)extView設(shè)置tools:text屬性,使用@string/語(yǔ)法形式指向代表問(wèn)題的字符串資源。
此外,還要在布局根標(biāo)簽處添加tools命名空間,讓Android Studio知道tools:text屬性的意思。該命名空間的作用是,在開(kāi)發(fā)預(yù)覽時(shí)讓tools屬性覆蓋部件的其他屬性。而在設(shè)備上運(yùn)行時(shí),系統(tǒng)會(huì)忽略該tools屬性。當(dāng)然,你仍然可以使用android:text,然后在代碼運(yùn)行時(shí)修改顯示文字。但使用tools:text更好,因?yàn)橐豢幢阒付ǖ闹凳莾H供預(yù)覽的。
以根LinearLayout為父部件,新增一個(gè)Button部件。
回到activity_main.xml文件中,參照代碼清單2-2,完成XML文件的相應(yīng)修改。
代碼清單2-2 新增按鈕以及對(duì)文本視圖的調(diào)整(res/layout/activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" ... > <TextView android:id="@+id/question_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:padding="24dp"android:text="@string/question_text"tools:text="@string/question_australia" /> <LinearLayout ... > ... </LinearLayout> <Button android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next_button" /> </LinearLayout>
保存activity_quiz.xml文件。這時(shí),會(huì)看到一個(gè)錯(cuò)誤提示,說(shuō)缺少字符串資源。
回到res/values/strings.xml文件,如代碼清單2-3所示,重命名question_text,添加新按鈕所需的字符串資源定義。
代碼清單2-3 更新字符串資源定義(res/values/strings.xml)
<string name="app_name">GeoQuiz</string><string name="question_text">Canberra is the capital of Australia.</string><string name="question_australia">Canberra is the capital of Australia.</string> <string name="true_button">True</string> <string name="false_button">False</string> <string name="next_button">Next</string> ...
既然已打開(kāi)strings.xml文件,那就繼續(xù)添加其他地理知識(shí)問(wèn)題的字符串,結(jié)果如代碼清單2-4所示。
代碼清單2-4 新增問(wèn)題字符串(res/values/strings.xml)
<string name="question_australia">Canberra is the capital of Australia.</string>
<string name="question_oceans">The Pacific Ocean is larger than
the Atlantic Ocean.</string>
<string name="question_mideast">The Suez Canal connects the Red Sea
and the Indian Ocean.</string>
<string name="question_africa">The source of the Nile River is in Egypt.</string>
<string name="question_americas">The Amazon River is the longest river
in the Americas.</string>
<string name="question_asia">Lake Baikal is the world\'s oldest and deepest
freshwater lake.</string>
...
注意最后一個(gè)字符串定義中的\'。為表示符號(hào)',這里使用了轉(zhuǎn)義字符。在字符串資源定義中,也可使用其他常見(jiàn)的轉(zhuǎn)義字符,比如\n是指新行符。
保存修改過(guò)的文件,然后回到activity_quiz.xml文件中,在圖形布局工具里預(yù)覽修改后的布局文件。
至此,GeoQuiz應(yīng)用視圖層的更新就全部完成了。為讓GeoQuiz應(yīng)用運(yùn)行起來(lái),接下來(lái)要更新控制層的MainActivity類(lèi)。
- DevOps:軟件架構(gòu)師行動(dòng)指南
- Advanced Machine Learning with Python
- Instant Testing with CasperJS
- 數(shù)據(jù)庫(kù)系統(tǒng)教程(第2版)
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- Reactive Programming with Swift
- Hands-On RESTful Web Services with Go
- 利用Python進(jìn)行數(shù)據(jù)分析(原書(shū)第3版)
- Visual FoxPro程序設(shè)計(jì)
- SQL Server 2016數(shù)據(jù)庫(kù)應(yīng)用與開(kāi)發(fā)
- HTML5從入門(mén)到精通 (第2版)
- 好好學(xué)Java:從零基礎(chǔ)到項(xiàng)目實(shí)戰(zhàn)
- Solutions Architect's Handbook
- Emotional Intelligence for IT Professionals
- Android應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)(第2版)