有哪些布局類型?
Android系統中為我們提供的五大布局:LinearLayout(線性布局)、FrameLayout(單幀布局)、AbsoluteLayout(絕對布局)、TablelLayout(表格布局)、RelativeLayout(相對布局)。其中最常用的的是LinearLayout、TablelLayout和RelativeLayout。這些布局都可以嵌套使用。
LinearLayout(線性布局)
線性布局是按照水平或垂直的順序將子元素(可以是控制項或布局)依次按照順序排列,每一個元素都位於前面一個元素之後。線性布局分為兩種:水平方向和垂直方向的布局。分別通過屬性android:orientation=”vertical” 和 android:orientation=”horizontal”來設置。
案例代碼分析:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical|center_horizontal" tools:context="zzxb.me.layoutdemo.MainActivity"> <Button android:id="@+id/linearLO" android:text="@string/linear_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/tableLO" android:text="@string/table_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/frameLO" android:text="@string/frame_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/relativeLO" android:text="@string/relative_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/absLO" android:text="@string/abslayout_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
線性布局是用<LinearLayout>標籤標示,其中常用的屬性:
layout_width/layout_height:設置寬度和高度,其值有:wrap_content(適配內容大小),match_parent(適配父容器大小),此兩個屬性在各個控制項中為通用屬性
id:唯一標識該控制項值
orientation:設置該布局是水平布局(horizontal)還是縱向布局(vertical)
gravity:設置控制項的對齊方式,常用值:center_vertical(縱向居中)|center_horizontal(水平居中)
在<Button>標籤中,也同樣有id,layout_width以及lay_height屬性。同時,還有如下常用屬性:
text:設置按鈕文字,這裡有兩種方式,一種是直接硬編碼,即直接寫內容,例如:
android:text="按鈕"
第二種方式是非硬編碼方式,是通過配置strings.xml文件來配置,例如:
<resources> <string name="btnText">按鈕</string> </resources>
然後,通過:
android:text="@string/btnText"
引用。
頁面跳轉的方式:
Intent intent = new Intent(); intent.setClass(MainActivity.this,LinearActivity.class); startActivity(intent);
TableLayout(表格布局)
表格布局與常見的表格類似,以行、列的形式來管理放入其中的UI組件。表格布局使用<TableLayout>標籤定義,在表格布局中,可以添加多個<TableRow>標籤佔用一行。由於<TableRow>標籤也是容器,所以還可以在該標籤中添加其他組件,每添加一個組件,表格就會增加一列。在表格布局中,列可以被隱藏,也可以被設置為伸展的,從而填充可利用的屏幕空間,還可以設置為強制收縮,直到表格匹配屏幕大小。
TableLayout跟TableRow 是一組搭配應用的布局,TableLayout置底,TableRow在TableLayout的上方,而Button、TextView等控制項就在TableRow之上.TableLayout是一個應用錯雜的布局,最簡單的用法就僅僅是拖沓控制項做出個界面,但實際上,會經常在代碼里應用TableLayout,例如做出表格的結果。
重要的幾個屬性如下:
android:collapseColumns="1,3" 隱藏第二列和第4列的控制項 android:stretchColumns="0,2,4" 第一列和三列以及第五列的空白textview被拉伸 android:shrinkColumns="1,3" 第二列和第4列的控制項被收縮
案例代碼:
<TableLayout android:stretchColumns="0,2,4" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:hint="請輸入用戶名" android:textSize="15sp" android:layout_margin="6dp" android:background="@drawable/corner_round" android:drawableLeft="@mipmap/account" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:hint="請輸入密碼" android:layout_margin="6dp" android:textSize="15sp" android:inputType="textPassword" android:background="@drawable/corner_round" android:drawableLeft="@mipmap/passwowrd" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="登錄" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="註冊" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> </TableLayout>
FrameLayout(幀布局)
幀布局被設計成在一個屏幕區域顯示一個單一的項(single item)。通常FrameLayout顯示一個單一的子控制項,它支持的布局屬性不夠豐富,一般通過layout_gravity來設置子控制項的位置。
FrameLayout的子控制項被繪製在一個堆棧中,最近添加進來的子控制項在堆棧的頂部。
案例代碼:
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/movie" android:contentDescription="@string/movie_desc" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/button" android:contentDescription="@string/pause_desc" android:layout_gravity="center" /> </FrameLayout>
RelativeLayout(相對布局)
相對布局,子控制項的位置關係可以通過子控制項與父控制項、子控制項與子控制項來確定,子控制項之間位置可以重疊,後面的控制項會蓋在前面控制項之上,拓展性好,靈活方便,是使用最多的布局方式。
案例代碼:
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/et_uname" android:hint="請輸入用戶名" android:textSize="20sp" android:background="@drawable/corner_round" android:layout_alignParentTop="true" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_pwd" android:hint="請輸入密碼" android:inputType="textPassword" android:layout_marginTop="12dp" android:background="@drawable/corner_round" android:textSize="20sp" android:layout_below="@+id/et_uname" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn_login" android:text="登錄" android:layout_width="150dp" android:layout_height="wrap_content" /> <View android:id="@+id/v1" android:layout_toRightOf="@+id/btn_login" android:layout_width="50dp" android:layout_height="0dp" /> <Button android:id="@+id/btn_reg" android:layout_toRightOf="@+id/v1" android:text="註冊" android:layout_width="150dp" android:layout_height="wrap_content" /> </RelativeLayout>
相對布局使用<RelativeLayout>標籤,其常用屬性如下:
android:layout_toLeftOf=”@+id/name” 指定控制項的左邊
android:layout_toRightOf=”@+id/name” 指定控制項的右邊
android:layout_above=”@+id/name” 指定控制項的上邊
android:layout_below=”@+id/name” 指定控制項的下邊
ndroid:layout_alignLeft=”@+id/name” 與指定控制項左對齊
android:layout_alignRight=”@+id/name” 與指定控制項右對齊
android:layout_alignTop=”@+id/name” 與指定控制項頂部對齊
android:layout_alignBottom=”@+id/name” 與指定控制項底部對齊
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” 在父控制項中中部居中
AbsoluteLayout(絕對布局)
絕對布局,子控制項的位置以絕對的位置定位,子控制項之間可以重疊,相對於其他布局,缺少靈活性,在最新的android版本中已經不建議使用。
總結
在android布局控制中,最常用的是線性布局和相對布局,往往它們通常是配合使用,也就是嵌套使用。
關於layout_gravity與gravity的區別
從名字上可以看到,android:gravity是對元素本身說的,元素本身的文本顯示在什麼地方靠著換個屬性設置,不過不設置默認是在左側的。
android:layout_gravity是相對與它的父元素說的,說明元素顯示在父元素的什麼位置。
比如說button: android:layout_gravity 表示按鈕在界面上的位置。 android:gravity表示button上的字在button上的位置。
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/209128.html