在`TableLayout`中實現「一行兩個按鈕」的常見需求,關鍵在於單元格內嵌套水平LinearLayout。本文提供標準實現代碼與佈局優化建議。
<TableRow
android:id="@+id/send_notice_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CDCDCD"
android:stretchColumns="1" >
<TextView
android:id="@+id/notice_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="5dp"
android:text="通知類型:" />
<!-- 關鍵:水平LinearLayout包裹兩按鈕 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center" >
<Button
android:id="@+id/meeting_notice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/boder"
android:text="會議通知"
android:textSize="15sp" />
<Button
android:id="@+id/leave_notice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/boder"
android:text="放假通知"
android:textSize="15sp" />
</LinearLayout>
</TableRow>
掌握「容器分空間、子項分權重」模式,複雜表格佈局亦能游刃有餘。