五大布局——表格、幀、絕對佈局

目錄

1.表格佈局(TableLayout)
2.幀佈局(FrameLayout)
3.絕對佈局

1.表格佈局(TableLayout)

TableLayout(表格佈局)
TbaleLayout的一些屬性;
xml 相關用法 說明
android:collapseColumns setColumnsCollapsed(in,boolean) 設置需要隱藏的列的序號,多個用逗號隔
android:shrinkColumns setShrinkColumns(boolean) 設置需要收縮的列的序號,多個用逗號隔
android:stretchColumns setStretchAllColumns(boolean) 設置需要伸張的列的序號,多個用逗號隔
說明:收縮和伸張的屬性可以同時使用,stretchColumns是將父控件填滿
屬性值是從0開始,0代表第一列
行數和列數的確定:是通過TableRow以及其他組件控制表格的行數和列數,具體如下:
TableLayout的行數有開發人員制定,即有多少個TableRow對象(或view控件),就有多少行
列的寬度:又該列中最寬的單元格決定,整個表格佈局的寬度取決於父容器的寬度(默認是佔滿父容器本身) 如第一個TableRow含2個控件,第二個TableRow含3個控件,那麼該TableRow的列數爲3
單元格屬性,2個參數
android:layout_column 指定單元格在第幾列顯示
android:layout_span 指定單元格佔的列數(未指定時 爲1)
且一個空間也可以同時具備這兩個特性
※示例如下:
這裏寫圖片描述

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:collapseColumns="0"//將第一列隱藏
    android:stretchColumns="1,2"//23列平均分配剩餘空間
    >


    <TableRow >><Button /><Button              android:layout_span="2"/><Button /></TableRow> //span=“2”這個button佔兩個格
    <TableRow><Button /><Button /><Button /></TableRow>
    <TableRow><Button /><Button /><Button /></TableRow>
    <TableRow><Button /><Button /><Button /></TableRow>


</TableLayout>

2.幀佈局(FrameLayout)

在這個佈局中,整個界面被當成一塊空白備用區域,所有的子元素都不能被指定放置的位置,它們統統放於這塊區域的左上角,並且後面的子元素直接覆蓋在前面的子元素之上,將前面的子元素部分和全部遮擋。

重要屬性:
android:visibility=”1.2.3”//可見性
1.visible可見的
2.invisible不可見 但還存在
3.gone 不可見,不存在了
這裏寫圖片描述

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:text="button1"
        android:background="@color/green"/>
    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:text="button2"
        android:background="@color/red"
       />

</FrameLayout>

3. 絕對佈局(AbsoluteLayout)

特點;一座標的方式來定位在屏幕上的位置,引起缺乏靈活性,在沒有絕對定位的情況下相比其他類型的佈局更難維護
在拖動控件,或有動畫的空間中常用絕對佈局;
與相對佈局的區別:
相對佈局的靈活性好,屬性複雜,程序複雜

示例

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  >
    <TextView
         android:id="@+id/lable1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="10dip"
        android:layout_y="10dip"
        android:text="inputname" />
    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_y="40dip" />
    <Button
        android:id="@+id/test1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="190dip"
        android:layout_y="100dip"
        android:width="70dip"
        android:text="OK" />
    <Button
        android:id="@+id/test2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="260dip"
        android:layout_y="100dip"
       android:width="70dip"
        android:text="Cancle" />
</AbsoluteLayout>

這裏寫圖片描述

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