Kotlin?LinearLayout與RelativeLayout布局使用詳解
安卓的開發(fā)從布局開始。
安卓的界面編寫也是使用xml
進(jìn)行布局的,一般如果熟悉了html
界面的布局,那么很容易就能夠理解安卓有關(guān)的布局了,這里介紹兩個比較重要的布局方式:線性布局(LinearLayout)和相對布局(RelativeLayout)。
新建的功能布局,一般是一個界面對應(yīng)一個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>
有幾個屬性需要熟悉一下:
- wrap_content 為按照控件內(nèi)容的大小進(jìn)行調(diào)整
- layout_marginLeft 為控件左邊的偏移,其他的一次類推
- layout_gravity 可以用來進(jìn)行控件居中顯示
- layout_weight 控件在horizontal模式下占到的比率
相對布局RelativeLayout
相對布局 主要兩種相當(dāng)模式,一種是父控件,一種是相對兄弟控件。
布局代碼如下:
<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>
幾個重要的布局:
- layout_alignParentxxxx 相對于父類的情況
- layout_to 相對于兄弟的情況
項目在github的地址在這里。
小結(jié)
布局的方式比較多,但是這兩個種布局方式是最重要的,也可以這么說掌握了這兩種以后,其他的就可以依次類推,只要知道里面的屬性基本上就容易上手了。
到此這篇關(guān)于Kotlin LinearLayout與RelativeLayout布局使用詳解的文章就介紹到這了,更多相關(guān)Kotlin LinearLayout與RelativeLayout內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析android 流量監(jiān)測的實現(xiàn)原理
本篇文章是對android中流量監(jiān)測的實現(xiàn)原理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android模仿實現(xiàn)微博詳情頁滑動固定頂部欄的效果實例
這篇文章主要給大家介紹了關(guān)于利用Android模仿實現(xiàn)微博詳情頁滑動固定頂部欄效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11android實現(xiàn)常駐通知欄遇到的問題及解決辦法
這篇文章主要介紹了android實現(xiàn)常駐通知欄遇到的問題及解決辦法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Android 模擬新聞APP顯示界面滑動優(yōu)化實例代碼
所謂滑動優(yōu)化就是滑動時不加載圖片,停止才加載,第一次進(jìn)入時手動加載。下面通過本文給大家介紹android 模擬新聞app顯示界面滑動優(yōu)化實例代碼,需要的朋友可以參考下2017-03-03Android DynamicGrid實現(xiàn)拖曳交換位置功能
這篇文章主要為大家詳細(xì)介紹了Android DynamicGrid實現(xiàn)拖曳交換位置功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Android實現(xiàn)標(biāo)題上顯示隱藏進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)標(biāo)題上顯示隱藏進(jìn)度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android SQLite數(shù)據(jù)庫版本升級的管理實現(xiàn)
這篇文章主要介紹了Android SQLite數(shù)據(jù)庫版本升級的管理實現(xiàn)的相關(guān)資料,這里提供實現(xiàn)代碼幫助大家掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09