1. 程式人生 > >Android佈局-LinearLayout屬性詳解

Android佈局-LinearLayout屬性詳解

LinearLayout,其實就是線性佈局,結構層次相對來說簡單非常明瞭,只有橫和豎2條直線的方向。這裡主要記錄下LinearLayout的重要屬性,以及使用時需要注意的事項。

1.排列方式orientation


在XML佈局中:

android:orientation="vertical"//垂直排列

android:orientation="horizontal"//水平排列

注意事項:

控制LinearLayout的子控制元件內部具體的排列順序,還需要使用android:layout_gravity這個屬性。

可是當orientation為vertical時,子控制元件設定android:layout_gravity="center_vertical"或者自身設定android:gravity="center_vertical"是無效的。

同樣當orientation為horizontal時,子控制元件設定android:layout_gravity="center_horizontal"或者自身設定android:gravity="center_horizontal"是無效的。

2.擺放位置gravity

從上面可以得知:在父控制元件中android:gravity屬性和其子控制元件的android:layout_gravity效果是一樣的。

既然上面提到了gravity,下面就對它的選項(可以多選)進行下解釋:

center:居中  center_horizontal:水平居中 center_vertical:垂直居中

left:偏左  |right:偏右  (start和end:詳見 這裡有這2個屬性的解釋

bottom:偏下 |top:偏上

下面的一些沒怎麼用過:

fill:充滿容器 |fill_horizontal:水平方向充滿容器 |fill_vertical:垂直方向充滿容器

clip_horizontal:水平裁剪|clip_vertical:垂直裁剪

3.分割線showDividers


既然是設定分割線,首先我們就需要有分割線:

android:divider="@drawable/drawable"//分割線的drwable,不能直接給color(無效)

android:dividerPadding="0.5dp"//分割線高度或者寬度

分割線的Shape.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/colorAccent"/>

<size android:height="1px"/>

</shape>

注意:size必須設定,不然顯示不出

選擇的樣式(可以多選):

LinearLayout.SHOW_DIVIDER_BEGINNING;//開始的分割線

LinearLayout.SHOW_DIVIDER_MIDDLE;//中間的分割線

LinearLayout.SHOW_DIVIDER_END;//結束的分割線

LinearLayout.SHOW_DIVIDER_NONE;//沒有分割線

其他方法:

用一個View將高度或寬度設定為match_parent,另一個長度設定為0.5dp,設定一個background

來達到分割線的效果:

<View android:layout_width="match_parent"

android:layout_height="0.5dp"

android:background="@color/colorPrimary"/>

在開發中,用的這種方式,可是個人覺得上面這個方式,程式碼比較簡約.

4.基線baselineAligned


上圖中2個佈局其實都差不多,唯一的區別就是:第二個佈局的android:baselineAligned="false";

不難發現有基線的情況下,文字預設都在一條直線上,這樣我們有時候就會有佈局的麻煩,解決方法當然就是設定基線為false;

同時,我們可以利用基線來佈局如佈局二的樣式,是不是感覺很常見的一個底部選單欄樣式?

注意:基線只對有文字內容,如TextView,EditText,Button等才有效果。

5.權重weight


圖一的佈局:

<LinearLayout

android:layout_width="match_parent"

android:layout_height="50dp"

android:orientation="horizontal">

<TextVIew android:id="@+id/tv1"

android:layout_width="wrap_content"

android:layout_height="50dp"

android:layout_weight="1"

android:background="@color/colorPrimary"/>

<TextView android:id="@+id/tv2"

android:layout_width="wrap_content"

android:layout_height="50dp"

android:layout_weight="2"

android:background="@color/colorAccent"/>

<TextView android:id="@+id/tv3"

android:layout_width="wrap_content"

android:layout_height="50dp"

android:layout_weight="2"

android:background="@color/colorPrimaryDark"/>

</LinearLayout>

圖二的佈局:

僅僅只是將TextView的android:layout_width="match_parent";

在圖片可以看出,wrap_content情況下, tv1:tv2:tv3=1:2:2;剛好是權重的比值。

符合大家所說的權重比值,權重越多比例越大。

可是在match_parent的情況下,tv1:tv2:tv3=3:1:1,這樣明顯不是我們想要的結果。

那麼權重到底是怎麼計算的呢?

首先按照分配的長度來給予長度,剩餘的長度再按權重的比例來分配。

圖二 tv1的寬度:match_parent,我們這裡用L表示。

那麼tv2和tv3的寬度也是L。

這時候剩餘的長度呢?當然是總長度減去3個控制元件的長度,即:L-3L=-2L。

然後將這個剩餘長度再來分配:

tv1的寬度=L-(1/5)2L=(3/5)L;

tv2和tv3的寬度=L-(2/5)2L=(1/5)L;

6.原始碼地址