Android ApiDemos示例解析(162):Views->Layouts->TableLayout->07. Column Collapse
2012-09-10 08:06
417 查看
本例介绍如果使用setColumnCollapsed 来显示或隐藏表格的某一列:
void setColumnCollapsed(int columnIndex, boolean isCollapsed)
columnIndex: 列的序号,从0开始
isCollapsed: true: 隐藏该列,false恢复该列。
在XML中可以使用android:collapseColumns 来设置开始需要隐藏的列的序号,本例开始显示时隐藏了第三列(Shortcut)
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<TableLayout
android:id=”@+id/menu”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:stretchColumns=”1″
android:collapseColumns=”2″>
<TableRow>
<TextView
android:layout_column=”1″
android:text=”@string/table_layout_7_open”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_open_shortcut”
android:gravity=”right”
android:padding=”3dip” />
</TableRow>
<TableRow>
<TextView
android:layout_column=”1″
android:text=”@string/table_layout_7_save”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_save_shortcut”
android:gravity=”right”
android:padding=”3dip” />
</TableRow>
<TableRow>
<TextView
android:layout_column=”1″
android:text=”@string/table_layout_7_save_as”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_save_as_shortcut”
android:gravity=”right”
android:padding=”3dip” />
</TableRow>
<View
android:layout_height=”2dip”
android:background=”#FF909090″ />
<TableRow>
<TextView
android:text=”@string/table_layout_7_x”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_import”
android:padding=”3dip” />
</TableRow>
<TableRow>
<TextView
android:text=”@string/table_layout_7_x”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_export”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_export_shortcut”
android:gravity=”right”
android:padding=”3dip” />
</TableRow>
<View
android:layout_height=”2dip”
android:background=”#FF909090″ />
</TableLayout>
<LinearLayout
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
<Button
android:id=”@+id/toggle2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/table_layout_7_toggle_checkmarks” />
<Button
android:id=”@+id/toggle1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/table_layout_7_toggle_shortcuts” />
</LinearLayout>
</LinearLayout>
代码中动态显示隐藏表格列:
void setColumnCollapsed(int columnIndex, boolean isCollapsed)
columnIndex: 列的序号,从0开始
isCollapsed: true: 隐藏该列,false恢复该列。
在XML中可以使用android:collapseColumns 来设置开始需要隐藏的列的序号,本例开始显示时隐藏了第三列(Shortcut)
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<TableLayout
android:id=”@+id/menu”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:stretchColumns=”1″
android:collapseColumns=”2″>
<TableRow>
<TextView
android:layout_column=”1″
android:text=”@string/table_layout_7_open”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_open_shortcut”
android:gravity=”right”
android:padding=”3dip” />
</TableRow>
<TableRow>
<TextView
android:layout_column=”1″
android:text=”@string/table_layout_7_save”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_save_shortcut”
android:gravity=”right”
android:padding=”3dip” />
</TableRow>
<TableRow>
<TextView
android:layout_column=”1″
android:text=”@string/table_layout_7_save_as”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_save_as_shortcut”
android:gravity=”right”
android:padding=”3dip” />
</TableRow>
<View
android:layout_height=”2dip”
android:background=”#FF909090″ />
<TableRow>
<TextView
android:text=”@string/table_layout_7_x”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_import”
android:padding=”3dip” />
</TableRow>
<TableRow>
<TextView
android:text=”@string/table_layout_7_x”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_export”
android:padding=”3dip” />
<TextView
android:text=”@string/table_layout_7_export_shortcut”
android:gravity=”right”
android:padding=”3dip” />
</TableRow>
<View
android:layout_height=”2dip”
android:background=”#FF909090″ />
</TableLayout>
<LinearLayout
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
<Button
android:id=”@+id/toggle2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/table_layout_7_toggle_checkmarks” />
<Button
android:id=”@+id/toggle1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/table_layout_7_toggle_shortcuts” />
</LinearLayout>
</LinearLayout>
代码中动态显示隐藏表格列:
button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { mShortcutsCollapsed = !mShortcutsCollapsed; table.setColumnCollapsed(2, mShortcutsCollapsed); } });
相关文章推荐
- Android ApiDemos示例解析(166):Views->Layouts->TableLayout->11. Gravity
- Android ApiDemos示例解析(158):Views->Layouts->TableLayout->03.Long Content
- Android ApiDemos示例解析(167):Views->Layouts->TableLayout->12. Cell Spanning
- Android ApiDemos示例解析(159):Views->Layouts->TableLayout->04.Stretchable
- Android ApiDemos示例解析(161):Views->Layouts->TableLayout->06. More Spanning and Stretchable
- Android ApiDemos示例解析(156):Views->Layouts->TableLayout->01. Basic
- Android ApiDemos示例解析(160):Views->Layouts->TableLayout->05. Spanning and Stretchable
- Android ApiDemos示例解析(157):Views->Layouts->TableLayout->02. Empty Cells
- Android ApiDemos示例解析(163):Views->Layouts->TableLayout->08. Toggle Stretch
- Android ApiDemos示例解析(164):Views->Layouts->TableLayout->09. Toggle Shrink
- Android ApiDemos示例解析(165):Views->Layouts->TableLayout->10. Simple Form
- Android ApiDemos示例解析(147):Views->Layouts->LinearLayout->7. Fill Parent
- Android ApiDemos示例解析(149):Views->Layouts->LinearLayout->9. Layout Weight
- Android ApiDemos示例解析(141):Views->Layouts->LinearLayout->1. Vertical
- Android ApiDemos示例解析(142):Views->Layouts->LinearLayout->2. Vertical (Fill Screen)
- Android ApiDemos示例解析(148):Views->Layouts->LinearLayout->8. Gravity
- Android ApiDemos示例解析(152):Views->Layouts->RelativeLayout->2.Simple Form
- Android ApiDemos示例解析(150):Views->Layouts->LinearLayout->10. Background Image
- Android ApiDemos示例解析(145):Views->Layouts->LinearLayout->5.Simple Form
- Android ApiDemos示例解析(151):Views->Layouts->RelativeLayout->1. Vertical