今天學習android界面設計的layout,記下過程,相當於是做筆記了。
首先是Framelayout:
Framelayout,字面意思上理解是“框架佈局”。特點是 每個佈局框架裏頭的元素都是在屏幕的左上角開始擺放,如果不止一個控件的話就相互重疊,後面的將前面的對象元素給覆蓋掉。如果後面的對象是透明的話就可以看見前面的對象,不然後面的對象就會把前面的對象給覆蓋。下面是例子:main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="hello ,this is a Button!"
/>
<Button
android:layout_width="125sp"
android:layout_height="wrap_content"
android:text="this is the second button"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="#00aa00"
/>
</FrameLayout>
然後是Linearlayout:即相對佈局。下面是main.xml文件根據xml佈局文件就可以看出其用法。<!-- -->是xml文件的註釋形式,這個比較重要,剛開始的時候還以爲和.java文件裏頭的註釋是一樣的,鬧了不少笑話。用//註釋老是編譯不過!悲劇了
<?xml version="1.0" encoding="utf-8"?>
<!--
<LinearLayout>
線性版面配置,在這個標籤中,所有元件都是按由上到下的排隊排成的
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- android:orientation="vertical" 表示豎直方式對齊
android:orientation="horizontal"表示水平方式對齊
android:layout_width="fill_parent"定義當前視圖在屏幕上
可以消費的寬度,fill_parent即填充整個屏幕。
android:layout_height="wrap_content":隨着文字欄位的不同
而改變這個視圖的寬度或者高度。有點自動設置框度或者高度的意思
layout_weight 用於給一個線性佈局中的諸多視圖的重要度賦值。
所有的視圖都有一個layout_weight值,默認爲零,意思是需要顯示
多大的視圖就佔據多大的屏幕空 間。若賦一個高於零的值,則將父視
圖中的可用空間分割,分割大小具體取決於每一個視圖的layout_weight
值以及該值在當前屏幕布局的整體 layout_weight值和在其它視圖屏幕布
局的layout_weight值中所佔的比率而定。
舉個例子:比如說我們在 水平方向上有一個文本標籤和兩個文本編輯元素。
該文本標籤並無指定layout_weight值,所以它將佔據需要提供的最少空間。
如果兩個文本編輯元素每一個的layout_weight值都設置爲1,則兩者平分
在父視圖佈局剩餘的寬度(因爲我們聲明這兩者的重要度相等)。如果兩個
文本編輯元素其中第一個的layout_weight值設置爲1,而第二個的設置爲2,
則剩餘空間的三分之二分給第一個,三分之一分給第二個(數值越小,重要
度越高)。
-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
我是初學,在網上看到有大蝦說在android開發中這個linearlayout是用得比較多的,拭目以待吧,看起來是要靈活一些哦。
下面是Relativelayout:相對佈局。xml文件如下面所示。
main.xml文件代碼:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:"
android:textSize="15sp"
/>
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_below="@id/label"
android:layout_marginBottom="10dp"
android:text="在此輸入文本..."
/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_alignParentRight="true"
android:layout_below="@id/entry"
android:layout_marginLeft="5dp"
/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel"
/>
</RelativeLayout>
在相對佈局中用得比較多的就我感覺就是相對屏幕位置左邊呀,或者右邊,頂部,等的位置。先記下來,以後在一步一步的研究,感覺這個在應用中還是蠻靈活的!
下面一個就是TableLayout,話不多說。現將xml文件和運行結果貼出來再說。
下面是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="0,1,2"
>
<TableRow>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello, i am button1"
android:layout_column="0"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello, i am button2"
android:layout_column="1"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello, i am button3"
android:layout_column="1"
/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello, i am button4"
android:layout_column="2"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello, i am button5"
android:layout_column="2"
/>
</TableRow>
</TableLayout>
根據運行結果可以看出,每一個TableRow中的Button都是按照水平方向排列的,並且每個Button的位置可以用android:layout_column="2"來定義,其中參數表示位置,並且從0開始,如果是0就從屏幕的最左邊開始排列,並且以此向後面排列。
最後就是AbsoluteLayout:絕對佈局。此種佈局形式是採用座標的形式來排列activity中的對象。下面是xml文件:
main.xml:<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:text="input your name..."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_x="370px"
android:layout_y="65px"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="OK"
/>
</AbsoluteLayout>
運行結果截圖:
以上就是android界面設計的5大布局哦!android學習必經之路!呵呵