Android LinearLayout(線性布局)

2023-03-31 12:00 更新

本節(jié)引言

本節(jié)開始講Android中的布局,Android中有六大布局,分別是: LinearLayout(線性布局),RelativeLayout(相對布局),TableLayout(表格布局) FrameLayout(幀布局),AbsoluteLayout(絕對布局),GridLayout(網(wǎng)格布局) 而今天我們要講解的就是第一個布局,LinearLayout(線性布局),我們屏幕適配的使用 用的比較多的就是LinearLayout的weight(權(quán)重屬性),在這一節(jié)里,我們會詳細(xì)地解析 LinearLayout,包括一些基本的屬性,Weight屬性的使用,以及比例如何計算,另外還 會說下一個用的比較少的屬性:android:divider繪制下劃線!


1.本節(jié)學(xué)習(xí)圖


2.weight(權(quán)重)屬性詳解:

①最簡單用法:

如圖:

實現(xiàn)代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/LinearLayout1"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"    
    android:orientation="horizontal">    

    <LinearLayout    
        android:layout_width="0dp"    
        android:layout_height="fill_parent"    
        android:background="#ADFF2F"     
        android:layout_weight="1"/>    

    <LinearLayout    
        android:layout_width="0dp"    
        android:layout_height="fill_parent"    
        android:background="#DA70D6"     
        android:layout_weight="2"/>    

</LinearLayout>  

要實現(xiàn)第一個的1:1的效果,只需要分別把兩個LinearLayout的weight改成1和1就可以了 用法歸納: 按比例劃分水平方向:將涉及到的View的android:width屬性設(shè)置為0dp,然后設(shè)置為android weight屬性設(shè)置比例即可;類推,豎直方向,只需設(shè)android:height為0dp,然后設(shè)weight屬性即可! 大家可以自己寫個豎直方向的等比例劃分的體驗下簡單用法!

②weight屬性詳解:

當(dāng)然,如果我們不適用上述那種設(shè)置為0dp的方式,直接用wrap_content和match_parent的話, 則要接著解析weight屬性了,分為兩種情況,wrap_content與match_parent!另外還要看 LinearLayout的orientation是水平還是豎直,這個決定哪個方向等比例劃分

1)wrap_content比較簡單,直接就按比例的了

實現(xiàn)代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/LinearLayout1"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"  
    android:orientation="horizontal" >    

    <TextView    
        android:layout_weight="1"    
        android:layout_width="wrap_content"    
        android:layout_height="fill_parent"    
        android:text="one"     
        android:background="#98FB98"    
     />    
     <TextView    
        android:layout_weight="2"    
        android:layout_width="wrap_content"    
        android:layout_height="fill_parent"    
        android:text="two"     
        android:background="#FFFF00"    
     />    
     <TextView    
        android:layout_weight="3"    
        android:layout_width="wrap_content"    
        android:layout_height="fill_parent"    
        android:text="three"     
        android:background="#FF00FF"    
     />    

</LinearLayout>  

2)match_parent(fill_parent):這個則需要計算了

我們寫這段簡單的代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/LinearLayout1"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent" >    

    <TextView    
        android:layout_weight="1"    
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent"    
        android:text="one"     
        android:background="#98FB98"    
     />    
     <TextView    
        android:layout_weight="2"    
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent"    
        android:text="two"     
        android:background="#FFFF00"    
     />    
     <TextView    
        android:layout_weight="3"    
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent"    
        android:text="three"     
        android:background="#FF00FF"    
     />    

</LinearLayout> 

運(yùn)行效果圖:

這個時候就會有疑問了,怎么會這樣,這比例是2:1吧,那么three去哪了?代碼里面明明有 three的啊,還設(shè)置了3的,而1和2的比例也不對耶,1:2:3卻變成了2:1:0,怎么會這樣呢? 答:這里其實沒那么簡單的,還是需要我們計算的,網(wǎng)上給出的算法有幾種,這里就給出筆者 覺得比較容易理解的一種: step 1:個個都是fill_parent,但是屏幕只有一個啦,那么1 - 3 = - 2 fill_parent step 2:依次比例是1/6,2/6,3/6 step 3:先到先得,先分給one,計算: 1 - 2 (1/6) = 2/3 fill_parent 接著到two,計算: 1 - 2 (2/6) = 1/3 fill_parent 最后到three,計算 1 - 2 * (3/6) = 0 fill_parent step 4:所以最后的結(jié)果是:one占了兩份,two占了一份,three什么都木有 以上就是為什么three沒有出現(xiàn)的原因了,或許大家看完還是有點蒙,沒事,我們舉多幾個例子試試就知道了!

比例為:1:1:1

按照上面的計算方法算一次,結(jié)果是:1/3 1/3 1/3,沒錯

接著我們再試下:2:3:4

計算結(jié)果:5/9 3/9 1/9,對比效果圖,5:3:1,也沒錯,所以這個計算方法你可得mark下了!

③Java代碼中設(shè)置weight屬性:

setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,     
        LayoutParams.WRAP_CONTENT, 1)); 

3.為LinearLayout設(shè)置分割線

很多界面開發(fā)中都會設(shè)置一些下劃線,或者分割線,從而使得界面更加整潔美觀,比如下面的酷狗 音樂的注冊頁面:

對于這種線,我們通常的做法有兩種 ①直接在布局中添加一個view,這個view的作用僅僅是顯示出一條線,代碼也很簡單:

<View  
    android:layout_width="match_parent"  
    android:layout_height="1px"  
    android:background="#000000" />  

這個是水平方向上的黑線,當(dāng)然你也可以改成其他顏色,或者使用圖片

②第二種則是使用LinearLayout的一個divider屬性,直接為LinearLayout設(shè)置分割線 這里就需要你自己準(zhǔn)備一張線的圖片了 1)android:divider設(shè)置作為分割線的圖片 2)android:showDividers設(shè)置分割線的位置,none(無),begining(開始),end(結(jié)束),middle(每兩個組件間) 3)dividerPadding設(shè)置分割線的Padding

使用示例:

實現(xiàn)代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/LinearLayout1"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:divider="@drawable/ktv_line_div"  
    android:orientation="vertical"  
    android:showDividers="middle"  
    android:dividerPadding="10dp"  
    tools:context="com.jay.example.linearlayoutdemo.MainActivity" >  

    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="按鈕1" />  

    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="按鈕2" />  

    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="按鈕3" />  

</LinearLayout>

4.LinearLayout的簡單例子:

實現(xiàn)代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/LinearLayout1"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    android:orientation="vertical"    
    tools:context=".MainActivity" >    

    <TextView    
        android:layout_width="wrap_content"    
        android:layout_height="wrap_content"    
        android:text="請輸入要保存的電話號碼"/>    
    <EditText    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"/>    
    <LinearLayout    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"    
        android:orientation="horizontal"    
        android:gravity="right">    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="保存"/>    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="清空"/>    
    </LinearLayout>    
</LinearLayout> 

5.注意事項:

使用Layout_gravity的一個很重要的問題!!! 問題內(nèi)容: 在一個LinearLayout的水平方向中布置兩個TextView,想讓一個左,一個右,怎么搞? 或許你會脫口而出:"gravity設(shè)置一個left,一個right就可以啦!" 真的這么簡單?你試過嗎?寫個簡單的Layout你就會發(fā)現(xiàn),事與愿違了: 代碼如下:

<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="horizontal"  
    tools:context="com.jay.example.getscreendemo.MainActivity" >  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="200dp"  
        android:layout_gravity="left"  
        android:background="#FF7878"  
        android:gravity="center"  
        android:text="O(∩_∩)O哈哈~" />  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="200dp"  
        android:layout_gravity="right"  
        android:background="#FF7428"  
        android:gravity="center"  
        android:text="(*^__^*) 嘻嘻……" />  

</LinearLayout>

運(yùn)行結(jié)果圖:

看到這里你會說:哎呀,真的不行耶,要不在外層LinearLayout加個gravity=left的屬性,然后設(shè)置第二個 TextView的layout_gravity為right,恩,好我們試一下:

<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="horizontal"  
    android:gravity="left"  
    tools:context="com.jay.example.getscreendemo.MainActivity" >  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="200dp"  
        android:background="#FF7878"  
        android:gravity="center"  
        android:text="O(∩_∩)O哈哈~" />  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="200dp"  
        android:layout_gravity="right"  
        android:background="#FF7428"  
        android:gravity="center"  
        android:text="(*^__^*) 嘻嘻……" />  

</LinearLayout> 

結(jié)果還是一樣:

好吧,沒轍了,怎么辦好?

當(dāng) android:orientation="vertical" 時, 只有水平方向的設(shè)置才起作用,垂直方向的設(shè)置不起作用。 即:left,right,center_horizontal 是生效的。 當(dāng) android:orientation="horizontal" 時, 只有垂直方向的設(shè)置才起作用,水平方向的設(shè)置不起作用。 即:top,bottom,center_vertical 是生效的。

然而,這方法好像并沒有什么卵用。比如: 如果只能豎直方向設(shè)置左右對齊的話,就會出現(xiàn)下面的效果:

這顯然不是我們要的結(jié)果把! 綜上,要么按照上述給出的規(guī)則來布局,不過對于這種情況還是使用相對布局RelativeLayout把! 網(wǎng)上沒給出具體的原因,都是說這樣改有人說這個和orientation的優(yōu)先級有關(guān) ,暫且先mark下來吧,后續(xù)如果知道原因的話再解釋!前面屏幕適配也說過了,布局還是建議使用 RelativeLayout!

PS:本文是之前筆者寫的布局系列文章中的一篇,自覺的寫得很贊,所以這里直接引用的原文內(nèi)容, 原文鏈接如下:New UI-布局之LinearLayout(線性布局)詳解

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號