歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android--表格布局

Android--表格布局

日期:2017/3/1 10:01:49   编辑:Linux編程

表格布局:是一個ViewGroup以表格顯示它的子視圖(view)元素,即行和列標識一個視圖的位置。其實Android的表格布局跟HTML中的表格布局非常類似,TableRow 就像HTML表格的<tr>標記。

用表格布局需要知道以下幾點:

android:shrinkColumns,對應的方法:setShrinkAllColumns(boolean),作用:設置表格的列是否收縮(列編號從0開始,下同),多列用逗號隔開(下同),如android:shrinkColumns="0,1,2",即表格的第1、2、3列的內容是收縮的以適合屏幕,不會擠出屏幕。

android:collapseColumns,對應的方法:setColumnCollapsed(int,boolean),作用:設置表格的列是否隱藏

android:stretchColumns,對應的方法:setStretchAllColumns(boolean),作用:設置表格的列是否拉伸

看下面的res/layour/main.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"><!-- have an eye on ! -->
<TableRow><!-- row1 -->
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a 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 a Button2"
android:layout_column="1"
/>
</TableRow>
<TableRow><!-- row2 -->
<Button android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a 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 a Button4"
android:layout_column="1"
/>
</TableRow>
<TableRow>
<Button android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button5"
android:layout_column="2"
/>
</TableRow>
</TableLayout>

運行之後可以得出下面的結果:

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved