2010-11-15 83 views
0

如果我有一个TableLayout结构(如下),它定义了2个ImageViews。其中还指定宽度/高度的像素值。这个工程,我所期望的 - PIC一个调整为50像素和PIC2保持在原来的大小:以编程方式调整TableLayout中的ImageView的大小

<TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> 
    <TableRow> 
     <ImageView android:src="@drawable/pic1" android:layout_width="50px" android:layout_height="50px" /> 
     <ImageView android:src="@drawable/pic2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
    </TableRow> 
</TableLayout> 

但我真的需要以编程方式做以上,所以我不喜欢这样...

TableLayout tableLayout = new TableLayout(ctx); 
TableRow tableRow = new TaleRow(ctx); 

ImageView imageViewOne = new ImageView(ctx); 
ImageView imageViewTwo = new ImageView(ctx); 
imageViewOne.setImageResource(R.drawable.pic1); 
imageViewTwo.setImageResource(R.drawable.pic2); 

imageViewOne.setLayoutParams(new LayoutParams(50, 50)); 

rootView.addView(tableLayout); 
tableLayout.addView(tableRow); 
tableRow.addView(imageViewOne); 
tableRow.addView(imageViewTwo); 

上面没有做一样的XML - 第一ImageView的没有显示在所有使用添加setLayoutParams()方法将其之后的编程方法。

我迷失在这里的东西,它是什么不同?

谢谢!

回答

11

好吧我已经设法解决这个问题。用相反的:

imageViewOne.setLayoutParams(new LayoutParams(50, 50)); 

我用这个来代替:

imageViewOne.setLayoutParams(new TableRow.LayoutParams(50, 50)); 
+0

感谢您发布您的解决方案。这非常帮助我! – 2012-04-10 03:28:25

相关问题