編輯:Android 開發實例
Android 的版面檔讓我們能很方便地安排元件位置與屬性,程式端也能直接取得元件並賦予行為。但如果遇到複雜的介面設計,把所有內容都塞進同一個 XML,檔案會迅速膨脹成難以維護的巨物。
為此 Android 提供了 <include> 標籤——顧名思義就是「包含」,能把多個版面檔組裝在一起。用法非常簡單,以下用三個檔案示範。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include android:id="@+id/cell1" layout="@layout/includeA" />
<include android:id="@+id/cell2"
android:layout_width="fill_parent"
layout="@layout/includeB" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="https://schemas.android.com/apk/res/android"
android:text="雨楓技術教程網 www.fengfly.com!"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="https://schemas.android.com/apk/res/android"
android:text="FENGFLY.COM"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
經過 layoutP 的整合,layoutA 與 layoutB 就成了它的子元素。整個版面的結構變得清楚、可讀性提高,介面上的功能模塊也能被明確切分開來。
<include> 有個容易踩的規則:只能覆寫 layout_* 這類版面參數,其他屬性(如 android:id 以外的樣式)不會生效。因此若需要客製被包含區塊的樣式,正確做法是把屬性寫在子版面自己的根節點上。另外被 include 的版面根節點必須同時具備 layout_width 與 layout_height,否則會引發例外。與 include 相關的還有 <merge>(消除多餘的巢狀層級)與 <ViewStub>(按需延遲載入),三者搭配能明顯改善複雜頁面的效能。
Android 登入畫面實作教學:帳號驗證、錯誤次數與安全性
登入畫面常用來驗證使用者身份。設計時除了帳號與密碼欄位,也要考慮錯誤提示、嘗試次數限制與安全性。 介面設計 使用兩個輸入框分別接收使用者名稱與密碼,密碼欄位需設定為密碼輸
自定義 TextView 實現文字倒影效果(附走動時鐘示例)
倒影效果在鎖屏、壁紙、數字時鐘這類場景裡很常見。實現它的關鍵只有一個方法:View 的 getDrawingCache()——先把自身繪製結果取成位圖,再做翻轉和漸變處理
Android 接入新浪微博、騰訊微博與人人網的 OAuth 2.0 實作
許多 Android 應用會需要接入社交平台的分享、登入或發文能力。本文以新浪微博、騰訊微博與人人網為例,說明整合時最關鍵的授權流程,以及如何取得後續呼叫 API 所需的
用 ProGuard 混淆程式碼,避免 APK 輕易被反編譯
Android 開發者都得面對一個現實:APK 本質上就是個壓縮檔,反編譯工具隨手可得,自己熬夜寫出來的程式碼,別人幾分鐘就能看得七七八八。 Google 也意識到了這點