本節(jié)引言
本節(jié)開始講Android中的布局七婴,Android中有六大布局,分別是: LinearLayout(線性布局),RelativeLayout(相對(duì)布局)拟逮,TableLayout(表格布局) FrameLayout(幀布局)韩玩,AbsoluteLayout(絕對(duì)布局),GridLayout(網(wǎng)格布局) 而今天我們要講解的就是第一個(gè)布局损谦,LinearLayout(線性布局)岖免,我們屏幕適配的使用 用的比較多的就是LinearLayout的weight(權(quán)重屬性),在這一節(jié)里,我們會(huì)詳細(xì)地解析 LinearLayout,包括一些基本的屬性
1.本節(jié)學(xué)習(xí)圖
2.weight(權(quán)重)屬性詳解:
①最簡(jiǎn)單用法:
如圖:
實(shí)現(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>
要實(shí)現(xiàn)第一個(gè)的1:1的效果,只需要分別把兩個(gè)LinearLayout的weight改成1和1就可以了 用法歸納: 按比例劃分水平方向:將涉及到的View的android:width屬性設(shè)置為0dp,然后設(shè)置為android weight屬性設(shè)置比例即可;類推,豎直方向,只需設(shè)android:height為0dp,然后設(shè)weight屬性即可岳颇! 大家可以自己寫個(gè)豎直方向的等比例劃分的體驗(yàn)下簡(jiǎn)單用法照捡!
②weight屬性詳解:
當(dāng)然,如果我們不適用上述那種設(shè)置為0dp的方式,直接用wrap_content和match_parent的話, 則要接著解析weight屬性了,分為兩種情況,wrap_content與match_parent!另外還要看 LinearLayout的orientation是水平還是豎直,這個(gè)決定哪個(gè)方向等比例劃分
1)wrap_content比較簡(jiǎn)單,直接就按比例的了
實(shí)現(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):這個(gè)則需要計(jì)算了
我們寫這段簡(jiǎ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" >
<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)行效果圖:
這個(gè)時(shí)候就會(huì)有疑問了,怎么會(huì)這樣,這比例是2:1吧,那么three去哪了话侧?代碼里面明明有 three的啊,還設(shè)置了3的,而1和2的比例也不對(duì)耶,1:2:3卻變成了2:1:0,怎么會(huì)這樣呢? 答:這里其實(shí)沒那么簡(jiǎn)單的,還是需要我們計(jì)算的,網(wǎng)上給出的算法有幾種,這里就給出筆者 覺得比較容易理解的一種: step 1:個(gè)個(gè)都是fill_parent,但是屏幕只有一個(gè)啦,那么1 - 3 = - 2 fill_parent step 2:依次比例是1/6,2/6,3/6 step 3:先到先得,先分給one,計(jì)算: 1 - 2 * (1/6) = 2/3 fill_parent 接著到two,計(jì)算: 1 - 2 * (2/6) = 1/3 fill_parent 最后到three,計(jì)算 1 - 2 * (3/6) = 0 fill_parent step 4:所以最后的結(jié)果是:one占了兩份,two占了一份,three什么都木有 以上就是為什么three沒有出現(xiàn)的原因了,或許大家看完還是有點(diǎn)蒙,沒事,我們舉多幾個(gè)例子試試就知道了栗精!
比例為:1:1:1
按照上面的計(jì)算方法算一次,結(jié)果是:1/3 1/3 1/3,沒錯(cuò)
接著我們?cè)僭囅?2:3:4
計(jì)算結(jié)果:5/9 3/9 1/9,對(duì)比效果圖,5:3:1,也沒錯(cuò),所以這個(gè)計(jì)算方法你可得mark下了!
③Java代碼中設(shè)置weight屬性:
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1));
注意
基本上Linearlayout的布局差不多只有這些,這里需要注意的是
當(dāng) android:orientation="vertical" 時(shí)瞻鹏, 只有水平方向的設(shè)置才起作用悲立,垂直方向的設(shè)置不起作用。 即:left新博,right薪夕,center_horizontal 是生效的。 當(dāng) android:orientation="horizontal" 時(shí)赫悄, 只有垂直方向的設(shè)置才起作用原献,水平方向的設(shè)置不起作用。 即:top埂淮,bottom姑隅,center_vertical 是生效的。
當(dāng)需要設(shè)置控件一個(gè)在左一個(gè)在右倔撞,筆者建議大家使用 RelativeLayout!