設定框架/元件大小

數值部分可以有以下選擇

  • match_parent 填滿父框架

  • wrap_content 自適應內容

  • 單位屬性如 px、dp、sp

範例

<RelativeLayout
    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">
</RelativeLayout>

背景顏色

  • 顏色使用 16 進位表示。
  • 開頭要帶 # 字號。
  • 由 6 個 16 進制數字組成。
  • 每 2 數字分成 1 組。
  • 這 3 組各代表紅綠藍( RGB )的分配。

黑:

android:background="#000000"

白:

android:background="#ffffff"

紅:

android:background="#ff0000"

綠:

android:background="#ff00ff"

藍:

android:background="#ffff00"

範例

<RelativeLayout
    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"

    android:background="#ffff00">
</RelativeLayout>

設定名稱

  • 名稱前面要加上前輟 @+id/,IDE才會知道要把名稱放進R.java中。
  • Xml文件中每個使用android:id=”@+id/xxx”的view都會被分配一個未用的資源id。
  • 可以用在程式中,藉由名稱取得元件的控制權。
  • 可以用在排版中,讓其他元件或框架藉由名稱,來做對齊。

範例

<RelativeLayout
    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"
    android:background="#ffff00"

    android:id="@+id/max_layout">
</RelativeLayout>