轉:布局【ViewGroup】

來源:互聯網
上載者:User

標籤:

轉: http://www.cnblogs.com/leehyuan/p/3389527.html

像素單位的變化:是用dip,而不是px,主要用於寬高的設定

在Android中支援的描述大小地區的類型有以下幾種。

px(pixels)——像素:不同的裝置顯示效果相同,一般我們HVGA代表320x480像素,這個用的比較多。

dip(device independent pixels)——裝置獨立像素:這個和裝置硬體有關,一般哦我們為了支援WCGA、HVGA和QVGA推薦使用這個,不依賴於像素。等同於dp。

sp(scaled pixels—best for text size)——帶比例的像素。

pt(points)——磅:1pt = 1/72英寸

in(inches)——英寸

mm(millimeters)——毫米

字型大小單位的變化:字型大小的單位要使用sp而不是dip或者px

layout_xxxx和xxxx的區別:layout_xxxx相對父親,xxxx為自己的孩子或者自己 顏色分為:3位【rgb】、6位【更多顏色】、8位【前兩位為透明度,後6位為更多顏色】

靜態布局,在資源檔夾res/layout內部,使用xml檔案描述。

動態布局,使用java文法構建ViewGroup對象。

  • 每個View必須指定寬高,layout_width、layout_height
    • 寬高控制對象為:android.view.ViewGroup.LayoutParams
    • 寬高具備父寬和父高即,父容器有多高寬,自己就有多高寬。值為:fill_parent
      match_parent
      • 高寬具備覆蓋原則,上面的會覆蓋下面的。
  • 除了高寬還有附加了一個權重layout_weight,單位為小數或者整數
    • 權重layout_weight計算方案為所有相加然後除自己,就是自己的比例
    • 控制對象為:android.view.ViewGroup.LayoutParams
    • 如果按照比例設定方案,那麼高或者寬需要調整為wrap_content
    • 例如:三個線布局,1線20%,2線60%,3線20%
    • 一線80dp,二線自動成長,三線80dp

android:id="@+id/b1"定義唯一標識

android:padding=""寫在父容器裡面,控制子容器邊距
android:layout_margin=""寫在子容器裡面,控制自己與父容器的邊距

  1. 線形布局【android.widget.LinearLayout】
    1. 橫排列android:orientation="horizontal"
    2. 豎排列android:orientation="vertical"
  2. 幀布局【android.widget.FrameLayout】
    1. 從螢幕左上方開始,分層次,下面為最上層
    2. 子View可以使用android:layout_gravity來控制方位,
  3. 相對布局【android.widget.RelativeLayout】
    1. 大多數情況都是子View使用屬性對位
    2. android:layout_centerInParent="true"相對父親垂直水平置中
    3. android:layout_centerHorizontal="true"相對父親水平置中
    4. android:layout_centerVertical="true"相對父親垂直置中
    5. android:layout_above="@+id/b1"相對b1上方
    6. android:layout_below="@+id/b1"相對b1下方
    7. android:layout_toLeftOf="@+id/b1"相對b1左方
    8. android:layout_toRightOf="@+id/b1"相對b1右方
    9. android:layout_alignLeft="@+id/b1"相對b1左方對齊
    10. android:layout_alignTop="@+id/b1"相對b1上方對齊
    11. android:layout_alignRight="@+id/b1"相對b1右方對齊
    12. android:layout_alignBottom="@+id/b1"相對b1下方對齊
    13. android:layout_alignParentLeft="true"相對父親左方對齊
    14. android:layout_alignParentRight="true"相對父親右方對齊
    15. android:layout_alignParentTop="true"相對父親垂上方對齊
    16. android:layout_alignParentBottom="true"相對父親下方對齊
    17. 相對布局最好採取鏈表的感覺,一個一個的相對下去具有連續性。
    18. 例如
    19. b1相對父容器,b2相對b1,b3相對b2,b4相對b3
  4. 表格版面配置【android.widget.TableLayout】
    1. android:collapseColumns="0,1,2"隱藏1,2,3列
    2. android:shrinkColumns="0,1,2" 收縮1,2,3列
    3. android:stretchColumns="0,1,2"擴充1,2,3列
  5. 網格布局【android.widget.GridLayout】
    1. 父容器屬性
      1. android:rowCount="3"總行數
      2. android:columnCount="4"總列數
    2. 子View的屬性
      1. android:layout_gravity="fill"儲存格對齊以及填充方式
      2. android:layout_columnSpan="2"列合并2個
      3. android:layout_rowSpan="2"行合并2個
      4. android:layout_column="2"指定第二列
      5. android:layout_row="2"指定第二行
      6. GridLayout的不提供支援空間(weight)分配的原則,空間分配界定。在一般情況下,可以配置一個GridLayout多餘的空間分布在多個行或列之間的不相同的比例。
      7. Android4.0才支援GridLayout布局,如果需要以前的版本也支援,需要使用android-support-v7擴充包
  6. 絕對布局【android.widget.AbsoluteLayout】
    1. android:layout_x="198dp"座標x軸
    2. android:layout_y="162dp"座標y軸

轉:布局【ViewGroup】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.