Kotlin?LinearLayout與RelativeLayout布局使用詳解
安卓的開(kāi)發(fā)從布局開(kāi)始。
安卓的界面編寫(xiě)也是使用xml進(jìn)行布局的,一般如果熟悉了html界面的布局,那么很容易就能夠理解安卓有關(guān)的布局了,這里介紹兩個(gè)比較重要的布局方式:線性布局(LinearLayout)和相對(duì)布局(RelativeLayout)。
新建的功能布局,一般是一個(gè)界面對(duì)應(yīng)一個(gè)xml文件,main界面的xml在activity_main.xml 中。
線性布局LinearLayout
根據(jù)名字我們就很清楚,線性布局的意思了,相當(dāng)于html中的div層,兩種布局方向:
vertical 下的布局方式:

horizontal 下的布局方式:

vertical 布局代碼:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#00aaff" >
<Button
android:id = "@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<Button
android:id = "@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
</LinearLayout>
horizontal 下的布局代碼:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#A6A7AF" >
<Button
android:id = "@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Button" />
<Button
android:id = "@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Button" />
</LinearLayout>
有幾個(gè)屬性需要熟悉一下:
- wrap_content 為按照控件內(nèi)容的大小進(jìn)行調(diào)整
- layout_marginLeft 為控件左邊的偏移,其他的一次類(lèi)推
- layout_gravity 可以用來(lái)進(jìn)行控件居中顯示
- layout_weight 控件在horizontal模式下占到的比率
相對(duì)布局RelativeLayout
相對(duì)布局 主要兩種相當(dāng)模式,一種是父控件,一種是相對(duì)兄弟控件。

布局代碼如下:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#9C27B0" >
<Button
android:id = "@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="Button5" />
<Button
android:id = "@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button6" />
<Button
android:id = "@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="@+id/btn6"
android:text="Button7" />
</RelativeLayout>
幾個(gè)重要的布局:
- layout_alignParentxxxx 相對(duì)于父類(lèi)的情況
- layout_to 相對(duì)于兄弟的情況
項(xiàng)目在github的地址在這里。
小結(jié)
布局的方式比較多,但是這兩個(gè)種布局方式是最重要的,也可以這么說(shuō)掌握了這兩種以后,其他的就可以依次類(lèi)推,只要知道里面的屬性基本上就容易上手了。
到此這篇關(guān)于Kotlin LinearLayout與RelativeLayout布局使用詳解的文章就介紹到這了,更多相關(guān)Kotlin LinearLayout與RelativeLayout內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Kotlin-Coroutines中的async與await異步協(xié)程管理
- Kotlin FrameLayout與ViewPager2控件實(shí)現(xiàn)滾動(dòng)廣告欄方法
- kotlin協(xié)程之coroutineScope函數(shù)使用詳解
- Kotlin協(xié)程啟動(dòng)createCoroutine及創(chuàng)建startCoroutine原理
- Android結(jié)合kotlin使用coroutine的方法實(shí)例
- Kotlin學(xué)習(xí)教程之協(xié)程Coroutine
- kotlin中泛型中in和out的區(qū)別解析
相關(guān)文章
Android實(shí)用小技巧之利用Lifecycle寫(xiě)出更好維護(hù)的代碼
lifecycle是一個(gè)類(lèi),用于存儲(chǔ)有關(guān)組件(如Activity或Fragment)的生命周期狀態(tài)的信息,并允許其他對(duì)象觀察此狀態(tài),下面這篇文章主要給大家介紹了關(guān)于Android實(shí)用小技巧之利用Lifecycle寫(xiě)出更好維護(hù)的代碼的相關(guān)資料,需要的朋友可以參考下2022-05-05
利用Jetpack Compose實(shí)現(xiàn)經(jīng)典俄羅斯方塊游戲
你的童年是否有俄羅斯方塊呢,本文就來(lái)介紹如何通過(guò)Jetpack Compose實(shí)現(xiàn)一個(gè)俄羅斯方塊!感興趣的小伙伴快跟隨小編一起動(dòng)手嘗試一下吧2022-05-05
在Android開(kāi)發(fā)中替換資源圖片不起作用的解決方法
這篇文章主要介紹了在Android開(kāi)發(fā)中替換資源圖片不起作用的解決方法,需要的朋友可以參考下2014-07-07
Android開(kāi)發(fā)應(yīng)用中Broadcast Receiver組件詳解
本篇文章主要介紹了Android開(kāi)發(fā)應(yīng)用中Broadcast Receiver組件詳解,想要學(xué)習(xí)的同學(xué)可以了解一下。2016-11-11
Android中findViewById獲取控件返回為空問(wèn)題怎么解決
這篇文章主要介紹了Android中findViewById獲取控件返回為空問(wèn)題怎么解決的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
Android 如何使用SQLite保存數(shù)據(jù)
對(duì)于重復(fù)數(shù)據(jù)或結(jié)構(gòu)化數(shù)據(jù)(例如聯(lián)系信息),將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)是理想選擇,SQL 數(shù)據(jù)庫(kù)的主要原則之一是架構(gòu),即數(shù)據(jù)庫(kù)組織方式的正式聲明,本篇文章介紹在 Android 上使用 SQLite 數(shù)據(jù)庫(kù),感興趣的朋友一起看看吧2024-03-03

