【ALearning】第四章 Android Layout組件布局(二)

來源:互聯網
上載者:User

標籤:absolute   wrap   pbm   center   bsp   odi   nts   art   androi   

       前面我們分別介紹和學習了LinearLayout(線性布局)、FrameLayout(單幀布局)和AbsoluteLayout(絕對布局)。這次我們要進行RelativeLayout(相對布局)和TableLayout(表格版面配置)的學習。

這部分是非常重要的知識點。RelativeLayout是開發過程中強烈建議使用的,而TableLayout是滿足一些特定需求時(常見表格顯示,但不局限於此)須要使用。

【部落格專欄:http://blog.csdn.net/column/details/alearning.html】

RelativeLayout(相對布局)       前面介紹的LinearLayout(現象布局)與即將介紹的RelativeLayout(相對布局)。是實際開發中較常使用的,也是開發過程中強烈建議的使用。以下我們來開始瞭解RelativeLayout布局。
       相對布局。顧名思義就是以“相對”位置/對齊為基礎的布局方式。


本例拓展的屬性配置是:

  • android:id   指定該控制項唯一標誌的ID值;
  • android:layout_above    指定該控制項的底部置於給定ID的控制項之上;
  • android:layout_below    指定該控制項的底部置於給定ID的控制項之下;
  • android:layout_toLeftOf  指定該控制項的右邊緣與給定ID的控制項左邊緣對齊;
  • android:layout_toRightOf 指定該控制項的左邊緣與給定ID的控制項右邊緣對齊;
  • android:layout_alignBaseline 指定該控制項的baseline與給定ID的baseline對齊;
  • android:layout_alignTop     指定該控制項的頂部邊緣與給定ID的頂部邊緣對齊;
  • android:layout_alignBottom  指定該控制項的底部邊緣與給定ID的底部邊緣對齊;
  • android:layout_alignLeft     指定該控制項的左邊緣與給定ID的左邊緣對齊;
  • android:layout_alignRight    指定該控制項的右邊緣與給定ID的右邊緣對齊;
  • android:layout_alignParentTop指定該控制項的頂部與其父控制項的頂部對齊;
  • android:layout_alignParentBottom 指定該控制項的底部與其父控制項的底部對齊;
  • android:layout_alignParentLeft    指定該控制項的左部與其父控制項的左部對齊;
  • android:layout_alignParentRight   指定該控制項的右部與其父控制項的右部對齊;
註:相對布局的屬相設定較多,在實際的開發中須要不斷的補充與拓展。
【布局檔案】activity_relativelayout.xml
<?xml version="1.0" encoding="utf-8"?

><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 該控制項位於介面中部 --> <TextView android:id="@+id/red" android:layout_width="70dp" android:layout_height="70dp" android:layout_centerInParent="true" android:background="#CD2E57" android:gravity="center" android:text="Red" /> <!-- 該控制項的左邊緣與給定id為red控制項的左邊緣對齊 --> <TextView android:layout_width="80dp" android:layout_height="80dp" android:layout_alignLeft="@id/red" android:background="#64DB99" android:gravity="center" android:text="Green" /> <!-- 該控制項位於介面底部 同一時候 位於給定id為red控制項的下邊/右邊(即右下位置) --> <TextView android:layout_width="50dp" android:layout_height="50dp" android:layout_below="@id/red" android:layout_centerInParent="true" android:layout_marginTop="20dp" android:layout_toRightOf="@id/red" android:background="#FFFA78" android:gravity="center" android:text="Yellow" /> <!-- 該控制項位於給定id為red控制項的左邊 --> <TextView android:layout_width="60dp" android:layout_height="60dp" android:layout_alignParentBottom="true" android:layout_toLeftOf="@id/red" android:background="#148CFF" android:gravity="center" android:text="Blue" /></RelativeLayout>


效果:

TableLayout(表格版面配置)       TableLayout表格版面配置,表格版面配置類似Html裡面的Table。每個TableLayout裡面有表格行TableRow(類似於Html中Table裡的tr)。TableRow裡面能夠詳細定義每個元素,比如TextView。設定他的對齊android:gravity="center"。


本例拓展的屬性配置是:
  • android:stretchColumns    指定可伸展的列。該列能夠向行方向伸展,最多可佔領一整行。
  • android:shrinkColumns     指定可收縮的列。當該列子控制項的內容太多。已經擠滿所在行,那麼該子控制項的內容將往列方向顯示。

  • android:collapseColumns  指定要隱藏的列。

示範範例:
android:stretchColumns="0"           第0列可伸展
android:shrinkColumns="1,2"         第1,2列皆可收縮
android:collapseColumns="*"         隱藏全部行
註:列能夠同一時候具備stretchColumns及shrinkColumns屬性。若此,那麼當該列的內容N多時,將“多行”顯示其內容。(這裡不是真正的多行,而是系統依據須要自己主動調節該行的layout_height)。
  • android:layout_column    指定該儲存格在第幾列顯示
  • android:layout_span      指定該儲存格佔領的列數(未指定時,為1)
示範範例:
android:layout_column="1"    該控制項顯示在第1列
android:layout_span="2"        該控制項佔領2列
說明:一個控制項也能夠同一時候具備這兩個特性。
【布局檔案】activity_relativelayout.xml。
<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:stretchColumns="0,1,2" >    <TableRow        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <TextView            android:layout_width="wrap_content"            android:layout_height="40dp"            android:background="#6495ED"            android:padding="2dp"            android:text="文本1-1" />        <TextView            android:layout_width="wrap_content"            android:layout_height="40dp"            android:background="#B0C4DE"            android:padding="2dp"            android:text="文本1-2" />        <TextView            android:layout_width="wrap_content"            android:layout_height="40dp"            android:background="#32CD32"            android:padding="2dp"            android:text="文本1-3" />        <TextView            android:layout_width="wrap_content"            android:layout_height="40dp"            android:background="#FFD700"            android:padding="2dp"            android:text="文本1-4" />    </TableRow>    <TableRow        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <TextView            android:layout_height="40dp"            android:layout_column="1"            android:layout_span="2"            android:background="#FF8C00"            android:text="跨列文本2-2,3跨2到3列" />    </TableRow>    <TableRow        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <TextView            android:layout_height="40dp"            android:layout_column="0"            android:layout_span="2"            android:background="#FF69B4"            android:text="跨列文本3-1,2跨1到2列" />    </TableRow>    <!--  -->    <TableLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:collapseColumns="2"         android:shrinkColumns="1"        android:stretchColumns="0" >        <TableRow            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <TextView                android:background="#696969"                 android:textColor="@android:color/white"                android:text="行方向伸展文本,可自行增長文本查看效果!" />            <TextView                 android:textColor="@android:color/white"                android:background="#800000"                android:text="列方向伸展文本,可自行增長文本查看效果!" />        </TableRow>    </TableLayout></TableLayout>

效果:

        到此,基於XML配置的五種經常使用的布局。已經所有解說完畢。可是布局的設計方式不止一種,另一種是基於Java(Android)開發語言的設定。

這部分。將會在接下來的不斷的學習中瞭解與使用。

敬請各位看官的繼續關注。


參考資料4、http://blog.sina.com.cn/s/blog_63c66eb60100u29p.html
5、http://blog.csdn.net/beyond0525/article/details/8841139

【ALearning】第四章 Android Layout組件布局(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.