參考資料:
(1)http://xjobinternship.blogspot.com/2014/07/tablelayout.html
(2)http://pianovv510.blogspot.com/2013/07/android-tablelayout.html
(3)https://kknews.cc/zh-tw/news/9jvjp65.html
本例中因為要讓TableLayout更好顯示在每一列所以在最外層用LinearLayout包起來,所以當我們在裡面寫了第二個TableLayout則會是在LinearLayout中的第二列去作顯示,透過這種方式可以很簡單得來排版我們常用的小算盤。
TableLayout是一個類似表格排列方式的layout,TableLayout使用TableRow將內容分行,TableLayout主要的功能有下面這幾個項目:
- android:stretchColumns:將指定的欄位填滿剩餘的空間,可以用*代表是全部的欄位
- android:shrinkColumns:將指定的欄位縮小空間,可以用*代表是全部的欄位
- android:collapseColumns:將指定的欄位進行刪除
- android:layout_span:合併欄位的格數
- android:layout_column:指定欄位的編號
結果參考http://xjobinternship.blogspot.com/2014/07/tablelayout.html
vertical
需要注意的是TableRow不需要設置寬度layout_width和高度layoutJheight,其寬度一定是match_parent,即自動填充父容器,高度一定為wrap_content,即根據內容改變高度。
但對於TableRow中的其他控制項來說,是可以設置寬度和高度的,但必其須是 wrap_content 或者 fill_parent。
相同高度的例子: https://stackoverflow.com/questions/13738630/table-layout-with-equal-rows-height
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="4" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#B0B0B0" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="vertical"
android:weightSum="4" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#B0B0B0" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="vertical"
android:weightSum="4" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#B0B0B0" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="vertical"
android:weightSum="4" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#B0B0B0" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="vertical"
android:weightSum="4" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#B0B0B0" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="vertical"
android:weightSum="4" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#B0B0B0" />
</TableRow>
</TableLayout>
</LinearLayout>
另一個例子 https://stackoverflow.com/questions/5794499/tablelayout-with-different-columns-at-different-rows?rq=1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/color_label"
android:layout_width="12dip"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:background="#ffffff" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:id="@+id/toptext"
android:gravity="left"
/>
<TextView
android:id="@+id/bottomtext"
android:gravity="right"
/>
</TableRow>
<View android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<TextView
android:id="@+id/col1"
android:gravity="left"
android:text="Column 1"
/>
<TextView
android:id="@+id/col2"
android:gravity="right"
android:text="Column 2"
/>
<TextView
android:id="@+id/col3"
android:gravity="left"
android:text="Column 3"
/>
<TextView
android:id="@+id/col4"
android:gravity="right"
android:text="Column 4"
/>
</TableRow>
<View android:layout_height="2dip"
android:background="#FF909090" />
</TableLayout>
</LinearLayout>
Android LinearLayout with weight
http://devdevelop.com/android-ui-design/
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="eng.devdevelop.com.uidemostackedpercentage.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:textColor="@color/colorWhite" android:layout_marginBottom="@dimen/activity_vertical_margin" android:background="@color/colorGrey" android:text="20%" /> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:textColor="@color/colorWhite" android:background="@color/colorGrey" android:layout_marginBottom="@dimen/activity_vertical_margin" android:text="20%" /> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="6" android:textColor="@color/colorWhite" android:background="@color/colorGrey" android:layout_marginBottom="@dimen/activity_vertical_margin" android:text="60%" /> </LinearLayout>
The total number of weights is
2 + 2 + 6 = 10.
So the first button view occupies 2/10 or 20% of the screen vertically.
In turn the second button view occupies 20% (vertically)of the space allocated for all the buttons and the last button occupies 60 % (vertically)of the screen space.
原文網址:https://kknews.cc/zh-tw/news/9jvjp65.html