步步爲營_Android開發課[14]_用戶界面之Layout(佈局)

Focus on technology, enjoy life!—— QQ:804212028
瀏覽鏈接:http://blog.csdn.net/y18334702058/article/details/44624305


  • 主題:用戶界面之Layout(佈局)
    -在Android開發中我們有傳說中的5大布局,它們的結合使用,畫出了APP界面的條條框框。

Android中常用的5大布局:
線性佈局(LinearLayout):按照垂直或者水平方向佈局的組件。
幀佈局(FrameLayout):組件從屏幕左上方佈局組件。
表格佈局(TableLayout):按照行列方式佈局組件。
相對佈局(RelativeLayout):相對其它組件的佈局方式。
絕對佈局(AbsoluteLayout):按照絕對座標來佈局組件。

各種佈局之間的關係圖:

這裏寫圖片描述

1.線性佈局(LinearLayout):

線性佈局是Android開發中最常見的一種佈局方式,它是按照垂直或者水平方向來佈局,通過“android:orientation”屬性可以設置線性佈局的方向。屬性值有垂直(vertical)和水平(horizontal)兩種。

main.xml源代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout 
         android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"        
        >
        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:background="#005588"
            android:layout_weight="1"
            />
        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:background="#337799"
            android:layout_weight="1"
            />  
        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:background="#227508"
            android:layout_weight="1"
            />
        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:background="#545729"
            android:layout_weight="1"
            />  
    </LinearLayout>

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:background="#234235"
            />
        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:background="#865321"
            />  
    </LinearLayout>

</LinearLayout>

運行結果:

這裏寫圖片描述

2.幀佈局(FrameLayout):

幀佈局是從屏幕的左上角(0,0)座標開始佈局,多個組件層疊排列,第一個添加的組件放到最底層,最後添加到框架中的視圖顯示在最上面。上一層的會覆蓋下一層的控件。

mian.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" 
    > 
    <TextView    
        android:layout_width="300dp"   
        android:layout_height="300dp"   
        android:background="#114488"          
        /> 
    <TextView    
        android:layout_width="250dp"   
        android:layout_height="250dp"   
        android:background="#225599"          
        /> 
    <TextView    
        android:layout_width="200dp"   
        android:layout_height="200dp"   
        android:background="#337700"          
        /> 
</FrameLayout> 

運行結果:

這裏寫圖片描述

3.表格佈局(TableLayout):

表格佈局是一個ViewGroup以表格顯示它的子視圖(view)元素,即行和列標識一個視圖的位置。
表格佈局常用的屬性如下:
android:collapseColumns:隱藏指定的列
android:shrinkColumns:收縮指定的列以適合屏幕,不會擠出屏幕
android:stretchColumns:儘量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:該控件所跨越的列數

mian.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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" 
            android:layout_column="0"
            /> 
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2" 
            android:layout_column="1"
            /> 

    </TableRow> 
    <TableRow> 
       <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3" 
            android:layout_column="1"
            /> 
       <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button4" 
            android:layout_column="2"
            /> 
    </TableRow> 

</TableLayout> 

運行結果:

這裏寫圖片描述

4.相對佈局(RelativeLayout):

相對佈局是按照組件之間的相對位置來佈局,比如相對於某個組件的上,下,左,右或者相對於父佈局的上,下,左,右。

mian.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="wrap_content" 
    android:padding="10px" 
    > 
    <TextView    
        android:id="@+id/tev1" 
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:layout_marginBottom="30dp" 
        android:text="嘟嘟跑哪裏去了:" 
        /> 
    <EditText 
        android:id="@+id/tx1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/tev1" 
        /> 
    <Button 
        android:id="@+id/btn1" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_below="@id/tx1" 
        android:layout_alignParentRight="true" 
        android:text="確定" 
        /> 
    <Button 
        android:id="@+id/btn2" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_below="@id/tx1" 
        android:layout_toLeftOf="@id/btn1" 
        android:layout_marginRight="30dp" 
        android:text="取消" 
        /> 
</RelativeLayout> 

運行結果:

這裏寫圖片描述

絕對佈局(AbsoluteLayout):

絕對佈局不能滿足適配各種不同尺寸的機型,逐漸地被淘汰,已經沒人使用了喲!大家不用學了喲,好高興^_^!

Focus on technology, enjoy life!—— QQ:804212028
瀏覽鏈接:http://blog.csdn.net/y18334702058/article/details/44624305

發佈了81 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章