Android studio六大基本布局詳解
Android中常用的布局方式有以下幾種:
線性布局LinearLayout
相對布局RelativeLayout
表格布局TableLayout
層布局FrameLayout
絕對布局AbsoluteLayout
網(wǎng)格布局GridLayout
用的相對較多的是線性布局和相對布局。接下來重點(diǎn)演示這兩種布局
其中,表格布局是線性布局的子類。網(wǎng)格布局是android 4.0后新增的布局。
(一)線性布局LinearLayout
線性布局中最重要的屬性:orientation
horizontal(水平布局)和vertical(垂直布局)兩種方式
屬性名
- orientation 布局方式,有horizontal(水平布局)和vertical(垂直布局)兩種方式
- id 組件名稱
- layout_width 該組件的寬度
- layout_height 該組件的高度
- layout_weight 權(quán)重
- layout_gravity 該組件(在父容器)中的對齊方式
- gravity 該組件所含子組件在其內(nèi)部的對齊方式
- background 設(shè)置背景圖片或填充顏色
效果圖

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/gray"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:text="權(quán)重1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:text="權(quán)重2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:text="權(quán)重3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:text="權(quán)重4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:text="權(quán)重5"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:background="@color/teal_200"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:text="第一個布局"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:background="@color/purple"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:text="第二個布局"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:background="@color/teal"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:text="第三個布局"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
(二)相對布局RelativeLayout
屬性:
- android:layout_marginTop=“25dip” //頂部距離
- android:gravity=“left” //空間布局位置
- android:layout_marginLeft="15dip //距離左邊距
相對于給定ID控件
- android:layout_above 將該控件的底部置于給定ID的控件之上;
- android:layout_below 將該控件的底部置于給定ID的控件之下;
- android:layout_toLeftOf 將該控件的右邊緣與給定ID的控件左邊緣對齊;
- android:layout_toRightOf 將該控件的左邊緣與給定ID的控件右邊緣對齊;
- android:layout_alignBaseline 將該控件的baseline與給定ID的baseline對齊;
- android:layout_alignTop 將該控件的頂部邊緣與給定ID的頂部邊緣對齊;
- android:layout_alignBottom 將該控件的底部邊緣與給定ID的底部邊緣對齊;
- android:layout_alignLeft 將該控件的左邊緣與給定ID的左邊緣對齊;
- android:layout_alignRight 將該控件的右邊緣與給定ID的右邊緣對齊;
相對于父組件
- android:layout_alignParentTop 如果為true,將該控件的頂部與其父控件的頂部對齊;
- android:layout_alignParentBottom 如果為true,將該控件的底部與其父控件的底部對齊;
- android:layout_alignParentLeft 如果為true,將該控件的左部與其父控件的左部對齊;
- android:layout_alignParentRight 如果為true,將該控件的右部與其父控件的右部對齊;
居中
- android:layout_centerHorizontal 如果為true,將該控件的置于水平居中;
- android:layout_centerVertical 如果為true,將該控件的置于垂直居中;
- android:layout_centerInParent 如果為true,將該控件的置于父控件的中央;
指定移動像素
- android:layout_marginTop 上偏移的值;
- android:layout_marginBottom 下偏移的值;
- android:layout_marginLeft 左偏移的值;
- android:layout_marginRight 右偏移的值;
效果圖

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/gray"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:gravity="center"
android:background="@color/teal"
android:text="text1"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<TextView
android:id="@+id/tv_two"
android:layout_alignParentBottom="true"
android:gravity="center"
android:background="@color/teal"
android:text="text2"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<TextView
android:layout_alignParentRight="true"
android:gravity="center"
android:background="@color/teal"
android:text="text3"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<TextView
android:layout_centerInParent="true"
android:gravity="center"
android:background="@color/teal"
android:text="text5"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<TextView
android:layout_above="@+id/tv_two"
android:layout_alignParentRight="true"
android:gravity="center"
android:background="@color/teal"
android:text="text4"
android:layout_width="50dp"
android:layout_height="50dp"
/>
</RelativeLayout>
(三)表格布局TableLayout
屬性
三個常用屬性
- android:collapseColumns:設(shè)置需要被隱藏的列的序號
- android:shrinkColumns:設(shè)置允許被收縮的列的列序號
- android:stretchColumns:設(shè)置運(yùn)行被拉伸的列的列序號
(四)幀布局FrameLayout
FrameLayout(幀布局)可以說是六大布局中最為簡單的一個布局,這個布局直接在屏幕上開辟出一塊空白的區(qū)域,當(dāng)我們往里面添加控件的時(shí)候,會默認(rèn)把他們放到這塊區(qū)域的左上角,而這種布局方式卻沒有任何的定位方式,所以它應(yīng)用的場景并不多;幀布局的大小由控件中最大的子控件決定,如果控件的大小一樣大的話,那么同一時(shí)刻就只能看到最上面的那個組件!后續(xù)添加的控件會覆蓋前一個!雖然默認(rèn)會將控件放置在左上角,但是我們也可以通過layout_gravity屬性,指定到其他的位置!
效果圖

xml布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/gray"
android:layout_height="match_parent">
<TextView
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="180dp"/>
<TextView
android:background="#ffff00"
android:layout_width="fill_parent"
android:layout_height="130dp"/>
<TextView
android:background="#ff00ff"
android:layout_width="fill_parent"
android:layout_height="100dp"/>
<TextView
android:background="#00ffff"
android:layout_width="fill_parent"
android:layout_height="50dp"/>
</FrameLayout>
(五)絕對布局AbsoluteLayout
屬性:
- 絕對布局又可以叫做坐標(biāo)布局,可以直接指定子元素的絕對位置(xy)
- 由于手機(jī)屏幕尺寸差別比較大使用絕對定位的適應(yīng)性會比較差,在屏幕的適配上有缺陷
常用屬性:
- android:foreground:*設(shè)置改幀布局容器的前景圖像
- android:foregroundGravity:設(shè)置前景圖像顯示的位置
- android:layout_x=”” 控制當(dāng)前子類控件的x位置
- android:layout_y=”” 控制當(dāng)前子類控件的y位置
效果圖

.xml布局
(六)網(wǎng)格布局GridLayout
和之前的TableLayout(表格布局) 有點(diǎn)類似,不過網(wǎng)格布局的好處是:
- 可以自己設(shè)置布局中組件的排列方式
- 可以自定義網(wǎng)格布局有多少行,多少列
- 可以直接設(shè)置組件位于某行某列
- 可以設(shè)置組件橫跨幾行或者幾列
效果圖

.xml布局:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/GridLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="4"
android:orientation="horizontal"
android:rowCount="6" >
<TextView
android:layout_columnSpan="4"
android:layout_gravity="fill"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#15CBE3"
android:text="0"
android:textSize="50sp" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="回退" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="清空" />
<Button android:text="1" />
<Button android:text="2" />
<Button android:text="3" />
<Button android:text="+" />
<Button android:text="4" />
<Button android:text="5" />
<Button android:text="6" />
<Button android:text="-" />
<Button android:text="7" />
<Button android:text="8" />
<Button android:text="9" />
<Button android:text="*" />
<Button android:text="0" />
<Button android:text="." />
<Button android:text="=" />
<Button android:text="/" />
</GridLayout>
<GridLayout android:layout_width=“fill_parent”:網(wǎng)格布局寬度為填滿屏幕
<GridLayout android:layout_height=“wrap_content”:網(wǎng)格布局高度為包裹內(nèi)容
<GridLayout android:columnCount=“4”:網(wǎng)格布局設(shè)置 4 列
<GridLayout android:rowCount=“6”:網(wǎng)格布局設(shè)置 6 行
<GridLayout android:layout_columnSpan=“2”:清空和回退橫跨兩列
<GridLayout android:orientation=“horizontal”:網(wǎng)格布局設(shè)置為水平布局
以上就是Android studio六大基本布局詳解的詳細(xì)內(nèi)容,更多關(guān)于Android studio基本布局的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android Studio升級3.6 Build窗口出現(xiàn)中文亂碼問題解決方法
這篇文章主要介紹了Android Studio升級3.6 Build窗口出現(xiàn)中文亂碼問題解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Textvie實(shí)現(xiàn)左邊圖片和換行文字左對齊的方法
下面小編就為大家分享一篇Textvie實(shí)現(xiàn)左邊圖片和換行文字左對齊的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android 單例模式實(shí)現(xiàn)可復(fù)用數(shù)據(jù)存儲的詳細(xì)過程
本文介紹了如何使用單例模式實(shí)現(xiàn)一個可復(fù)用的數(shù)據(jù)存儲類,該類可以存儲不同類型的數(shù)據(jù),并提供統(tǒng)一的接口來訪問這些數(shù)據(jù),通過雙重檢查鎖定機(jī)制,該類在多線程環(huán)境下是線程安全的,感興趣的朋友跟隨小編一起看看吧2025-02-02
Android Service中使用Toast無法正常顯示問題的解決方法
這篇文章主要介紹了Android Service中使用Toast無法正常顯示問題的解決方法,分析了Service中Toast無法正常顯示的原因與相關(guān)的解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
Android中獲取資源 id 及資源 id 的動態(tài)獲取
這篇文章主要介紹了 Android中獲取資源 id 及資源 id 的動態(tài)獲取的相關(guān)資料,需要的朋友可以參考下2017-01-01

