1. 程式人生 > >Android第一天---開發中常用的幾種佈局

Android第一天---開發中常用的幾種佈局

第一種:LinearLayout:線性佈局

線性佈局是按照處置或者水平進行排布的,預設是水平

屬性:orientation:用來指定當前的線性佈局的排布方向。

wrap_content:包裹內容

match_parent:匹配父類

margin:外邊距

padding:內邊距

gravity:便是控制元件的內部內容針對控制元件本身的對其方式

layout_gravity:表示該控制元件自身在父類佈局中的對齊方式

注:如果線性佈局排布方式為水平,那麼layout_gravity在水平的方向上就不起作用,只能在垂直的方向上起作用

layout_weight:表示權重的概念,即該百分比的形式進行對齊方式

如果控制元件劃分的區域為wrap_content,那麼權重值越大,所佔比例就會越大

如果控制元件劃分的區域為match_parent,那麼權重值越大,所佔比例就會越小
      第二種:RelativeLayout:絕對佈局

按照空間之間的相對位置進行排布,存在一個參照物的概念。

屬性:centerInparent:位於父類的中部

alignParentRight:對齊父類的右方

alignParentLeft:對齊父類的左方

toRightOf:在某個控制元件的右方

toLeftOf:在某個控制元件的左方

alignBotton:底部對齊

alignTop:頂部對齊

alignRight:右對齊

alignLeft:左對齊

alignBaseLine:基準線對齊

注:針對相對佈局而言,一般都不會過多的給定很多相關聯的屬性,否則耦合性就大大的增加

第三種:TableLayout:表格佈局------>繼承LinearLayout

屬性:stretchColumns:拉伸某一列,讓佈局顯得不緊湊

shrinkColunms:回縮某一列,讓整體的內容都得以呈現

collapseColumns:隱藏某一列

TableLayout屬於行和列的形式的管理控制元件,每行為一個TableRow物件

TableLayout不會生成邊框

注:TableRow的寬和高可以不指定,系統會自動給定對應的寬和高

第四種:FrameLayout:幀佈局

幀佈局中的每一個元件都代表一個畫面,用該佈局可以實現動畫效果

1.預設按照左上角(0,0)開始排布

2.在幀佈局中定義的控制元件每一個都是以畫面的形式進行呈現

3.最開始定義的控制元件出現在最下方那個,最後定義的控制元件出現在最上面。

注:幀佈局可以使用在手機聯絡人的導航顯示字母的呈現

幀佈局使用在幀動畫

第五種:AbsolutiveLayout:絕對佈局

又叫座標佈局,可以直接指定子元素的絕對位置

特點:這種佈局簡單直接,直觀性強

使用中一般通過Layout_x和Layout_y來指定對應的控制元件存放的位置

缺點:不利於螢幕的適配

第六種:GridLayout:網格佈局

Android4.0以上的版本出現,可以實現合併行合併列的效果

屬性:layout_columnSpan:擴充套件列的數目

layout_rowSpan:擴充套件行的數目

layout_gravity:填充方式(fill)

columnCount:存在多少列

RowCount:存在多少行

在開發中以及在目前開發的介面上,這六大布局使用最多的是相對佈局,可以根據不同的螢幕大小來自動調整所在的相對位置,如果在有足夠多(能具體的指定控制元件的相對位置)或者相對簡單的介面,使用相對佈局能更好的適配.
下面舉個例子:使用相對佈局來完成一個類登入的介面
在這裡插入圖片描述

程式碼如下:

<?xml version="1.0" encoding="utf-8"?>



<TextView
    android:id="@+id/user_name_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/head_pic"
    android:layout_alignBottom="@id/user_name"
    android:layout_marginTop="15dp"
    android:layout_toLeftOf="@id/user_name"
    android:gravity="center_vertical"
    android:text="User Name:" />

<EditText
    android:id="@+id/user_name"
    android:layout_width="110dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/head_pic"
    android:layout_alignLeft="@id/head_pic"
    android:layout_centerHorizontal="true" />

<TextView
    android:id="@+id/password_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/user_name_text"
    android:layout_alignBottom="@id/password"
    android:layout_marginTop="15dp"
    android:layout_toLeftOf="@id/user_name"
    android:gravity="center_vertical"
    android:text="Password:" />

<EditText
    android:id="@+id/password"
    android:layout_width="110dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/user_name"
    android:layout_alignLeft="@id/head_pic"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/password_text"
    android:layout_alignLeft="@id/password_text"
    android:text="登入" />

<Button
    android:id="@+id/register"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/password_text"
    android:layout_centerHorizontal="true"
    android:layout_toRightOf="@id/login"
    android:text="註冊" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="15dp"
    android:text="copyright:不幾根不幾根之小母雞" />

佈局完!!!