Android中有六大布局,分別是: LinearLayout(線性佈局),RelativeLayout(相對佈局),TableLayout(表格佈局) FrameLayout(幀佈局),AbsoluteLayout(絕對佈局),GridLayout(網格佈局) 而今天我們要講解的就是第一個佈局,LinearLayout(線性佈局),我們屏幕適配的使用 用的比較多的就是LinearLayout的weight(權重屬性),在這一節裏,我們會詳細地解析 LinearLayout,包括一些基本的屬性,Weight屬性的使用,以及比例如何計算,另外還 會說下一個用的比較少的屬性:android:divider繪製下劃線!
1.weight(權重)屬性使用詳解:
① 0dp時設置weight(也是最簡單的方式)
代碼如下:
<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屬性即可! 大家可以自己寫個豎直方向的等比例劃分的體驗下簡單用法!
② wrap_content 時設置weight
wrap_content比較簡單,直接就按比例的了
代碼如下:
<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>
③match_parent(fill_parent) 設置weight, 這個則需要計算了
代碼如下:
<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>
這就有疑問了,怎麼設置明明是1:2:3,結果確實2:1,且第三個都無法顯示出來?
真正的原因是Layout_width="fill_parent"的原因造成的。依照上面理解我們來分析:系統先給3個textview分配他們所要的寬度fill_parent,也就是說每一都是填滿他的父控件,這裏就是屏幕的寬度,那麼這時候的剩餘空間=1*parent_width - 3*parent_width = -2*parent_width (parent_width指的是屏幕寬度 )
那麼第一個TextView的實際所佔寬度應該=fill_parent的寬度, 即1*parent_width + 他所佔剩餘空間的權重比列1/6 * 剩餘空間大小(即-2 parent_width)=2/3*parent_width,即佔屏幕寬度的2/3顯示;
同理第二個TextView的實際所佔寬度=1*parent_width + 2/6*(-2*parent_width)=1/3*parent_width,即佔屏幕的1/3顯示;
第三個TextView的實際所佔寬度=1*parent_width + 3/6*(-2parent_width)=0*parent_width;所以就不能顯示。
④在JAVA代碼中設置weight屬性的方式
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1));
2. layout_gravity 和 gravity屬性介紹
layout_gravity控制該組件在父容器中的對齊方式
gravity 控制該組件所包含的子VIEW的對其方式
當 android:orientation="vertical" 時, 只有水平方向的設置才起作用,垂直方向的設置不起作用。 即:left,right,center_horizontal 是生效的。
當
android:orientation="horizontal" 時, 只有垂直方向的設置才起作用,水平方向的設置不起作用。 即:top,bottom,center_vertical 是生效的。
不過,控制複雜的佈局,最好使用RelativeLayout相對佈局。