本節開始講Android中的佈局,Android中有六大布局,分別是: LinearLayout(線性佈局),RelativeLayout(相對佈局),TableLayout(表格佈局) FrameLayout(幀佈局),AbsoluteLayout(絕對佈局),GridLayout(網格佈局) 而今天咱們要講解的就是第一個佈局,LinearLayout(線性佈局),咱們屏幕適配的使用 用的比較多的就是LinearLayout的weight(權重屬性),在這一節裏,咱們會詳細地解析 LinearLayout,包括一些基本的屬性,Weight屬性的使用,以及比例如何計算,另外還 會說下一個用的比較少的屬性:android:divider繪製下劃線!android
如圖:算法
實現代碼:ide
<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>
要實現第一個的1:1的效果,只須要分別把兩個LinearLayout的weight改爲1和1就能夠了 用法概括: 按比例劃分水平方向:將涉及到的View的android:width屬性設置爲0dp,而後設置爲android weight屬性設置比例便可;類推,豎直方向,只需設android:height爲0dp,而後設weight屬性便可! 你們能夠本身寫個豎直方向的等比例劃分的體驗下簡單用法!佈局
固然,若是咱們不適用上述那種設置爲0dp的方式,直接用wrap_content和match_parent的話, 則要接着解析weight屬性了,分爲兩種狀況,wrap_content與match_parent!另外還要看 LinearLayout的orientation是水平仍是豎直,這個決定哪一個方向等比例劃分學習
1)wrap_content比較簡單,直接就按比例的了spa
實現代碼:.net
<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):這個則須要計算了code
咱們寫這段簡單的代碼:xml
<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>
運行效果圖:blog
這個時候就會有疑問了,怎麼會這樣,這比例是2:1吧,那麼three去哪了?代碼裏面明明有 three的啊,還設置了3的,而1和2的比例也不對耶,1:2:3卻變成了2:1:0,怎麼會這樣呢? 答:這裏其實沒那麼簡單的,仍是須要咱們計算的,網上給出的算法有幾種,這裏就給出筆者 以爲比較容易理解的一種: 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:因此最後的結果是:one佔了兩份,two佔了一份,three什麼都木有 以上就是爲何three沒有出現的緣由了,或許你們看完仍是有點蒙,沒事,咱們舉多幾個例子試試就知道了!
比例爲:1:1:1
按照上面的計算方法算一次,結果是:1/3 1/3 1/3,沒錯
接着咱們再試下:2:3:4
計算結果:5/9 3/9 1/9,對比效果圖,5:3:1,也沒錯,因此這個計算方法你可得mark下了!
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
不少界面開發中都會設置一些下劃線,或者分割線,從而使得界面更加整潔美觀,好比下面的酷狗 音樂的註冊頁面:
對於這種線,咱們一般的作法有兩種 ①直接在佈局中添加一個view,這個view的做用僅僅是顯示出一條線,代碼也很簡單:
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#000000" />
這個是水平方向上的黑線,固然你也能夠改爲其餘顏色,或者使用圖片
②第二種則是使用LinearLayout的一個divider屬性,直接爲LinearLayout設置分割線 這裏就須要你本身準備一張線的圖片了 1)android:divider設置做爲分割線的圖片 2)android:showDividers設置分割線的位置,none(無),begining(開始),end(結束),middle(每兩個組件間) 3)dividerPadding設置分割線的Padding
使用示例:
實現代碼:
<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>
實現代碼以下:
<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>
使用Layout_gravity的一個很重要的問題!!! 問題內容: 在一個LinearLayout的水平方向中佈置兩個TextView,想讓一個左,一個右,怎麼搞? 或許你會脫口而出:"gravity設置一個left,一個right就能夠啦!" 真的這麼簡單?你試過嗎?寫個簡單的Layout你就會發現,事與願違了: 代碼以下:
<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>
運行結果圖:
看到這裏你會說:哎呀,真的不行耶,要不在外層LinearLayout加個gravity=left的屬性,而後設置第二個 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>
結果仍是同樣:
好吧,沒轍了,怎麼辦好?
當 android:orientation="vertical" 時, 只有水平方向的設置才起做用,垂直方向的設置不起做用。 即:left,right,center_horizontal 是生效的。 當 android:orientation="horizontal" 時, 只有垂直方向的設置才起做用,水平方向的設置不起做用。 即:top,bottom,center_vertical 是生效的。
然而,這方法好像並無什麼卵用。好比: 若是隻能豎直方向設置左右對齊的話,就會出現下面的效果:
這顯然不是咱們要的結果把! 綜上,要麼按照上述給出的規則來佈局,不過對於這種狀況仍是使用相對佈局RelativeLayout把! 網上沒給出具體的緣由,都是說這樣改有人說這個和orientation的優先級有關 ,暫且先mark下來吧,後續若是知道緣由的話再解釋!前面屏幕適配也說過了,佈局仍是建議使用 RelativeLayout!
PS:本文是以前筆者寫的佈局系列文章中的一篇,自覺的寫得很贊,因此這裏直接引用的原文內容, 原文連接以下:New UI-佈局之LinearLayout(線性佈局)詳解