Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android UI 定位技巧的運用與九宮格圖片的使用

編輯: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 下測試,其他解析度可能發生定位偏移。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved