Android線性布局與相對布局的實現(xiàn)
更新時間:2022年02月14日 14:15:36 作者:少年白馬
大家好,本篇文章主要講的是Android線性布局與相對布局的實現(xiàn),感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
線性布局(LinearLayout)
名字 | 含義 |
---|---|
android:id | 設置一個id方便使用 |
android:layout_width | 寬度 |
android:layout_height | 高度 |
android:background | 設置背景顏色 |
android:layout_margin | 設置外邊距 |
android:layout_padding | 設置內(nèi)邊距 |
android:orientation | 設置方向(水平或者垂直) |
練習代碼
<?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>
實現(xiàn)效果
相對布局(RelativeLayout)
最常用屬性
名字 | 含義 |
---|---|
android:layout_toLeftOf | 在什么的左邊 |
android:layout_toRightOf | 在什么的右邊 |
android:layout_alignBottom | 跟什么底部對齊 |
android:layout_alignParentBottom | 與父控件底部對齊 |
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>
實現(xiàn)效果
總結(jié)
到此這篇關(guān)于Android線性布局與相對布局的實現(xiàn)的文章就介紹到這了,更多相關(guān)Android布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)中DatePicker日期與時間控件實例代碼
本文通過實例代碼給大家介紹了Android開發(fā)中DatePicker日期與時間控件,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-08-08Android開發(fā)基礎(chǔ)實現(xiàn)音頻文件的播放詳解
這篇文章主要為大家介紹了Android開發(fā)基礎(chǔ)實現(xiàn)音頻文件的播放詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02Android開啟閃光燈的方法 Android打開手電筒功能
這篇文章主要為大家詳細介紹了Android開啟閃光燈的方法,Android打開手電筒功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Android組件ContextMenu實現(xiàn)長按事件
這篇文章主要為大家詳細介紹了Android組件ContextMenu實現(xiàn)長按事件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-04-04android獲取情景模式和鈴聲 實現(xiàn)震動、鈴聲提醒
這篇文章主要介紹了android獲取情景模式和鈴聲,實現(xiàn)震動、鈴聲提醒,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12