android LinearLayout 安卓無憂第一篇

java學習手冊,最全java資料


今天給大家介紹一下安卓佈局中的 LinearLayout,文章分爲四個部分

 線性佈局是一個很常用的佈局,可以讓它的子元素垂直或水平的方式排成一行,但是在我的工作中只在佈局結構簡單的時候使用,很多時候是使用相對佈局的。因爲如果佈局結構複雜時使用線性佈局往往需要LinearLayout嵌套LinearLayout,而這會使得加載頁面的效率變低。所以寫佈局文件之前要好好分析需求,再決定是否使用LinearLayout。

1 常用屬性

android:orientation

android:orientation="vertical"垂直線性佈局,"horizontal"水平線性佈局。

android:gravity="top"(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal)控制佈局中控件的對齊方式。如果是沒有子控件的控件設置此屬性,表示其內容的對齊方式,比如說TextView裏面文字的對齊方式;若是有子控件的控件設置此屬性,則表示其子控件的對齊方式,gravity如果需要設置多個屬性值,需要使用“|”進行組合

android:baselineAligned

默認爲true,當android:orientation=“horizontal ”時,其子View的baseline 是對齊的,表現在不同View(TextView、Button、EditView)顯示的文字的baseline是對齊的。

效果:上排爲true時的效果


android:divider

在每個子View間加分隔線。可設置爲顏色值(#rgb","#argb", "#rrggbb", or "#aarrggbb")或資源引用

“@[+][package:]type:name" 或屬性引用"?[package:][type:]name",需android:showDividers不爲none時纔會顯示

android:showDividers默認爲none,設置分割線顯示位置,可選項包含:none、beginning、middle、end。其中middle較常用

效果:此爲middle的效果



2 gravity 與layout_gravity的區別

android:gravity是指定本元素的子元素相對它的對齊方式。

android:layout_gravity是指定本元素相對它的父元素的對齊方式。

3 android:layout_weight

在android開發中LinearLayout很常用,LinearLayout的內控件的android:layout_weight在某些場景顯得非常重要,比如我們需要按比例顯示。android並沒用提供table這樣的控件,雖然有TableLayout,但是它並非是我們想象中的像html裏面的table那麼好用,我們常用ListView實現table的效果,但是列對齊確比較麻煩,現在用LinearLayout及屬性android:layout_weight能很好地解決。下面我們共同體驗下layout_weight這個屬性。

  LinearLayout內的控件的layout_width設置爲"wrap_content",效果如下:


可以看到這三個TextView是按照1:2:3的比例進行顯示的,這樣看來似乎可以實現按照比例顯示了,但是有個問題,如果TextView內的文本長度一同那麼較長文本的TextView會寬度會有所增加,見下面配置及效果:

配置如下:

<LinearLayout
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:orientation="horizontal">
<TextView
        android:background="#aa0000"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:text="1"/>

<TextView
        android:background="#00aa00"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:layout_width="wrap_content"
        android:text="1"/>

<TextView
        android:background="#0000aa"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:text="1"/>
</LinearLayout>

效果如下:


這樣看來我們所需要的按比例又無法實現了。

只有LinearLayut中子view將layout_width設置爲"0dp"。配置及效果見下:

<LinearLayout
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:orientation="horizontal">
<TextView
        android:background="#aa0000"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:text="1111111111111111111111111111111111111111111"/>

<TextView

        android:background="#00aa00"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:text="2"/>

<TextView
        android:background="#0000aa"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        android:layout_width="0dp"
        android:text="3"/>

</LinearLayout>

效果如下,這樣終於達到我們的按比例顯示的效果了。 


4 安卓無憂例子

首先打開安卓無憂,界面如下,


進入佈局管理,點擊LinearLayout,界面如下:






點擊源碼查看源碼界面如下




最後,請去應用寶下載安卓無憂,源碼例子文檔一網打盡:源碼下載。百度網盤地址:http://yun.baidu.com/share/link?shareid=996618543&uk=1000858045

本人微博:honey_11



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章