Android中有六大布局,分別是:
- LinearLayout(線性布局)
- RelativeLayout(相對布局)
- TableLayout(表格布局)
- FrameLayout(幀布局)
- AbsoluteLayout(絕對布局)
- GridLayout(網格布局)
線性布局。這個布局簡單的說,就是所有控件都依次排序,
誰也不會覆蓋誰。線性布局需要定義一個方向,
橫向(Android:orientation="horizontal")
或縱向(android:orientation="vertical")。
也就是說,控件要么就並排橫向的排列,要么就縱向的筆直排列。
而Android的開發常用到的有LinearLayout和RelativeLayout。我們屏幕適配的使用用的比較多的就是LinearLayout的weight(權重屬性)。下面的關於LinearLayout的一些知識。
<LinearLayout +代碼
> </LinearLayout>水平布局
android:background="#ef0000"背景顏色
android:layout_weight="1"塊所占的權重
android:gravity="center_vertical">對齊方式
android:id="@+id/LinearLayout1" :為該資源控件設置一個資源 id,在 Java 代碼中可以通過 findViewById(id) 找到該控件
android:layout_width="match_parent":布局的寬度設置為填滿父容器
android:layout_height="match_parent":布局的高度設置為填滿父容器
android:orientation="horizontal":布局中控件的排列方式設置為水平方向
android:layout_height="fill_parent:布局高度設置為充滿父容器
android:layout_margin:外邊距,布局或控件距離外部元素的邊距
android:layout_padding:內邊距,布局或控件距離內部元素的邊距
tools:context=".MainActivity":聲明當前布局提供給 activity 使用
android:layout_weight:設置權重,按比例划分水平方向,將涉及到的 View 的 android:layout_width 屬性設置為 0dp,然后使用 android:layout_weight 屬性設置比例即可,如上所示,第二個 LinearLayout 是第一個的兩倍。
以此類推,豎直方向,只需設 android:layout_height 為 0dp,然后設置 layout_weight 屬性即可,如果要達到 1:1 等分效果,則顯然只需要分別把兩個 LinearLayout 的 weight 改成1和1就可以了。
水平線性布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <TextView android:background="#da0202" android:layout_width="100dp" android:layout_height="100dp" android:text="Hello World!" /> <TextView android:background="#1aab95" android:layout_width="100dp" android:layout_height="100dp" android:text="Hello World!" /> <TextView android:background="#1f1f99" android:layout_width="100dp" android:layout_height="100dp" android:text="Hello World!" /> <TextView android:background="#121010" android:layout_width="100dp" android:layout_height="100dp" android:text="Hello World!" /> </LinearLayout>
水平線性布局把控件依次水平線性排列不會重疊,但是第四個明顯放不下跑到屏幕外面去了。
我們再來看看垂直方向的將android:orientation=”horizontal”改成android:orientation=”vertical”
這里變成了垂直排列其中的控件。
從這里我們可以看出線性布局沒法直接控制控件的具體位置,以及相對的位置關系。每個控件都依次擺放。不過控件間的間距可以調整,控件也不會相互覆蓋。線性布局可以嵌套使用,可以在一個縱向布局中加入一個橫向布局。用這種嵌套方式,可以完成一些稍微復雜的頁面。不過,當嵌套的情況使用的多了,並且嵌套的層次也多了,就會給維護帶來非常大的麻煩。自己回頭再看布局那就真是頭大了。
下面學習下如何將其中的控件均勻擺放,恩就是權重。android:layout_weight
我們將上面的布局代碼更改如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <TextView android:background="#da0202" android:layout_width="0dp" android:layout_weight="1" android:layout_height="100dp" android:text="Hello World!" /> <TextView android:background="#1aab95" android:layout_width="0dp" android:layout_weight="1" android:layout_height="100dp" android:text="Hello World!" /> <TextView android:background="#1f1f99" android:layout_width="0dp" android:layout_weight="1" android:layout_height="100dp" android:text="Hello World!" /> <TextView android:background="#121010" android:layout_width="0dp" android:layout_weight="1" android:layout_height="100dp" android:text="Hello World!" /> </LinearLayout>
權重也就是按比例來分配控件。谷歌建議水平線性布局的寬度設置成0dp后再設置對應控件的權重,垂直的的高度設置成0再設置權重
權重分配問題:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <TextView android:background="#da0202" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="100dp" android:text="Hello World!" /> <TextView android:background="#1aab95" android:layout_width="match_parent" android:layout_weight="2" android:layout_height="100dp" android:text="Hello World!" /> </LinearLayout>
我們發現權重比是1:2 然而這兩個控件的比例卻反過來了!
再來看看垂直方向的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <TextView android:background="#da0202" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent" android:text="Hello World!" /> <TextView android:background="#1aab95" android:layout_width="match_parent" android:layout_weight="2" android:layout_height="match_parent" android:text="Hello World!" /> </LinearLayout>
還是倒了
權重(layout_weight)對線性布局中水平(垂直)方向的剩余空間進行分配。
那么我們現在假設這個手機橫向寬度 480dp 縱向高度480dp 。先分析水平方向的布局。
左邊的紅色控件占據的的空間為match_parent 也就是320dp 右邊綠色的空間占據的空間也是320dp 此時屏幕剩余的空間是多少? 480dp - 480dp - 480dp = -480dp ; 此時屏幕剩余空間是-320dp 那么左右進行比例分配 也就是
左邊的空間為 : 480dp + 1/3 * (-480dp ) = 320dp
右邊的空間為 :480dp + 2/3 * (-480dp ) = 160dp
符合上面的現象
垂直方向的分析也是如此
總結下權重分配有兩種情況:
情況1:當線性布局中內部子控件的寬度之和大於線性布局的總寬度時,即權重越大,當前控件所占空間越小。
情況2:當線性布局中內部子控件的寬度之和小於線性布局的總寬度時,即權重越大,當前控件所占空間越小。
所以我們在使用權重的時候一般情況把要布局的一般把width或者height設置為0dp
部分參考https://blog.csdn.net/qq910689331/article/details/52760534