一 佈局文件的創建
1.點左面layout文件夾,單擊鼠標右鍵,選New—>layout resource file
第二種方式
點左面layout文件夾,單擊鼠標右鍵,選New—>xml-use-選layout xml file/values xml file
二 佈局文件類型
1.表格佈局(TableLayout)
以表格形式排列控件,通過行和列頁面分爲多個單元格,每個單元格都可以添加控件。
表格佈局需要和TableRow配合使用,每一行都由TableRow對象組成,因爲TableRow的數量決定表格的行數,列數由最多控件的TableRow決定。
佈局屬性 | 功能 |
---|---|
android:stretchColumns | 該列被拉伸 |
android:shrinkColumns | 該列被收縮 |
android:collapseColumns | 該列被隱藏 |
控件屬性 | 功能 |
---|---|
android:layout_column | 設置單元格顯示位置下標值從0開始的 |
android:layout_span | 設置單元格佔幾列,默認1列 |
例子:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:stretchColumns="2"
android:layout_height="match_parent">
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_column="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
</TableLayout>
效果:
2.線性佈局(LinearLayout)
特點:以水平或或垂直反向排列,水平排列時,從左到右;垂直排列時,從上到下,控件的方向用orientation
控制,默認用android:orientation="horizontal"
,是水平排列,vertical
垂直排列
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕1"
android:textSize="20sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕2"
android:textSize="20sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕3"
android:textSize="20sp"
/>
</LinearLayout>
效果圖
注意
這裏的layout_width的屬性值不能設置成match_parent(填充父窗體),其餘的控件會被擠出來,顯示不出來,layout_width設置成wrap_content(包裹內容)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕1"
android:textSize="20sp"
/>
<!--這裏的layout_width的屬性值不能設置成match_parent(填充父窗體),
其餘的控件會被擠出來,顯示不出來-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕2"
android:textSize="20sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕3"
android:textSize="20sp"
/>
</LinearLayout>
這樣的效果是
在上面三個按鈕的內個圖,可以發現按鈕並沒有把行填滿,在真實的情況下,並沒有這樣的,影響美觀。
這裏可以用layout_weight
設置權重,在button中使用屬性時,控件寬度不在由layout_width
決定,指定爲0dp就可以了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="按鈕1"
android:textSize="20sp"
android:layout_weight="1"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="按鈕2"
android:textSize="20sp"
android:layout_weight="2"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="按鈕3"
android:textSize="20sp"
android:layout_weight="1"
/>
</LinearLayout>
效果爲:
3.相對佈局(RelativeLayout)
相對佈局是通過相對定位的方式指定控件位置,即以其他控件或父容器爲參照物,來擺放控件位置。
控件屬性名稱 | 功能 |
---|---|
android:layout_centerInParent | 當前控件位於父佈局的中央位置 |
android:centerVertical | 當前控件位於父佈局垂直居中位置 |
android:layout_centerHorizontal | 當前控件位於父控件水平居中位置 |
android:layout_above | 當前控件位於某控件上方 |
---|---|
android:layout_below | 當前控件位於某控件下方 |
android:layout_toLeftOf | 當前控件位於某控件左側 |
android:layout_toRightOf | 當前控件位於某控件右側 |
android:layout_alignParentTop | 當前控件位於佈局頂端 |
android:layout_alignParentBottom | 當前控件位於佈局底端 |
android:layout_alignParentLeft | 當前控件位於佈局左側 |
android:layout_alignParentRight | 當前控件位於佈局右側 |
android:layout_alignTop | 當前控件上邊界與某控件上邊界對齊 |
android:layout_alignBottom | 當前控件下邊界與某控件下邊界對齊 |
android:layout_alignLeft | 當前控件左邊界與某控件左邊界對齊 |
android:layout_alignRight | 當前控件右邊界與某控件右邊界對齊 |
android:layout_marginTop | 當前控件上邊界與某控件的距離 |
android:layout_marginBottom | 當前控件下邊界與某控件的距離 |
android:layout_marginLeft | 當前控件左邊界與某控件的距離 |
android:layout_marginRight | 當前控件右邊界與某控件的距離 |
佈局控件屬性 | 功能描述 |
---|---|
android:paddingTop | 設置佈局頂部內邊距的距離 |
android:paddingBottom | 設置佈局底部內邊距的距離 |
android:paddingLeft | 設置佈局左邊內邊距的距離 |
android:paddingRight | 設置佈局右邊內邊距的距離 |
android:padding | 設置佈局四周內邊距的距離 |
例子
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20sp"
>
<!--
android:paddingBottom="20sp"父窗體距底邊距20sp
-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="按鈕1"
android:textSize="20dp"
android:layout_alignParentBottom="true"
/>
<!--
android:layout_alignParentBottom屬性共2個值,如果位true代表該控件的底部與其父控件的底部對齊
-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:text="按鈕2"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="200sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn3"
android:text="按鈕3"
android:textSize="20dp"
android:layout_toRightOf="@id/btn2"
android:layout_alignBottom="@id/btn2"
android:layout_marginBottom="100sp"
/>
<!--
@id/btn2代表引用id等於btn2的控件
-->
</RelativeLayout>
效果:
(4) 幀佈局(FrameLayout)
該佈局爲每個加入其中的控件創建一個空白區域,稱爲一幀,每個控件佔據一幀,採用幀佈局方式設計界面時,所有控件都默認顯示在屏幕左上角,並按照先後放入的順序重疊擺放,先放入的控件顯示在最底層,後放入的控件顯示在最頂層。幀佈局適用於圖層設計。
佈局屬性 | 功能 |
---|---|
android:foreground | 設置幀佈局容器的前景圖像 |
android:foregroundGravity | 設置前景圖像顯示位置 |
注意:android:foreground設置幀佈局容器的前景圖片,始終在所有控件之上
android:foregroundGravity設置前景圖像顯示位置
@mipmap/ic_launcher代表引入mipmap文件下的ic_launcher.png圖片
例子
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="@mipmap/ic_launcher"
android:foregroundGravity="left"
>
<Button
android:layout_height="400dp"
android:layout_width="400dp"
android:text="按鈕1"
android:textSize="20dp"
></Button>
<Button
android:layout_height="200dp"
android:layout_width="200dp"
android:text="按鈕1"
android:textSize="20dp"
></Button>
</FrameLayout>
效果
常用的就是這四種