Android五種布局

來源:互聯網
上載者:User

標籤:

1. LinearLayout

LinearLayout是線性布局控制項,它包含的子控制項將以橫向或縱向排列。

註:布局之間可以嵌套使用。即布局內既可包含控制項,又可包含布局。

兩個全域屬性:

1. android:orientation --決定其子類控制項排布方式android:orientation="horizontal" --水平排布android:orientation="vertical" --垂直排布2. android:gravity --決定子類控制項位置android:gravity="bottom" --底部android:gravity="bottom|center_horizontal"--標籤可連用(註:豎線左右不能加空格,需要注意邏輯)--gravity幾個屬性值:center_horizontal  --水平置中center_vertical  --垂直置中center  --水平垂直都置中right  --子類控制項位於當前布局的右邊left  --子類控制項位於當前布局的左邊bottom  --底部
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

子控制項常用屬性:

1. android:layout_gravity= "bottom"  --控制項本身在當期父容器的位置2. android:layout_weight= "n"  --指本身控制項占當前父容器的一個比例(n為數字)
  • 1
  • 2

weight屬性範例程式碼:

<EditText     android:id="@+id/text_1"    android:layout_width="0dp" --注意此處為0dp(勿忘單位)    android:layout_weight="1" --weight    android:layout_height="wrap_content" /><Button    android:id="@+id/button1"    android:layout_width="0dp" --注意此處為0dp(勿忘單位)    android:layout_height="wrap_content"    android:layout_weight="1" --weight    android:text="send" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

注意:兩個控制項的layout_width屬性均為”0dp”,也說明有layout_weight屬性的時候,layout_width屬性不起作用。而此處指定為0是一種比較規範的寫法。

效果: 

註:系統會先把 LinearLayout下所有控制項指定的layout_weight 相加,得到一個總值,然後每個控制項所佔大小的比例就是用該控制項的layout_weight 值除以剛才算出的總值。

若代碼改為:

<EditText     android:id="@+id/text_1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_weight="1" /><Button    android:id="@+id/button1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="send" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

則效果如所示: 
 
這樣看起來更加舒服,而且在各種螢幕上的適配效果會更好。

注意: 
1. gravity是用於指定文字在控制項中的對齊;而layout_gravity是用於指定控制項在布局中的對齊。 
2. 當LinearLayout的排列方向是 horizontal時,只有垂直方向上的對齊才會生效。因為此時水平方向上的長度是不固定的,每添加一個控制項,水平方向上的長度都會改變,因而無法指定該方向上的對齊。同理,當 LinearLayout 的排列方向是vertical時,只有水平方向上的對齊才會生效。

2. RelativeLayout

RelativeLayout是相對布局控制項,它包含的子控制項將以控制項之間的相對位置或者子類控制項相對父類容器的位置的方式排列。 
子類控制項相對父類容器:

android:layout_margin="40dp" --子控制項距父容器的邊距android:layout_marginLeft="32dp" --子控制項距父容器的左邊距android:layout_marginTop="40dp" --子控制項距父容器的頂邊距android:layout_alignParentLeft="true" --子控制項相對當前父容器靠左邊android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true" --子控制項相對父容器水平置中android:layout_centerVertical="true"android:layout_centerInParent="true" --水平、垂直都置中
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

子類控制項相對子類控制項:

android:layout_below="@+id/bt1" --給定標籤的底部android:layout_above="@+id/bt1" --上面android:layout_toRightOf="@+id/bt1" --右邊android:layout_alignBaseline="@+id/bt1" --該控制項與給定控制項內容在一條線上android:layout_alignTop="@+id/bt1" --該控制項與給定控制項頂部對齊android:layout_alignBottom="@+id/bt1"android:layout_alignLeft="@+id/bt1" --該控制項與給定控制項左邊緣對齊android:layout_alignRight="@+id/bt1"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
3. FrameLayout

FrameLayout是幀布局中,所有子項目都不能被放置指定位置,預設放在布局左上方。且後面的子項目直接覆蓋在前面的子項目之上,可將前面的子項目部分或全部遮擋。 
: 
 

幾個屬性:

android:foreground="" --設定前景圖 ,在所有子視圖的前面android:foregroundGravity="" --設定前景圖位置android:layout_gravity="center" --設定置中android:background= "#999999" --設定背景android:keepScreenOn="true" --保持螢幕喚醒
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
4. TableLayout

TableLayout表格版面配置模型以行列的形式管理子控制項,每一行為一個TableRow的對象,當然也可以是一個View的對象。

: 

TableLayout全域屬性:

android:collapseColumns="n" --隱藏第 n 列android:stretchColumns= "m, n" --展開第 m,n 列android:shrinkColumns= "n" --壓縮第 n 列
  • 1
  • 2
  • 3

注意:索引從0開始,多個之間用逗號分隔( * 號表示所有列)

TableLayout局部屬性:

android:layout_column= "n" --該控制項佔據第 n 列(注意:從第0列開始算起)android:layout_span= "n" --該控制項佔用 n 列
  • 1
  • 2

註:TableRow 中的控制項不能指定寬度。

5. AbsoluteLayout

AbsoluteLayout是絕對布局,又可叫座標布局,可直接指定子項目的絕對位置(xy座標)。由於手機螢幕尺寸差別比較大,故絕對布局適應性差。因此該布局方式用的較少。瞭解即可。

Android五種布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.