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)我們往里面添加控件的時候,會默認(rèn)把他們放到這塊區(qū)域的左上角,而這種布局方式卻沒有任何的定位方式,所以它應(yīng)用的場景并不多;幀布局的大小由控件中最大的子控件決定,如果控件的大小一樣大的話,那么同一時刻就只能看到最上面的那個組件!后續(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 開機(jī)自啟動Service實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Android 開機(jī)自啟動Service實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Android中使用orc實(shí)現(xiàn)文字識別實(shí)例
這篇文章主要介紹了Android中使用orc實(shí)現(xiàn)文字識別實(shí)例,詳細(xì)的介紹了orc的簡介和用法,有興趣的可以了解一下2017-05-05android 開發(fā)教程之日歷項(xiàng)目實(shí)踐(二)
決定開始學(xué)習(xí) Android 平臺下的軟件開發(fā),以日歷作為實(shí)踐項(xiàng)目,進(jìn)行一周后,基本完成,有需要的朋友可以參考下2013-01-01Android錄音--AudioRecord、MediaRecorder的使用
本篇文章主要介紹了Android錄音--AudioRecord、MediaRecorder的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02如何在Android中實(shí)現(xiàn)漸顯按鈕的左右滑動效果
本篇文章是對在Android中實(shí)現(xiàn)漸顯按鈕的左右滑動效果進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android 倒計(jì)時控件 CountDownView的實(shí)例代碼詳解
這篇文章主要介紹了Android 倒計(jì)時控件 CountDownView的實(shí)例代碼,代碼簡單易懂,非常不錯,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Android帶進(jìn)度條的文件上傳示例(使用AsyncTask異步任務(wù))
這篇文章主要介紹了Android帶進(jìn)度條的文件上傳示例(使用AsyncTask異步任務(wù)),使用起來比較方便,將幾個方法實(shí)現(xiàn)就行,感興趣的小伙伴們可以參考一下。2016-11-11