"""Offline deterministic verifier for Android CH18 (tutorial-tablelayout)."""
from __future__ import annotations
import json
import xml.etree.ElementTree as ET
ANDROID_NS = "{http://schemas.android.com/apk/res/android}"
def parse_col_spec(spec: str, ncols: int) -> set[int]:
spec = spec.strip()
if spec == "*":
return set(range(ncols))
return {int(x.strip()) for x in spec.split(",") if x.strip()}
def case_rows_cols_and_collapse() -> dict:
xml_text = """<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:collapseColumns="0,2">
<TableRow>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="one" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="two" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="three" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="four" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="five" />
</TableRow>
</TableLayout>"""
root = ET.fromstring(xml_text)
rows = list(root)
cells = [c.attrib[f"{ANDROID_NS}text"] for c in rows[0]]
collapsed = parse_col_spec(root.attrib[f"{ANDROID_NS}collapseColumns"], len(cells))
visible = [txt for idx, txt in enumerate(cells) if idx not in collapsed]
actual = {
"row_count": len(rows),
"raw_col_count": len(cells),
"collapsed_indices": sorted(collapsed),
"visible_buttons": visible,
}
expected = {
"row_count": 1,
"raw_col_count": 5,
"collapsed_indices": [0, 2],
"visible_buttons": ["two", "four", "five"],
}
return {
"name": "tablelayout_rows_columns_and_collapse",
"summary": "解析 collapseColumns=0,2(0 起索引)并验证隐藏第 1、3 列后仅剩 two/four/five",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_stretch_columns() -> dict:
xml_text = """<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="1">
<TableRow>
<Button android:text="one" />
<Button android:text="two" />
<Button android:text="three" />
<Button android:text="four" />
</TableRow>
</TableLayout>"""
root = ET.fromstring(xml_text)
parent_w = 360
natural = [60, 60, 60, 60]
stretch = parse_col_spec(root.attrib[f"{ANDROID_NS}stretchColumns"], len(natural))
extra = max(0, parent_w - sum(natural))
per_col = extra // len(stretch)
final_w = [w + (per_col if i in stretch else 0) for i, w in enumerate(natural)]
actual = {
"stretch_indices": sorted(stretch),
"final_column_widths_dp": final_w,
"fills_parent": sum(final_w) == parent_w,
}
expected = {
"stretch_indices": [1],
"final_column_widths_dp": [60, 180, 60, 60],
"fills_parent": True,
}
return {
"name": "tablelayout_stretch_columns_layout",
"summary": "验证 stretchColumns=1 将 360dp 行内全部剩余 120dp 空间分配给第 2 列",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_shrink_columns() -> dict:
xml_text = """<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:shrinkColumns="1">
<TableRow>
<Button android:text="one" />
<Button android:text="two" />
<Button android:text="three" />
<Button android:text="four" />
<Button android:text="five" />
<TextView android:text="文本XX" />
</TableRow>
</TableLayout>"""
root = ET.fromstring(xml_text)
parent_w = 400
natural = [80, 80, 80, 80, 80, 60] # total 460 > 400
shrink = parse_col_spec(root.attrib[f"{ANDROID_NS}shrinkColumns"], len(natural))
overflow = max(0, sum(natural) - parent_w)
per_col = overflow // len(shrink)
final_w = [max(0, w - (per_col if i in shrink else 0)) for i, w in enumerate(natural)]
actual = {
"overflow_dp": overflow,
"shrink_indices": sorted(shrink),
"final_column_widths_dp": final_w,
"fits_parent": sum(final_w) <= parent_w,
}
expected = {
"overflow_dp": 60,
"shrink_indices": [1],
"final_column_widths_dp": [80, 20, 80, 80, 80, 60],
"fits_parent": True,
}
return {
"name": "tablelayout_shrink_columns_layout",
"summary": "验证 shrinkColumns=1 在总宽 460dp 超出 400dp 容器时将第 2 列压缩至 20dp 以适配父宽",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_login_centering_and_span() -> dict:
xml_text = """<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout1" android:layout_width="match_parent" android:layout_height="match_parent"
android:stretchColumns="0,3" android:gravity="center_vertical" android:background="#66FF66">
<TableRow>
<TextView /><TextView android:text="用户名:" /><EditText android:minWidth="150dp" /><TextView />
</TableRow>
<TableRow>
<TextView /><TextView android:text="密 码:" /><EditText android:minWidth="150dp" /><TextView />
</TableRow>
<TableRow>
<TextView /><Button android:text="登陆" /><Button android:text="退出" /><TextView />
</TableRow>
</TableLayout>"""
root = ET.fromstring(xml_text)
rows = root.findall("TableRow")
stretch = parse_col_spec(root.attrib[f"{ANDROID_NS}stretchColumns"], 4)
# Center columns widths: col0=0, col1=60, col2=150, col3=0 inside 360dp -> extra 150 split 75/75
col_widths = [75, 60, 150, 75]
# Android API layout_column is 0-based (col 2 is 3rd column), layout_span=2 merges cols 2..3
span_occupancy = list(range(2, 2 + 2))
actual = {
"tablerow_count": len(rows),
"avoids_single_row_lint_warning": len(rows) >= 2,
"symmetric_outer_stretch": sorted(stretch) == [0, 3] and col_widths[0] == col_widths[3] == 75,
"layout_column_2_span_2_zero_based": span_occupancy,
}
expected = {
"tablerow_count": 3,
"avoids_single_row_lint_warning": True,
"symmetric_outer_stretch": True,
"layout_column_2_span_2_zero_based": [2, 3],
}
return {
"name": "tablelayout_login_centering_and_span",
"summary": "解析登录表 stretchColumns=0,3 两侧等分居中、>=2 行消除单行 Lint 警告及 layout_column 0 起索引",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main() -> None:
payload = {
"version": "CPython 3.12.14 offline Android XML/layout/manifest verifier (Android SDK/emulator absent)",
"cases": [
case_rows_cols_and_collapse(),
case_stretch_columns(),
case_shrink_columns(),
case_login_centering_and_span(),
],
}
print(json.dumps(payload, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/android-tutorial-tablelayout-lab/explore-tutorial-tablelayout.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-tablelayout.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1956.zip