一、限制布局
Android Studio中預設為限制布局,自己建立的話,在layout檔案下建立xml檔案,
布局方式使用元件周圍的控鍵進行布局,如下圖
二、線性布局
在layout檔案夾下建立xml檔案,
線性布局預設是從上往下的格式進行布局,可以設定
android:orientation="horizontal"
從左到右進行布局,一個線性布局裡可以嵌套多個線性布局。
三、相對布局
layout檔案夾下建立xml檔案
相對布局可以控制幾個元件之間的相對位置
<Button
android:id="@+id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"//水準居中
android:layout_centerVertical="true"//豎直居中
android:text="Center" />
<Button
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btn_center"//相對于center按鈕的左側
android:layout_alignBottom="@id/btn_center"//與center按鈕下底對齊
android:text="Left" />
<Button
android:id="@+id/btn_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/btn_center"//相對于center按鈕的上面
android:layout_alignLeft="@id/btn_center"//與center按鈕左側對齊
android:text="Top" />
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn_center"//相對于center按鈕的右側
android:layout_alignBottom="@id/btn_center"//與center按鈕下底對齊
android:text="Right" />
<Button
android:id="@+id/btn_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn_center"//相對于center按鈕下面
android:layout_alignLeft="@id/btn_center"//與center按鈕左側對齊
android:text="Bottom" />
四、表格布局
layout檔案夾下建立xml檔案
表格布局一般很少使用,一般表格行
TableRow
的形式進行布局,然後在行中放置控件,下面為一個兩行的表格布局
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:text="工資" />
<Button android:text="人數"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:text="工資" />
<Button android:text="人數"/>
<Button android:text="工資" />
<TextView android:text="你好啊"/>
</TableRow>
五、幀布局
layout檔案夾下建立xml檔案
幀布局的特點是控件之間可以覆寫,如下圖
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:background="#ff0000"/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#00ff00"/>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#0000ff"/>
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:background="#ff00ff"/>