一个TableLayout内重新调整ImageView的编程 [英] Resizing an ImageView within a TableLayout programmatically

查看:176
本文介绍了一个TableLayout内重新调整ImageView的编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have a TableLayout structure (below) which defines 2 ImageViews. One which also specifies pixel values for width/height. This works as I would expect - Pic one is resized to 50px and pic2 is kept at its original size:

<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()方法后的编程方法

The above doesn't do the same as the XML - The first ImageView does not show at all using the programmatic way after adding the setLayoutParams() method to it.

我的雾在这里失去了一些东西,它是什么,有什么不同?

谢谢!

推荐答案

好,我设法解决这个问题。而不是使用的:

Ok I've managed to solve this. Instead of using:

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

我用这个来代替:

I used this instead:

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

这篇关于一个TableLayout内重新调整ImageView的编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆