一個好的應用不僅僅功能強,還要在介面上花了一番功夫,設計越好看,使用者體驗增加了一番或者加動畫那就更好不過了.瞭解布局就必須知道五大布局:
線性布局(LinearLayout),相對布局(RelativeLayout),幀布局(FrameLayout),絕對布局(AbsoluteLayout),表格版面配置(TableLayout)
目前用的最多前兩個:線性,相對布局,前者特點是:它將控制群組織在一個垂直或水平的形式。當布局方向設定為垂直時,它裡面的所有子控制項被組織在同一列中;當布局方向設定為水平時,所有子控制項被組織在一行中,後者特點為可以調整方向(左),(水平垂直)(右)對齊,幀布局有點像網頁,絕對布局已經目前沒什麼人用了。表格版面配置,顧名思義就是用表格顯示布局,只不過表格你是看不見的。怎麼把以上布局用的熟練了。我做了一個demo,:
這個怎麼做的呢?我可以回答你用相對布局,但總體布局還是線性。而且不是寫在同一個布局裡,二是分開寫,分開寫好處在於減少代碼的重複性而已。所以我建立了三個布局,一個頂部的,底部,最後主介面的。讓我們看看代碼,首先是頭部:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:background="@drawable/top" android:layout_width="fill_parent" android:layout_height="@dimen/main_top"> <TextView android:text="測試" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal|center_vertical" android:textSize="19sp" android:textColor="@android:color/background_dark" android:layout_centerVertical="true" android:layout_centerHorizontal="true"></TextView> </RelativeLayout>
你可以看到頭部是相對作為命名空間,方向是水平,而且高度控制在40-50dip之內,設定wrap_content會覺得很大,所以縮小高度,下面只要文字就行,哪怕你拖進也行。接著是底部:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:background="@drawable/list_bottombar_bg" android:layout_width="fill_parent"android:layout_height="@dimen/main_bottom"> <ImageView android:layout_width="105dip" android:layout_height="wrap_content" android:background="@drawable/list_bottombar_local" android:layout_alignParentLeft="true"/> <ImageView android:layout_width="105dip" android:layout_height="wrap_content" android:background="@drawable/list_bottombar_favorite" android:layout_centerHorizontal="true"/> <ImageView android:layout_width="105dip" android:layout_height="wrap_content" android:background="@drawable/list_bottombar_online" android:layout_alignParentRight="true"/> </RelativeLayout>
還是和頭部一樣,相對布局扔三個ImageView,寬度105dip是估算的,並非是精確計算的。三張圖左,中,右代碼想必你看的夠清楚了吧。最後是主介面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"><!--用include把布局包圍起來--> <include android:layout_width="wrap_content" android:layout_height="@dimen/main_top" layout="@layout/main_top"/> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/listview" android:cacheColorHint="#ffffffff"> </ListView> <include android:layout_width="wrap_content" android:layout_height="@dimen/main_bottom" layout="@layout/main_bottom"/></LinearLayout>
以上可以看到一個新的東西include,什麼是include?翻過來意思是包括,就是要一個布局寫好包進來,省去代碼的繁瑣,看!頂部用include包圍起來,裡面定義寬高(必須要),然後"@layout"這個尋找要包圍的布局,同理,底部也是,中間的listview大家都懂的。OK,這個布局介紹到這裡了。
本例子源碼在這:http://files.cnblogs.com/feifei1010/ListViewLayout.zip
歡迎熱愛Android開發的朋友們加入群一起交流~~成都252743807 廣州252743081