Android TableLayout不收缩所有图像
我试图创建一个包含8x5图像的表格,如下所示:Android TableLayout不收缩所有图像,android,tablelayout,Android,Tablelayout,我试图创建一个包含8x5图像的表格,如下所示: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TableLayout table = new TableLayout(this); table.setStretchAllColumns(true); table.setShrinkAllColumns(true);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout table = new TableLayout(this);
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
for(int i = 0; i < 6; i++){
TableRow card = new TableRow(this);
card.setGravity(Gravity.CENTER);
for(int j = 0; j < 5; j++){
TableRow.LayoutParams paramsCard = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsCard.setMargins(CARDS_MARGIN,CARDS_MARGIN,CARDS_MARGIN,CARDS_MARGIN);
ImageView imgCard = new ImageView(this);
imgCard.setImageResource(R.drawable.dos);
imgCard.setLayoutParams(paramsCard);
card.addView(imgCard);
}
table.addView(card);
}
setContentView(table);
table.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
}
我敢打赌,您的TableView是线性布局或其他不会溢出可用屏幕的东西-所以最后的视图会缩小,以使所有内容都适合其中-可能。。。您能否确保TableView处于滚动视图中,并查看是否发生这种情况
-serkan如果您已为作为TableLayout父级的外部布局指定了边距或填充,则设置它。 有时会发生这种情况,只是因为您给了TableLayout的父布局一些Margin和填充。 希望它能帮助你。
谢谢。最终我使用线性布局,将Weights设置为1,以自动调整屏幕大小。TableLayout不能很好地调整图像。如果从xml文件加载布局,是否会发生同样的情况?我需要通过编程来完成。你的意思是如果我膨胀表格布局吗?是的,因为完全通过代码创建布局有时会产生奇怪的结果。你应该从xml膨胀布局并相应地设置属性。它更干净,更易于管理..正如您在代码TableLayout table=new TableLayoutthis上看到的那样;我的主要布局是表格布局,而不是线性布局。是的,你能把它放在滚动视图中试试吗?
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);