Android 第八課——UI佈局2

Android佈局分爲:線性佈局、相對佈局、表格佈局、幀佈局、網格佈局五種android

1)FrameLayout(幀佈局)佈局

幀佈局是最簡單的佈局對象,它被定製爲用戶屏幕上的一個空白備用區域,以後用戶能夠在其中填充一個單一對象,例如一張圖片等。全部的子元素將會固定在屏幕左上角;咱們不能爲FrameLayout中的一個子元素指定一個位置。並且新增的子元素將會直接覆蓋填充舊的子元素,相似於一個棧結構,固然也不必定是所有擋住,這樣看透明度以及大小來決定。學習

<FrameLayout xmlns:android=
"http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:foreground="#ff0000"
> 
<TextView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/loginName" 
android:textSize="@dimen/dp50"/> 
<TextView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/loginName" 
android:textSize="@dimen/dp100"/> 
</FrameLayout>

記住一些特性:
1)全部的組件都會放置於這塊區域的左上角;
2)幀佈局的大小由子控件中最大的子控件決定,
3)若是都組件都同樣大的話,同一時刻就只能能看到最上面的那個組件了!緣由是覆蓋
4)foreground="#ff0000" 前景色,指定的顏色或圖片永遠覆蓋其餘的  
5)foregroundGravity="fill_vertical" 前景色的位置,這個屬性具體我沒弄懂ui

2)GridLayout(網格佈局)spa

網格佈局是Android4.0以後新增的一中佈局方式,他與TableLayout以及LinearLayout在操做上有點神似,固然,他天然有他的過人之處,主要就是在LinearLayout基礎上新增一些屬性,更易於使用。使用當中,注意組件若是沒有標明跨行跨列那麼組件按順序默認只佔一個單元格。.net

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/GridLayout1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:rowCount="3" 
android:columnCount="2" 
android:orientation="horizontal">
  <Button  
        android:id="@+id/btn"  
        android:layout_columnSpan="2" //跨2列
        android:layout_gravity="fill" //按鈕填充兩列的空白,緣由是不須要設置layout_width layout_height
        android:text="0"/>
        ----------------
    <GridLayout>

記住一些特性:code

1)android:orientation:組件排列方式xml

2)android:layout_gravity:組件對齊方式對象

3)android:rowCount:設置行數量blog

4)android:columnCount:設置列數量

對於容器內的組件也新增了一些屬性:

1)android:layout_rowSpan:佔用行數

2)android:layout_columnSpan:佔用列數

3)android:layout_gravity = "fill"  組件填滿所橫越的整行或者整列,這個屬性對那些有跨行或跨列的組件頗有必要。

關於網格佈局,其實我也是很頭痛的,由於一開始學習沒有相關經驗,老是沒法編譯經過,後面看了一下這篇博客,因此就學習了起來。特別是在GridPanel如何在Eclipse中運行起來,這篇博客就顯的格外重要了。

下面這些內容就是轉載自博客:

http://blog.csdn.net/jianghuiquan/article/details/8299973


可能遇到的問題:

當讀者將佈局設置爲GridLayout時,會出現莫名其妙的報錯,

若是代碼語法邏輯沒有錯的話,就多是配置文件AndroidManifest.xml的問題了

由於GridLayout是android 4.0 後才推出的,API Level 爲 14

只須要將配置文件中的MinSDK改爲14或者以上版本便可,保存,問題就解決了!


請參考 :

 http://blog.csdn.net/jianghuiquan/article/details/8299973

相關文章
相關標籤/搜索