LinearLayout線性布局搭配權重屬性的使用


在開發中,我們是通過布局來完成應用界面的搭配的,通過各種布局,我們可以完成各種復雜的界面設計。而LinearLayout也就是我們說的線性布局,這個比較簡單而且使用很廣泛的一種布局。下面我們通過一個Demo來對這個布局進行學習。我們先來看看效果圖吧。

捕獲

然后在來看看布局文件main_layout.xml

 

<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"
    android:orientation="vertical"
    tools:context="com.example.linerlayoutdemo.MainActivity" >
    
    <!-- 這個例子很簡單主要知識點也就兩個 -->
    <!-- 1、布局嵌套,這個例子通過在一個線性布局里面嵌套了兩個垂直方向線性布局 -->
    <!-- 2、利用android:layout_weight這個屬性平均分配寬度和高度,也就是權重屬性 -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#120ff0" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#950ff0" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/darker_gray" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#000ff0" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#079ff0" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000fca" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000f65" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_bright" />
    </LinearLayout>

</LinearLayout>

 

 

 

這里我們首先說一下LinearLayout,線性布局有兩個方向,水平和垂直方向。分別是通過android:orientation="horizontal"和android:orientation="vertical"來控制的。這里說一個記憶的小技巧。因為小編英文不太好。經常忘記那個是水平那個是垂直。所以我就記住horizontal是H開頭的。這個是水平方向,那么水平就是橫向嘛!hen,H開頭。這樣就不會記錯了。

從這個Demo的效果圖我們可以很清楚的看出,Demo在一個垂直方向的線性布局里面分別嵌套了兩個線性布局。上面一個是horizontal方向,下面一個是vertical方向。

說完布局我們順便說一說這個Demo的知識點。代碼注釋我們有說,這個Demo主要兩個知識點,一個是布局嵌套,一個是權重分配。這里們說一說權重。

權重,也就是對控件設置 android:layout_weight的屬性。這個屬性的意思是分配剩余空間。注意,是剩余空間,所以,我們一般將控件的寬度或者高度設置為0dp(至於是寬度還是高度就是看布局是水平還是垂直了)。這里我先說簡單的。

比如有倆個控件,分別設置為android:layout_weight=“1”,android:layout_weight=“2”,表示控件分別占屏幕的1/3和2/3,。不過這是有一個前提的,就是建立在控件的寬度或者高度設置為0dp的情況下。

 

然后我們說一下復雜的。其實也不復雜。權重是分配剩余空間的意思就是說,權重屬性他是對屏幕剩下的空間進行分配。這就是為什么上面我們說要將控件的寬度或者高度設置為0dp的原因。假如說,控件設置了寬度或者高度,那么權重分配的公式如下:

控件1的寬度或者高度=定義的寬度或者高度+1/3(屏幕寬度或者高度-兩個控件的寬度或者高度之和)

控件2的寬度或者高度=定義的寬度或者高度+2/3(屏幕寬度或者高度-兩個控件的寬度或者高度之和)。

 

通過公式我們應該就明白了權重分配剩余空間的原理了吧。這里我們要注意一點,假如你是在horizontal的線性布局里面分配權重,那么,高度不能設置為0dp。理解很簡單,水平方向的寬度因為你設置了權重,所以寬度由權重來分配。所以寬度設置為0dp系統不會報錯。但是高度沒人給他分配呀。。所以高度不能設置為0dp。在vertical布局中就反之。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM