淺析Android的佈局

Android兩大布局

線性佈局

常用屬性

  • orientation:佈局中的排列方式有horizon(水平方向),vertical(垂直方向)兩種排列方向。
  • gravity:控制組件所包含的元素對齊方式。
  • layout-gravity:控制組件在父容器裏面的元素對齊方式。
  • layout-height:佈局的高度。
  • layout-weigh:佈局的寬度。
  • ID:爲組件設置一個標誌位,以便於在java文件中通過findviewById找到此組件。
  • background:爲組件設置顏色及圖片。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    tools:context="com.example.zwzz.myapplication.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="點擊開始"
        android:id="@+id/bt_ks"
        android:layout_gravity="center"
        android:background="@color/colorAccent"/>
</LinearLayout>

weight(權重)

  • 這個屬性是用來按比例劃分區域的。
  android:layout_weight="1"

相對佈局

基本屬性

  • gravity:設置容器內組件對齊方式。
  • ignoreGravity:設置了該屬性爲true的屬性將不受gravity屬性的影響。

根據父容器定位

  • layout-alighParentLeft:左對齊。
  • layout-alighParentRight:右對齊。
  • layout-alighParentTop:頂部對齊。
  • layout-alighParentBotoom:底部對齊。
  • android:layout-centerHorizon:水平居中。
  • android:layout-centerVertical:垂直居中。
  • android:layout-centerParent:中間位置。

根據兄弟組件定位

  • layout-toRight:參考組件的右邊。
  • layout-toLeft:參考組件的左邊。
  • layout-above:參考組件的上方。
  • layout-below:參考組件的下方。
  • layout-alignTop::對齊參考組件的上邊界。
  • layout-alignBottom::對齊參考組件的下邊界。
  • layout-alignLeft::對齊參考組件的左邊界。
  • layout-alignRight::對齊參考組件的右邊界。

margin(偏移)

  • layout-margin:設置組件上下左右的偏移量。
  • layout-marginRight:設置組件離右邊的偏移量。
  • layout-marginLeft:設置組件離左邊的偏移量。
  • layout-marginTop:設置組件離上邊的偏移量。
    • layout-marginBotoom:設置組件離下方的偏移量。

padding(填充)

  • layout-padding:往內部元素的上下左右填充一定的邊距。
  • layout-paddingRight:往內部元素的右邊填充一定的邊距。
  • layout-paddingLeft:往內部元素的左邊填充一定的邊距。
  • layout-paddingTop:往內部元素的上面填充一定的邊距。
  • layout-paddingBotoom:往內部元素的下面填充一定的邊距。

    舉個簡單例子

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.example.zwzz.myapplication.MainActivity">

    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="點擊開始"
        android:id="@+id/bt_ks"
        android:background="@color/colorAccent"/>
    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="點擊結束"
        android:id="@+id/bt_js"
        android:background="@color/colorPrimary"
        android:layout_marginLeft="50dp"
        />
    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="還沒開始"
        android:id="@+id/bt_hmks"
        android:background="@color/colorPrimaryDark"
        />
    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content" 
         android:layout_toRightOf="@id/bt_hmks"
        android:layout_marginTop="50dp"
        android:text="還沒結束"
        android:background="@color/colorAccent"
        />
</RelativeLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章