以编程方式使用 android:layout_weight 填充 TableLayout
我正在尝试用单元格和行拉伸填充表格布局以填充整个屏幕。像这样:
http://imageshack.us/photo/my-images/69 /device20120201005942.png/
在 Eclipse 的图形布局窗口中设计的这个布局给出了我想要的(android:layout_weight 解决了垂直拉伸):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_weight="1">
<ImageView
android:id="@+id/imageView1"
android:layout_gravity="center"
android:src="@drawable/add" />
<ImageView
android:id="@+id/imageView2"
android:layout_gravity="center"
android:src="@drawable/bound" />
<ImageView
android:id="@+id/imageView3"
android:layout_gravity="center"
android:src="@drawable/cod" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageView4"
android:layout_gravity="center"
android:src="@drawable/delete" />
<ImageView
android:id="@+id/imageView5"
android:layout_gravity="center"
android:src="@drawable/delete2" />
<ImageView
android:id="@+id/imageView6"
android:layout_gravity="center"
android:src="@drawable/floppy" />
</TableRow>
</TableLayout>
我试图在代码中创建相同的外观,但到目前为止失败了。这是我的 Activity 的 oncreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablelo);
TableLayout tl = (TableLayout) findViewById(R.id.table);
Log.d(TAG, "Width :" + tl.getWidth());
for (int row = 0; row < 3; row++) {
TableRow tr = new TableRow(this);
tr.setId(100 + row);
for (int cell = 0; cell < 3; cell++) {
ImageView image = new ImageView(this);
image.setImageResource(imageIDs[row * 3 + cell]);
tr.addView(image);
}
tr.setGravity(Gravity.CENTER);
LayoutParams params = new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1);
tr.setLayoutParams(params);
tl.addView(tr, params);
}
tl.setStretchAllColumns(true);
}
我缺少设置 ImageView 重力的代码,但这种尝试会导致崩溃:
image.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
此代码会创建 3 行,每行水平拉伸,但不垂直拉伸:
http://imageshack.us/photo/my-images/835/gridc.png/
我找不到我想要的我在这里失踪了。我还尝试在设计器中准备一个表行并在运行时膨胀并添加到表中,但结果没有改变。
因此,不知何故,我想以编程方式获得与使用 editor 创建的布局相同的布局。
请帮忙!
I am trying to populate a tablelayout with cells and rows stretches to fill the whole screen. Like this:
http://imageshack.us/photo/my-images/69/device20120201005942.png/
This layout designed in Graphical Layout window in Eclipse gives what I want (android:layout_weight solves vertical stretching):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_weight="1">
<ImageView
android:id="@+id/imageView1"
android:layout_gravity="center"
android:src="@drawable/add" />
<ImageView
android:id="@+id/imageView2"
android:layout_gravity="center"
android:src="@drawable/bound" />
<ImageView
android:id="@+id/imageView3"
android:layout_gravity="center"
android:src="@drawable/cod" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageView4"
android:layout_gravity="center"
android:src="@drawable/delete" />
<ImageView
android:id="@+id/imageView5"
android:layout_gravity="center"
android:src="@drawable/delete2" />
<ImageView
android:id="@+id/imageView6"
android:layout_gravity="center"
android:src="@drawable/floppy" />
</TableRow>
</TableLayout>
I am trying to create the same look within code but failed so far. Here is the oncreate of my Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablelo);
TableLayout tl = (TableLayout) findViewById(R.id.table);
Log.d(TAG, "Width :" + tl.getWidth());
for (int row = 0; row < 3; row++) {
TableRow tr = new TableRow(this);
tr.setId(100 + row);
for (int cell = 0; cell < 3; cell++) {
ImageView image = new ImageView(this);
image.setImageResource(imageIDs[row * 3 + cell]);
tr.addView(image);
}
tr.setGravity(Gravity.CENTER);
LayoutParams params = new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1);
tr.setLayoutParams(params);
tl.addView(tr, params);
}
tl.setStretchAllColumns(true);
}
I am missing the code to set ImageView gravity, but this attemp causes crash:
image.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
This code results 3 rows created, each stretch horizontally but not vertically:
http://imageshack.us/photo/my-images/835/gridc.png/
I can not find what I am missing here. I also tried to prepare a tablerow in designer and inflate and add to table during runtime but result did not change.
So, somehow, I want to get programmatically the same layout that I could created with editor .
Help, please!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我花了一个半小时试图弄清楚发生了什么事。然后我找到了它:)这是我的 TableActivity 的完整列表,请注意表行和图像的不同 LayoutParams 的使用:
table_activity.xml 只是一个空表布局:
所以简而言之,最简单的解决方案是使用 xml 文件中定义的布局:) 但是如果您必须使用 JAVA,则必须小心您使用的 LayoutParams 国王,您应该始终使用视图的父级之一。
I've spent an hour and a half trying to figure out what was going on. And then I found it :) Here's a complete listing of my TableActivity, please note the usage of different LayoutParams for table rows and images:
table_activity.xml is just an empty table layout:
So to put it short, the simplest solution would be to use layouts defined in xml files :) But if you have to use JAVA, you must be careful with what king of LayoutParams you are using, you should always use the one of a view's parent.