TableLayout使用表格的方式劃分子組件。

TableLayout的共有XML屬性繼承自:Component。
TableLayout的自有XML屬性見(jiàn)下表:
| 屬性名稱 | 中文描述 | 取值 | 取值說(shuō)明 | 使用案例 |
|---|---|---|---|---|
| alignment_type | 對(duì)齊方式 | align_edges | 表示TableLayout內(nèi)的組件按邊界對(duì)齊。 | ohos:alignment_type="align_edges" |
| align_contents | 表示TableLayout內(nèi)的組件按邊距對(duì)齊。 | ohos:alignment_type="align_contents" | ||
| column_count | 列數(shù) | integer類型 | 可以直接設(shè)置整型數(shù)值,也可以引用integer資源。 |
ohos:column_count="3" ohos:column_count="$integer:count" |
| row_count | 行數(shù) | integer類型 | 可以直接設(shè)置整型數(shù)值,也可以引用integer資源。 |
ohos:row_count="2" ohos:row_count="$integer:count" |
| orientation | 排列方向 | horizontal | 表示水平方向布局。 | ohos:orientation="horizontal" |
| vertical | 表示垂直方向布局。 | ohos:orientation="vertical" |
在XML中創(chuàng)建TableLayout,示例代碼如下:
在graphic文件夾下創(chuàng)建Text的背景table_text_bg_element.xml,示例代碼如下:
ohos:radius="5vp"/>
在TableLayout布局中添加子組件。
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#87CEEB"
ohos:padding="8vp">
ohos:height="60vp"
ohos:width="60vp"
ohos:background_element="$graphic:table_text_bg_element"
ohos:margin="8vp"
ohos:text="1"
ohos:text_alignment="center"
ohos:text_size="20fp"/>
TableLayout默認(rèn)一列多行。

設(shè)置行列數(shù):
...
ohos:row_count="2"
ohos:column_count="2">
設(shè)置TableLayout的行為2,列為2效果。

在XML中設(shè)置布局排列方向,以“vertical”為例:
...
ohos:orientation="vertical">
...
設(shè)置布局排列方向?yàn)椤皏ertical”的效果。

TableLayout提供兩種對(duì)齊方式,邊距對(duì)齊“align_contents”、邊界對(duì)齊“align_edges”,默認(rèn)為邊距對(duì)齊“align_contents”。代碼如下:
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_content"
ohos:width="match_content"
ohos:alignment_type="align_contents"
ohos:background_element="$graphic:layout_borderline"
ohos:column_count="3"
ohos:padding="8vp">
ohos:height="48vp"
ohos:width="48vp"
ohos:background_element="$graphic:table_text_bg_element"
ohos:margin="8vp"
ohos:padding="8vp"
ohos:text="1"
ohos:text_alignment="center"
ohos:text_size="14fp"/>
邊距對(duì)齊效果:

將TableLayout的對(duì)齊方式修改為邊界對(duì)齊。
...
ohos:alignment_type="align_edges">
...
邊界對(duì)齊效果:

引用graphic文件夾下的背景資源文件為layout_borderline.xml,示例代碼如下:
TableLayout合并單元格的效果可以通過(guò)設(shè)置子組件的行列屬性來(lái)實(shí)現(xiàn)。
設(shè)置子組件的行列屬性均為2的效果展示:

在XML中創(chuàng)建TableLayout,并添加子組件,代碼如下:
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_content"
ohos:width="match_content"
ohos:alignment_type="align_edges"
ohos:background_element="$graphic:layout_borderline"
ohos:column_count="3"
ohos:padding="8vp"
ohos:row_count="3">
ohos:id="$+id:text_one"
ohos:height="48vp"
ohos:width="48vp"
ohos:background_element="$graphic:table_text_bg_element"
ohos:margin="16vp"
ohos:padding="8vp"
ohos:text="1"
ohos:text_alignment="center"
ohos:text_size="14fp"/>
在Java代碼中設(shè)置子組件的行列屬性,代碼如下:
@Override
protected void onStart(Intent intent) {
...
Component component = findComponentById(ResourceTable.Id_text_one);
TableLayout.LayoutConfig tlc = new TableLayout.LayoutConfig(vp2px(72), vp2px(72));
tlc.columnSpec = TableLayout.specification(TableLayout.DEFAULT, 2);
tlc.rowSpec = TableLayout.specification(TableLayout.DEFAULT, 2);
component.setLayoutConfig(tlc);
}
private int vp2px(float vp) {
return AttrHelper.vp2px(vp, getContext());
}
在設(shè)置子組件的行列屬性時(shí),TableLayout剩余的行數(shù)和列數(shù)必須大于等于該子組件所設(shè)置的行數(shù)和列數(shù)。
目前僅支持Java代碼設(shè)置TableLayout子組件的行列屬性。
在創(chuàng)建子組件的行列屬性時(shí),還可設(shè)置子組件的對(duì)齊方式,修改上述Java代碼如下:
@Override
protected void onStart(Intent intent) {
...
tlc.columnSpec = TableLayout.specification(TableLayout.DEFAULT, 2, TableLayout.Alignment.ALIGNMENT_FILL);
tlc.rowSpec = TableLayout.specification(TableLayout.DEFAULT, 2, TableLayout.Alignment.ALIGNMENT_FILL);
...
}
子組件的對(duì)齊方式設(shè)置為ALIGNMENT_FILL的效果:

設(shè)置子組件的權(quán)重,代碼如下:
@Override
protected void onStart(Intent intent) {
...
TableLayout.LayoutConfig tlc = new TableLayout.LayoutConfig(0, vp2px(48));
tlc.columnSpec = TableLayout.specification(TableLayout.DEFAULT, 1, 1.0f);
tlc.rowSpec = TableLayout.specification(TableLayout.DEFAULT, 1);
findComponentById(ResourceTable.Id_text_one).setLayoutConfig(tlc);
findComponentById(ResourceTable.Id_text_two).setLayoutConfig(tlc);
findComponentById(ResourceTable.Id_text_three).setLayoutConfig(tlc);
findComponentById(ResourceTable.Id_text_four).setLayoutConfig(tlc);
findComponentById(ResourceTable.Id_text_five).setLayoutConfig(tlc);
findComponentById(ResourceTable.Id_text_six).setLayoutConfig(tlc);
}
上述代碼將子組件的寬度權(quán)重設(shè)置為1.0,每行子組件會(huì)均分TableLayout的寬度,所以需要設(shè)置TableLayout為固定寬度或match_parent。
ohos:width="match_parent"
...>
ohos:id="$+id:text_one"
.../>
將子組件的寬度權(quán)重設(shè)置為1.0的效果展示:

審核編輯:湯梓紅
-
JAVA
+關(guān)注
關(guān)注
20文章
3001瀏覽量
116448 -
XML
+關(guān)注
關(guān)注
0文章
188瀏覽量
34536 -
框架
+關(guān)注
關(guān)注
0文章
404瀏覽量
18425 -
HarmonyOS
+關(guān)注
關(guān)注
80文章
2153瀏覽量
36060
原文標(biāo)題:HarmonyOS學(xué)習(xí)路之開(kāi)發(fā)篇—Java UI框架(Table Layout)
文章出處:【微信號(hào):美男子玩編程,微信公眾號(hào):美男子玩編程】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
【HarmonyOS HiSpark AI Camera試用連載 】鴻蒙JS UI介紹
請(qǐng)教鴻蒙應(yīng)用開(kāi)發(fā)JAVA UI 框架ProgressBar或者RoundProgressBar怎么實(shí)現(xiàn)滑動(dòng)調(diào)節(jié)
基于HarmonyOS Java UI使用元數(shù)據(jù)綁定框架實(shí)現(xiàn)UI和數(shù)據(jù)源的綁定
基于HarmonyOS Java UI,使用元數(shù)據(jù)綁定框架,實(shí)現(xiàn)UI和數(shù)據(jù)源的綁定
鴻蒙應(yīng)用開(kāi)發(fā)的JS UI框架如何實(shí)現(xiàn)高德地圖的訪問(wèn)?
HarmonyOS自動(dòng)化測(cè)試框架—Hypium
4天帶你上手HarmonyOS ArkUI開(kāi)發(fā)——《HarmonyOS ArkUI入門(mén)訓(xùn)練營(yíng)之健康生活實(shí)戰(zhàn)》
在HarmonyOS版本下如何基于JS UI框架來(lái)開(kāi)發(fā)?
HarmonyOS JS應(yīng)用開(kāi)發(fā)需要關(guān)注哪些線程?官方解析來(lái)啦~
華為開(kāi)發(fā)者HarmonyOS零基礎(chǔ)入門(mén):HarmonyOS UI編程框架快速上手
華為開(kāi)發(fā)者HarmonyOS零基礎(chǔ)入門(mén):UI組件設(shè)計(jì)開(kāi)發(fā)實(shí)踐
HarmonyOS測(cè)試技術(shù)與實(shí)戰(zhàn)-分布式UI測(cè)試框架
全面解讀HarmonyOS新一代UI框架
HarmonyOS應(yīng)用開(kāi)發(fā)之Java UI框架
評(píng)論