Android線性布局與相對(duì)布局的實(shí)現(xiàn)
線性布局(LinearLayout)
| 名字 | 含義 |
|---|---|
| android:id | 設(shè)置一個(gè)id方便使用 |
| android:layout_width | 寬度 |
| android:layout_height | 高度 |
| android:background | 設(shè)置背景顏色 |
| android:layout_margin | 設(shè)置外邊距 |
| android:layout_padding | 設(shè)置內(nèi)邊距 |
| android:orientation | 設(shè)置方向(水平或者垂直) |
練習(xí)代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_1"
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="#000000"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="50dp"
android:paddingBottom="10dp">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0033"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:background="#0066FF"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#000000"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#FF0033"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#55AA99"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
實(shí)現(xiàn)效果

相對(duì)布局(RelativeLayout)
最常用屬性
| 名字 | 含義 |
|---|---|
| android:layout_toLeftOf | 在什么的左邊 |
| android:layout_toRightOf | 在什么的右邊 |
| android:layout_alignBottom | 跟什么底部對(duì)齊 |
| android:layout_alignParentBottom | 與父控件底部對(duì)齊 |
| android:layout_below | 在什么的底部 |
樣例效果
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#000000" />
<View
android:id="@+id/view_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@id/view_1"
android:background="#FF0033" />
<LinearLayout
android:id="@+id/ll_1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/view_2"
android:background="#0066FF"
android:orientation="horizontal"
android:padding="15dp">
<View
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FF0033" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:padding="15dp">
<View
android:id="@+id/view_3"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FF9900"/>
<View
android:id="@+id/view_4"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FF9900"
android:layout_toRightOf="@id/view_3"
android:layout_marginLeft="10dp"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
實(shí)現(xiàn)效果
![[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-QfAGp5xf-1644810525369)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220212100319575.png)]](http://img.jbzj.com/file_images/article/202202/2022021414102831.png)
總結(jié)
到此這篇關(guān)于Android線性布局與相對(duì)布局的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Android布局內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android仿微博個(gè)人詳情頁滾動(dòng)到頂部的實(shí)例代碼
這篇文章主要介紹了Android仿微博個(gè)人詳情頁滾動(dòng)到頂部的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒家,需要的朋友可以參考下2019-05-05
Android自定義短信倒計(jì)時(shí)view流程分析
倒計(jì)時(shí)實(shí)現(xiàn)有三種方式 而這個(gè)自定義view是通過handler實(shí)現(xiàn)的。本文通過實(shí)例代碼給大家介紹Android自定義短信倒計(jì)時(shí)view流程,,需要的朋友可以參考下2020-03-03
Android開發(fā)中DatePicker日期與時(shí)間控件實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了Android開發(fā)中DatePicker日期與時(shí)間控件,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-08-08
Android實(shí)現(xiàn)未讀消息小紅點(diǎn)顯示實(shí)例
大家好,本篇文章主要講的是Android實(shí)現(xiàn)未讀消息小紅點(diǎn)顯示實(shí)例,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Android使用AIDL方式實(shí)現(xiàn)播放音樂案例
這篇文章主要介紹了Android使用AIDL方式實(shí)現(xiàn)播放音樂案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android開發(fā)基礎(chǔ)實(shí)現(xiàn)音頻文件的播放詳解
這篇文章主要為大家介紹了Android開發(fā)基礎(chǔ)實(shí)現(xiàn)音頻文件的播放詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android開啟閃光燈的方法 Android打開手電筒功能
這篇文章主要為大家詳細(xì)介紹了Android開啟閃光燈的方法,Android打開手電筒功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android組件ContextMenu實(shí)現(xiàn)長按事件
這篇文章主要為大家詳細(xì)介紹了Android組件ContextMenu實(shí)現(xiàn)長按事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
android獲取情景模式和鈴聲 實(shí)現(xiàn)震動(dòng)、鈴聲提醒
這篇文章主要介紹了android獲取情景模式和鈴聲,實(shí)現(xiàn)震動(dòng)、鈴聲提醒,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

