我已經寫了下面的代碼來首先增加ImageView的大小,並在100ms後減小相同的ImageView的大小。然而,這段代碼增加了ImageView的大小,但它不會減小它的大小,或者延遲100ms後的代碼不會影響imageView的尺寸。setLayoutParams不能第二次工作
我在做什麼錯?
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);
}
});
問候
嘗試創建'LayoutParams'再次在的地方,你說的代碼無法正常工作'FrameLayout.LayoutParams佈局=(android.widget.FrameLayout.LayoutParams)ImageView的。 getLayoutParams();'。同時增加'Thread.sleep()'中的時間。看到差異可能太困難了。 –
當你用睡眠阻止UI線程時,你認爲重新佈局發生在哪個線程中? – laalto
你確定第二個Set沒有被調用嗎?請使用'AsyncTask'而不是'uiHandler'並再次測試。複製'onPreExecute()'中的第一部分''在'onBackground'中休眠並在'onPostExeute()'中設置第二個參數' –