LayoutParams 是布局参数的意思。在将创建好的子控件添加到它的父容器控件时,可以同时指定一个布局参数。这个布局参数指示了这个子控件如何在父容器控件里呈现。
LayoutParams 的其中一个构造方法接受两个参数,指示了宽和高的布局,我们可以指定
ViewGroup.LayoutParams.WRAP_CONTENT,以自身内容大小自动适应
ViewGroup.LayoutParams.FILL_PARENT, 填充父容器
setColumnStretchable是TableRow的方法,他指示了第2列(列索引为1)是可以拉伸的。
从视图中读取控件的内容:
我们在一个按钮的单击事件里写下这样的代码
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i< _pnlContent.getChildCount();i++)
{
TableRow tr = (TableRow)_pnlContent.getChildAt(i);
TextView txt1 = (TextView)tr.getChildAt(0);
EditText et1 = (EditText)tr.getChildAt(1);
sb.append(String.format("{%s}={%s} ", txt1.getTag()。toString(),
et1.getText() ));
}
Toast.makeText(getApplicationContext(), sb.toString(), 0)。show();
如上面的代码显示。我们遍历容器控件,使用方法getChildAt(0);获得指定索引的子控件,合理转型后为我们所用。我们还可以使用Tag存放我们的额外信息。
LayoutParams 的其中一个构造方法接受两个参数,指示了宽和高的布局,我们可以指定
ViewGroup.LayoutParams.WRAP_CONTENT,以自身内容大小自动适应
ViewGroup.LayoutParams.FILL_PARENT, 填充父容器
setColumnStretchable是TableRow的方法,他指示了第2列(列索引为1)是可以拉伸的。
从视图中读取控件的内容:
我们在一个按钮的单击事件里写下这样的代码
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i< _pnlContent.getChildCount();i++)
{
TableRow tr = (TableRow)_pnlContent.getChildAt(i);
TextView txt1 = (TextView)tr.getChildAt(0);
EditText et1 = (EditText)tr.getChildAt(1);
sb.append(String.format("{%s}={%s} ", txt1.getTag()。toString(),
et1.getText() ));
}
Toast.makeText(getApplicationContext(), sb.toString(), 0)。show();
如上面的代码显示。我们遍历容器控件,使用方法getChildAt(0);获得指定索引的子控件,合理转型后为我们所用。我们还可以使用Tag存放我们的额外信息。