編輯:Android 開發實例
本節主要可學習兩點:一是定位的設計,二是九宮格圖片的運用。
先來看定位的設計,重點在於 RelativeLayout 的相對定位屬性搭配 layout_margin 與 padding 來精準控制元件位置:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="horizontal"> <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical"> <ImageView android:layout_height="wrap_content" android:id="@+id/UserPic" android:layout_marginTop="10px" android:src="@drawable/ic_search_user_gray" android:layout_width="wrap_content"></ImageView> <TextView android:layout_height="wrap_content" android:id="@+id/UserName" android:padding="5px" android:layout_width="wrap_content" android:text="Jinmn"></TextView> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:paddingLeft="10px" android:layout_width="fill_parent"> <RelativeLayout android:layout_height="80px" android:background="@drawable/share_item_two_bg" android:paddingLeft="10px" android:id="@+id/ShareHollItem" android:layout_width="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Title" android:maxWidth="150dip" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="5px" android:layout_marginLeft="10px" android:textColor="#00ff00" android:id="@+id/ShareHollTitle"></TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This game is good!" android:layout_alignBottom="@+id/ShareHollTitle" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" android:layout_marginLeft="10px" android:padding="3px" android:id="@+id/ShareHollContent"></TextView> <TextView android:layout_width="wrap_content" android:layout_alignParentRight="true" android:layout_margin="3px" android:layout_alignParentTop="true" android:layout_height="wrap_content" android:text="12 minute" android:id="@+id/ShareHollTime"></TextView> <ImageView android:layout_height="wrap_content" android:id="@+id/CommentPic" android:layout_alignParentRight="true" android:layout_margin="5px" android:layout_alignParentBottom="true" android:src="@drawable/magic" android:padding="5px" android:layout_width="wrap_content"></ImageView> </RelativeLayout> </LinearLayout> </LinearLayout>
其中黑色標註的部分即為定位的關鍵,其餘則是搭配 layout_margin 與 padding 控制元件在配置中的顯示位置。
另一點是九宮格圖的設計(圖標設計涉及的技巧很多,這裡僅略作提示,更具體者可上官網查閱)。程式碼中淺藍色部分引用了一張九宮格圖片(android:background="@drawable/share_item_two_bg")。
所謂 .9.png,是相對於傳統 PNG 格式而言,在圖片四周多了一圈由一個像素點組成的邊沿,用來定義圖片的可擴展區與內容顯示區,這種格式的圖片在 Android 環境下具備自適應縮放的能力:
(1)允許開發人員定義可擴展區域——當需要延伸圖片以填滿比本身更大的區域時,可擴展區的內容會被延展;
(2)允許開發人員定義內容顯示區——用於顯示文字或其他內容。
以九宮格繪圖工具(draw9patch.bat)為例,它讓你能透過所見即所得的編輯器輕鬆建立 NinePatch 圖。左側與上方的黑線交叉處即可擴展區域,右側與下方的黑線交叉處即內容顯示區。
歸納來說,圖片左方與上方的黑線決定了向四周擴展時重複填補的區域,而下方與右方的黑線則定義了內容的顯示範圍(即背景上 UI 元件的位置)。若將下方與右方的黑線都去除,內容便可能被遮住,中間僅保留填充用的擴展區。
正因為有了這樣的內容填充機制,我們能將它應用於許多情境,例如遊戲的載入進度條——只給背景圖一條中間的填充內容顯示,便能省去大量控制中間圖片定位的時間。此專案僅在 320x480 下測試,其他解析度可能發生定位偏移。
Android 手機衛士(十一):自訂焦點 TextView 實作跑馬燈
跑馬燈效果原理在 Android 中,TextView 要出現跑馬燈效果,通常需要單行顯示、設定 ellipsize 為 marquee,並讓元件取得焦點。若焦點不定時被
Android 本地化:讓應用適配不同語言環境
為什麼需要本地化?Android 應用可能會在不同地區、不同語言環境的裝置上執行。為了讓使用者更容易理解,應用中的文字、數字格式與其他資源最好能依語言環境調整。建立區域資
Android MediaPlayer 多媒體播放實作指南
在 Android 開發中,音訊與影片播放是常見需求。Android 內建的 MediaPlayer 類別提供了一個直接可用的播放服務,可處理本機資源、檔案路徑或串流來源
Android 進度指示:用 Loading Spinner 告訴使用者「正在忙」
當 App 要跑一段時間的任務(例如讀取網路資料、解壓縮檔案)時,如果畫面完全沒有反應,使用者往往會以為程式當掉了。這時候就需要一個進度條來回饋狀態。 Android