Android六種佈局詳細講解(轉載)
轉載地址:http://blog.csdn.net/u013254061/article/details/52512146
這篇就對LinearLayout、RelativeLayout、自定義ViewGroup、FrameLayout、TableLayout、AbsoluteLayout六種佈局進行詳細的講解。
1.LinearLayout佈局
線性佈局,兩種排法:
- 從左到右
android:orientation=”horizontal” - 從上到下
android:orientation=”vertical”
具體上圖
一個豎向的大LinearLayout巢狀著兩個小LinearLayout,第一個小LinearLayout為橫向,第二個小LinearLayout為豎向。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width ="match_parent"
android:layout_height="250dp"
android:orientation="horizontal">
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#b2dfdb" />
<TextView
android:layout_width ="96dp"
android:layout_height="match_parent"
android:background="#80cbc4" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#4db6ac" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#26a69a" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#b2dfdb" />
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#80cbc4" />
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#4db6ac" />
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#26a69a" />
</LinearLayout>
</LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
2.RelativeLayout佈局
參考其他控制元件進行佈局,預設為父控制元件。
有三種類型的屬性:
- 屬性值是true或false
- android:layout_centerHrizontal 水平居中
- android:layout_centerVertical 垂直居中
- android:layout_centerInparent 相對於父元素完全居中。
- android:layout_alignParentBottom 位於父元素的下邊緣
- android:layout_alignParentTop 位於父元素的上邊緣
- android:layout_alignParentLeft 位於父元素的左邊緣
- android:layout_alignParentRight 位於父元素的右邊緣
- 屬性值是”@id/*“
- android:layout_below 在某元素的下方
- android:layout_above 在某元素的上方
- andorid:layout_toRightOf 在某元素的右方
- android:layout_toLeftOf 在某元素的左方
- android:layout_alignBottom 和某元素下方對齊
- android:layout_alignTop 和某元素上方對齊
- android:layout_alignRight 和某元素右方對齊
- android:layout_alignLeft 和某元素左方對齊
- 屬性值是數值
- android:layout_marginLeft 離某元素左邊緣的距離
- android:layout_marginRight 離某元素右邊緣的距離
- android:layout_marginTop 離某元素上邊緣的距離
- android:layout_marginBottom 離某元素下邊緣的距離
各取一個來寫例子,如圖。
注意:
- 如果沒有定義左右,那麼預設在左邊,如果沒有定義上下,預設在上邊。
- 相同位置,新定義的元素會覆蓋舊的元素。例:1被2覆蓋了。
- 4只定義了在父元素的下部,左右沒有定義,於是預設就在左邊了。
- android:layout_below,在某元素的下部並不意味著就一定是緊隨某元素,只是在下部的預設位置。例如:5是在3的下部,但是是在下部的預設左邊。
- 6為下邊緣對齊3,7為marginLeft=150dp。
- 8為多個屬性共同定義的結果。首先是在3的右部,然後是垂直居中,然後marginLeft=100dp得到最後位置。
程式碼如下:
<?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"
android:orientation="vertical">
<TextView
style="@style/btn_relative"
android:text="1" />
<TextView
style="@style/btn_relative"
android:text="2" />
<TextView
android:id="@+id/txt_center"
style="@style/btn_relative"
android:layout_centerInParent="true"
android:text="3" />
<TextView
style="@style/btn_relative"
android:layout_alignParentBottom="true"
android:text="4" />
<TextView
style="@style/btn_relative"
android:layout_below="@id/txt_center"
android:background="#d0d9ff"
android:text="5" />
<TextView
style="@style/btn_relative"
android:layout_alignBottom="@+id/txt_center"
android:text="6" />
<TextView
style="@style/btn_relative"
android:layout_marginLeft="150dp"
android:text="7" />
<TextView
style="@style/btn_relative"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:layout_toRightOf="@id/txt_center"
android:text="8" />
</RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
3.MyLayout佈局(自定義ViewGroup)
自定義佈局主要是重寫兩個方法:
- onMeasure() 這個是寫自定義容器的大小。
- onLayout() 這個是寫子元素的佈局。
我自己寫了一個自定義佈局,是順序填充會延對角線進行排列。
3.1onMeasure()
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/**
* 獲得此ViewGroup上級容器為其推薦的寬和高,以及計算模式
*/
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
// 計算出所有的childView的寬和高
measureChildren(widthMeasureSpec, heightMeasureSpec);
/**
* width和height是當wrap_content時使用的屬性。
*/
int width = 0;
int height = 0;
int cCount = getChildCount();
int cWidth = 0;
int cHeight = 0;
/**
* 在這裡計算當wrap_content時,佈局的大小。
*/
for (int i = 0; i < cCount; i++) {
View childView = getChildAt(i);
cWidth = childView.getMeasuredWidth();
cHeight = childView.getMeasuredHeight();
width += cWidth;
height += cHeight;
}
/**
* 如果是wrap_content設定為我們計算的值
* 否則:直接設定為父容器計算的值
*/
setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth
: width, (heightMode == MeasureSpec.EXACTLY) ? sizeHeight
: height);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
首先要說一下佈局計算模式,即最後的EXACTLY。一共有三種計算模式:
- MeasureSpec.EXACTLY:精確尺寸,相當於具體數值和match_parent。
- MeasureSpec.AT_MOST:最大尺寸,相當於 warp_content。
- MeasureSpec.UNSPECIFIED:未指定尺寸,這種情況不多,一般用於AdapterView。
最後的設定大小時,如果是精確尺寸就是用sizeWidth即獲取的尺寸,如果是最大尺寸就是要我們自己計算的那個尺寸了。
onMeasure()最主要的功能就是計算wrap_content的尺寸和設定尺寸。
我將這個方法稱為“建畫布”,先建了畫布才能在上面繪圖嘛。
3.2 onLayout()
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int cCount = getChildCount();
/**
* 遍歷所有childView根據其寬和高,以及margin進行佈局
*/
for (int i = 0; i < cCount; i++) {
View childView = getChildAt(i);
r = l + childView.getMeasuredWidth();
b = t + childView.getMeasuredHeight();
childView.layout(l, t, r, b);
l += childView.getMeasuredWidth();
t += childView.getMeasuredHeight();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
這個方法的作用是設定擺放子元素的位置。其中onLayout()傳入的l、t、r、b分別是這樣
- l,t分別對應子元素左上角的left,top座標
- r,b分別對應子元素右下角的right,bottom座標
並且可以使用childview.getMeasuredWidth()和childView.getMeasureHeight()得到子元素的寬和高。
這樣就可以來對每個子元素進行佈局了。
我稱這個方法為“定位置”。定完位置後那麼子元素就被放到了我們想要的地方。
這樣一個自定義ViewGroup就可以使用了。
xml檔案如下:
<?xml version="1.0" encoding="utf-8"?>
<com.example.layoutdemo.MyLayout.MyLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#b2dfdb" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#80cbc4" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#4db6ac" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#26a69a" />
</com.example.layoutdemo.MyLayout.MyLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
最後效果如圖:
4.FrameLayout佈局
幀佈局,這個佈局的特點是從左上角開始,後面的會覆蓋前面的控制元件。
程式碼如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="100dp"
android:textColor="#9c27b0"
android:text="第一層"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="80dp"
android:textColor="#e91e63"
android:text="第二層"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60dp"
android:textColor="#e51c23"
android:text="第三層"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:textColor="#5677fc"
android:text="第四層"/>
</FrameLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
實際效果如下:
5.TableLayout佈局
表格佈局。
它遵循著以下結構:
<TableLayout>
<TableRow>
<!-在這裡填充第一行的元素->
</TableRow>
<TableRow>
<!-在這裡填充第二行的元素->
</TableRow>
</TableLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
還有幾個重要屬性:
- 寫在TableLayout中的屬性
- android:stretchColumns 設定第幾列為伸展(0表示第一列)
- android:shrinkColumns 設定第幾列為收縮
- android:collapseColumns 設定第幾列為隱藏
- 寫在TableRow裡的控制元件裡的屬性
- android:layout_column 設定控制元件在第幾列
- android:layout_span 設定控制元件能跨多少列
程式碼如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:collapseColumns="2"
android:shrinkColumns="1"
android:stretchColumns="0">
<TableRow
>
<TextView android:text="我是伸展的第一列" />
<TextView android:text="我是收縮的第二列" />
<TextView android:text="我被隱藏了" />
</TableRow>
<TableRow>
<TextView android:text="我可以伸展的很長很長很長長" />
<TextView android:text="我可以收縮,我可以變的很深很深很深" />
<TextView android:text="我被隱藏了T_T" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="我要在第2列" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_span="2"
android:text="我要 跨 兩 列" />
</TableRow>
</TableLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
-
相關推薦
Android六種佈局詳細講解(轉載)
轉載地址:http://blog.csdn.net/u013254061/article/details/52512146 這篇就對LinearLayout、RelativeLayout、自定義ViewGroup、FrameLayout、TableLayout、Abs
四.Android六種佈局詳細講解
這篇就對LinearLayout、RelativeLayout、自定義ViewGroup、FrameLayout、TableLayout、AbsoluteLayout六種佈局進行詳細的講解。 1.LinearLayout佈局 線性佈局,兩種排法:
android BLE藍芽詳細講解(一)
本文主要講解Android低功耗藍芽的api使用以及藍芽掃描、連線、傳送資料、接收資料等一系列操作,本篇結尾有本人封裝的BleLib藍芽庫,非常適合藍芽初學者使用,只需要一行程式碼注入就OK了,而且用法也極其簡單,我會在第二篇中專門講解一下BleLib庫的使用。
BN講解(轉載)
com 簡化 struct 如果 shift 深度 簡單的 反向傳播 tps 本文轉載自:http://blog.csdn.net/shuzfan/article/details/50723877 本次所講的內容為Batch Normalization,簡稱BN,來源於《B
CSDN中字型顏色的十六進位制表(轉載)
顏色名列表 顏色名 十六進位制顏色值 顏色 AliceBlue #F0F8FF rgb(240, 248, 255) AntiqueWhite #FAEBD7
json的三種反序列方式(轉載)
JSON(JavaScript Object Notation),在實際的開發中非常常用,甚至一個json就可以儲存所有需要的信心呢。 物件:一個物件以花括號"{"開始,並以"}"結束,json儲存使用key:value形式,每一個鍵後 有一個冒號
MySQL的四種事務隔離級別(轉載)
本文實驗的測試環境:Windows 10+cmd+MySQL5.6.36+InnoDB 一、事務的基本要素(ACID) 1、原子性(Atomicity):事務開始後所有操作,要麼全部做完,要麼全部不做,不可能停滯在中間環節。事務執行過程中出錯,會回滾到事務開始前的狀態,所有的操作就像沒有
C# wpf StackPanel,DockPanel,和WrapPanel3種佈局的區別(3)
1,StacjPanel StackPanel是以堆疊的方式顯示其中的控制元件 可以使用Orientation屬性更改堆疊的順序分為水平方向(Orientation=“Horizontal”)和豎直方向(Orientation=“Vertical”),以保證要實現的效果。 2,WrapP
白話Hadoop入門-WordCount詳細講解(2)
前一篇部落格講述瞭如何進行Hadoop壞境的搭建,以及第一個傳輸檔案程式的編寫,通過第一個檔案可能大概對Hadoop有一個瞭解了,但是Hadoop的精髓在於mapreduce,下面我們就來看看如何編寫Hadoop的第一個“hello world”程式--也就是Wor
vmware三種網路模式配置(轉載)
虛擬機器系統安裝的是Linux系統 首先,在本機上檢視所有網路配置連線,使用命令:ipconfig Microsoft Windows [版本 6.1.7600]版權所有 (c) 2009 Microsoft Corporation。保留所有權利。 C:\Users\Administrator>ip
jquery ajax error函式和及其引數詳細說明(轉載)
使用jquery的ajax方法向伺服器傳送請求的時候,常常需要使用到error函式進行錯誤資訊的處理,本文詳細的說明了ajax中error函式和函式中各個引數的用法。一般error函式返回的引數有三個: function(jqXHR jqXHR, String textStatus, String error
Android開發中佈局與元件(二)—— padding 與 margin 的區別
在 Android開發中我們會設定某個檢視相對於別的檢視的距離,這時我們就要用到 margin 和 padding ,但是有時候很容易把這兩個屬性弄混淆,那我們就看看他們的區別。 外邊距(margin): 屬於佈局引數,決定兩個元件之間的距離。作用於多個元件之間。 內邊距(
Android開發中佈局與元件(一)—— 螢幕尺寸單位dp,px,sp的探究
在Android開發中,常用的尺寸單位有 dp , px , sp 。當然還有其他的單位如 pt , mm 等,不過這些都是不常用,所以我們重點來探究一下 dp , px , sp 這三個常用的單位。 px 英文 pixel 的縮寫,即畫素。無論螢幕密度為多少,一個畫素單位對應
5G技術通俗講解(轉載)
網際網路改變了世界,移動網際網路重新塑造了生活,“在家不能沒有網路,出門不能忘帶手機”已成為很多人的共同感受。人們對動網際網路的要求是更高速、更便捷、更強大、更便宜,需求的“更”是沒有止境的,這促使著移動網際網路技術突飛猛進,技術體制的更新換代也隨之越來越快。很多使用者剛剛踏入4G的門檻,5G時代很快就要來到
Android Studio安裝配置詳細步驟(圖文)
下載 到 https://developer.android.com/sdk/index.html (需翻牆)或者通過其他途徑下載安裝包。我下載的是android-studio-bundle-145.3537739-windows.exe 為完整安裝包,其中包
你看到的最直白清晰的CNN講解(轉載)
向AI轉型的程式設計師都關注了這個號??? 大資料探勘DT資料分析 公眾號: datadw 這篇部落格介紹的是深度神經網路中常用在影象處理的模型——卷積神經網路(CNN),CNN在影象分類中(如kaggle的貓狗大戰)大顯身手。這篇部落格將帶你瞭解影象
六種白盒測試(1)
常用的軟體測試方法有兩大類:靜態測試方法和動態測試方法。其中軟體的靜態測試不要求在計算機上實際執行所測程式,主要以一些人工的模擬技術對軟體進行分析和測試;而軟體的動態測試是通過輸入一組預先按照一定的測試準則構造的例項資料來動態執行程式,而達到發現程式錯誤的過程。
Cascade R-CNN論文講解(轉載)
轉載連結:https://blog.csdn.net/qq_21949357/article/details/80046867 論文思想:為了解決IOU設定帶來的最終的AP值,作者引入了cascade結構的迴歸器,採用cascade R-CNN stages,用一個stage的輸出去訓練下一個stage,
using namespace std;的講解(轉載)
所謂namespace,是指識別符號的各種可見範圍。 C++標準程式庫中的所有識別符號都被定義於一個名為std的namespace中。 由於namespace的概念,使用C++標準程式庫的任何識別符號時,可以有三種選擇: 1、直接指定識別符號。例如std::ostream而不是ostream。完整語句如下
專案解耦必備-EventBus的詳細講解(一)
相關文章: 很早之前,就想寫一篇關於EventBus使用的文章,一直沒有時間,最近專案不太忙,抽出一些時間把之前的專案中的技術沉澱一下,抽取出一些東西寫成文章與大家分享一下。 在做專案的時候,難