【发布时间】:2014-01-31 22:29:49
【问题描述】:
我编写了以下代码,首先增加 ImageView 的大小,然后在 100 毫秒后减小同一个 ImageView 的大小。然而,这段代码增加了 ImageView 的大小,但不会减小它的大小,或者延迟 100 毫秒后的代码不会影响 imageView 的尺寸。
我做错了什么?
uiHandler.post(new Runnable()
{
@Override
public void run()
{
FrameLayout.LayoutParams layout = (android.widget.FrameLayout.LayoutParams) imageView.getLayoutParams();
layout.height = (int) (2*iconsSizeInDP);
layout.width = (int) (2*iconsSizeInDP);
imageView.setLayoutParams(layout);
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
// following code block doesnt'affect imageView dimensions
layout.height = (int) iconsSizeInDP;
layout.width = (int) iconsSizeInDP;
imageView.setLayoutParams(layout);
}
});
问候
【问题讨论】:
-
尝试在您说代码不起作用的地方再次创建
LayoutParamsFrameLayout.LayoutParams layout = (android.widget.FrameLayout.LayoutParams) imageView.getLayoutParams();。还要增加Thread.sleep()的时间。可能很难看出差异。 -
当你一直用睡眠阻塞UI线程时,你认为重新布局发生在哪个线程?
-
你确定没有调用第二个 Set 吗?请使用
AsyncTask而不是uiHandler并再次测试。复制onPreExecute()中的第一部分在onBackground中休眠并在onPostExeute()中设置第二个参数
标签: java android view layoutparams