我创建了一个有五个表行的表。第一行是每个表的标题。
在接下来的四行中,第二列外壳分别代表一个图像和一个textview。
我的问题是我的图像显示在行的中心。
如果我在图像上添加一些layoutparams的宽度,则它会消失。
我希望图片的对齐方式向左,因此第一列旁边的右侧就结束了。
创建行:
for (int i = 0; i < 4; i++) {
TableRow tableRow = new TableRow(context);
for (int column = 1; column <= 8; column++) {
TextView textView = null;
if (column == 2) {
ImageView imgView = new ImageView(context);
imgView.setLayoutParams(new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
tableRow.addView(imgView);
textView = new TextView(context);
textView.setGravity(LEFT);
} else {
textView = new TextView(context);
}
textView.setGravity(CENTER);
textView.setTextColor(WHITE);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
用数据更新表:
for (int column = 0; column <= 7; column++) {
View child = tableRow.getChildAt(column);
if (child instanceof ImageView) {
ImageView flag = (ImageView) child;
flag.setImageResource(getFlagByClubName(group.getTeams().get(i).getClub()));
}
if (child instanceof TextView) {
TextView textView = (TextView) tableRow.getChildAt(column);
setContentInColumn(group.getTeams().get(i), column, textView);
}
}
最佳答案
您可以尝试将imageview和textview包装在相对的布局中。请注意,我还没有测试下面的代码,但是它是根据我的其他一些正常工作的代码改编而成的。
RelativeLayout wrapper = new RelativeLayout(context);
// Create imageView params
RelativeLayout.LayoutParams imageParams;
imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
// Create imageView
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(imageParams);
imageView.setId(1);
// Create textView params
RelativeLayout.LayoutParams textParams;
textParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textParams.addRule(RelativeLayout.LEFT_OF, imageView.getId());
// Create textView
TextView textView = new TextView(context);
textView.setLayoutParams(textParams);
// Add to the wrapper
wrapper.addView(imageView);
wrapper.addView(textView);
然后将
wrapper
添加到表中:tableRow.addView(wrapper);