TableLayout布局的特点:没加入一个TableRow就表示表格添加一行,然后TableRow中每添加一个控件就表示该行加入一列;TableRow中的空间不能指定宽度;
在表格布局中:
表格的列数由控件最多的一行决定;
列宽由控件最宽的一个决定;
常见的几个属性:
android:layout_span=”2” 合并两个单元格;
android:stretchColumns=”1” 拉伸第二列:
android:shrinkColumns=”2” 收缩第三列:
android:collapseColumns=”0” 第0列隐藏(设置某列隐藏:)
实例:实现列的隐藏:
先贴图:
上代码:
activity_main.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/table1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
tools:context="com.example.tablelayoutdemo.MainActivity" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="50dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_column="1"
android:gravity="center_vertical"
android:text="打开···" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Ctrl+o" />
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_column="1"
android:gravity="center_vertical"
android:text="保存···" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Ctrl+s" />
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_column="1"
android:gravity="center_vertical"
android:text="另保存···" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Ctrl+Shift+s" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#686868" />
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="*" />
<TextView
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_column="1"
android:gravity="center_vertical"
android:text="导入···" />
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="*" />
<TextView
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_column="1"
android:gravity="center_vertical"
android:text="导出···" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Ctrl+E" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#686868" />
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_column="1"
android:gravity="center_vertical"
android:text="退出" />
</TableRow>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="隐藏" />
</TableLayout>
MainActivity.java中的主要代码:
//声明一下控件名称:
Button button;
TableLayout t1;
boolean falg=true;
boolean falgs=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.button);
t1=(TableLayout) findViewById(R.id.table1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(falg){
//setColumnCollapsed(0, true):设置某列是否为为隐藏状态
t1.setColumnCollapsed(0, true);
button.setText("显示");
falg=false;
}else{
t1.setColumnCollapsed(0, false);
button.setText("隐藏");
falg=true;
}
}
});
}
//学习使用,代替笔记!!!