setLayoutParams第二次不起作用

时间:2014-01-13 08:36:19

标签: java android view layoutparams

我编写了以下代码,首先增加ImageView的大小,100ms后减小相同ImageView的大小。但是,此代码增加了ImageView的大小,但它不会减小它的大小或100ms延迟后的代码不会影响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);
    }
});

此致

2 个答案:

答案 0 :(得分:2)

您在同一UI线程中更改了布局2次,因此只有最后一次更改才能生效。 您应该分为2个UI线程,如下所示:

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);
    }
};
uiHandler.postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        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);
    }
},50);

答案 1 :(得分:0)

请查看FrameLayout.LayoutParams layout = (android.widget.FrameLayout.LayoutParams) imageView.getLayoutParams();

请尝试制作FrameLayout.LayoutParams layout全球