国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android UI組件LinearLayout線性布局詳解

2019-12-12 05:36:45
字體:
來源:轉載
供稿:網友

LinearLayout 線性布局,該布局的繼承關系:

這里寫圖片描述 

1. 什么是線性布局
通俗的說感覺起來和線有關,參照線的特點,有么是橫向的,要么是豎向的。
LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列(通過android:orientation屬性來控制),按照相對位置來排列所有的widgets或者其他的containers,超過邊界時,某些控件將缺失或消失
2. 線性布局常用基本屬性
- android:id
- android:orientation
- android:layout_height
- android:layout_width
- android:gravity
- android:layout_gravity
- android:background
- android:layout_margin:
- android:padding
- android:weightSum
- android:layout_weight
- android:baselineAligned
3. 常用屬性值介紹:
android:id:
這是布局的唯一標識ID
android:orientation:他表示的是這個線性布局是采用橫向還是縱向布局,通常來說只有兩個值:
1. android:orientation=”vertical”表示采用縱向的布局方式,所有在當前布局中添加的所有控件都依次按豎向排列

這里寫圖片描述 

2. android:orientation=”horizontal”表示采用橫向的布局方式,所有在當前布局中添加的所有控件都依次按橫向排列(默認水平)

這里寫圖片描述

android:layout_height: 表示當前線性布局的高度

4. android:layout_height="match_parent"  (表示高度占滿整個屏幕)
5. android:layout_height="wrap_content" (表示高度根據其包含的控件自適應調整)
6. android:layout_height="30dp"(自定義設置高度,通常單位為dp)

android:layout_width: 表示當前線性布局的寬度

7. android:layout_width="match_parent"  (表示寬度占滿整個屏幕)
8. android:layout_width="wrap_content" (表示寬度根據其包含的控件自適應調整)
9. android:layout_width="30dp"(自定義設置寬度,通常單位為dp)

android:gravity: 表示所有包含在當前布局中的所有控件采用某種方式對齊(默認左對齊)

這里寫圖片描述
這里寫圖片描述 

從上依次往下為:
center (垂直且水平居中)
center_horizontal (水平居中)
bottom (底部對齊)
center_vertical (垂直居中)
clip_horizontal (水平方向裁剪,當對象邊緣超出容器的時候,將上下邊緣超出的部分剪切掉,剪切基于縱向對齊設置:頂部對齊時,剪切底部;底部對齊時剪切頂部;除此之外剪切頂部和底部.)
clip_vertical (垂直方向裁剪,當對象邊緣超出容器的時候,將左右邊緣超出的部分剪切掉,剪切基于橫向對齊設置:左對齊時,剪切右邊部分;右對齊時剪切左邊部分;除此之外剪切左邊和右邊部分.)
end (放在容器的結束位置,不改變其大小)
fill (必要的時候增加對象的橫縱向大小,以完全充滿其容器)
fill_horizontal (必要的時候增加對象的橫向大小,以完全充滿其容器. 水平方向充)
fill_vertical (必要的時候增加對象的縱向大小,以完全充滿其容器. 垂直方向填充)
left (將對象放在其容器的左部,不改變其大小)
right (將對象放在其容器的右部,不改變其大小)
start (將對象放在其容器的開始位置,不改變其大小)
top (將對象放在其容器的頂部,不改變其大小)

android:layout_gravity: 表示當前線性布局相對于父元素的對齊方式
如上所示
android:background: 表示當前線性布局的背景顏色
android:margin:表示外邊距,通常表示本控件與父控件四面之間的距離

這里寫圖片描述 

從上往下:
1. 底邊距
2. 與控件結尾的邊距
3. 左邊距
4. 右邊距
5. 與控件的起始邊距
6. 頂邊距

android:padding:表示內邊距,通常表示是本元素所有子元素的與父元素邊緣的距離,設置在父元素上,比如文字與文本控件的所有距離

這里寫圖片描述 

從上往下如上所示
android:weightSum:權重的總比例
android:layout_weight:子元素對未占用空間水平或垂直分布的權重

<?xml version="1.0" encoding="utf-8"?><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" tools:context="com.qianfeng.demo1.MainActivity" android:weightSum="6" //總的權重為6 android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2"http://此控件占用比例為2,其他控件全部占用1,超過比例的權重都不會分配對應的大小,相當于大小為0 android:textSize="30sp" android:text="aaaa" android:background="#f00" p /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="30sp" android:text="bbbb" android:background="#0f0" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="30sp" android:background="#00f" android:text="cccc" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="30sp" android:text="dddd" android:background="#0ff" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="30sp" android:background="#f0f" android:text="eeee" /></LinearLayout>

效果圖如下:

這里寫圖片描述

android:baselineAligned:該控件只對能顯示text的子控件有效。
這必須是一個布爾值,要么“true”或“false”,并防止布局調整其子的基線。默認是true

這里介紹一個小細節:

這里以垂直分配權重為列,權重是可以為負數的,權重是根據比例分配對應大小,這里aaaa控件的高為0dp,bbbb控件的高為0dp,所以,aaaa控件分配權重的是5,bbbb分配權重的是1,所以這里aaaa會比bbbb占用比例大。

<?xml version="1.0" encoding="utf-8"?><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" tools:context="com.qianfeng.demo1.MainActivity" android:weightSum="6" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="5" android:textSize="30sp" android:text="aaaa" android:background="#f00" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="30sp" android:text="bbbb" android:background="#0f0" /></LinearLayout>

效果圖:

這里寫圖片描述

反之,如果設置了aaaa控件的高為match_parent, bbbb控件的高也為match_parent,aaaa控件分配權重的是5,bbbb分配權重的是1,這樣就形成了一種想反的效果,相當于aaaa控件分配的是-5 bbbb控件分配的是-1 而-1 比 -5大,所以,對應bbbb控件占用的比例比aaaa大

<?xml version="1.0" encoding="utf-8"?><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" tools:context="com.qianfeng.demo1.MainActivity" android:weightSum="6" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="5" android:textSize="30sp" android:text="aaaa" android:background="#f00" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:textSize="30sp" android:text="bbbb" android:background="#0f0" /></LinearLayout>

效果圖如下:

這里寫圖片描述

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 星座| 岳阳市| 宁陵县| 临澧县| 北辰区| 黔东| 布尔津县| 临高县| 搜索| 高平市| 陵水| 思南县| 灌云县| 射阳县| 忻州市| 法库县| 黄大仙区| 盐池县| 西乌| 嘉禾县| 凤阳县| 论坛| 通道| 长乐市| 金昌市| 葵青区| 广宗县| 建宁县| 金坛市| 寻乌县| 安义县| 五寨县| 雅安市| 襄城县| 镇赉县| 成都市| 张掖市| 包头市| 利津县| 如皋市| 满城县|