3

我有一个滚动视图,其高度和宽度在 xml 文件中定义,但我想在运行时动态增加或减少滚动视图的高度和宽度。

实际上我想增加滚动视图的高度和宽度 10 像素每秒 10 秒。但是 scrollView.getLayoutParams().height = GivenHeight 使用这段代码我只能增加一次运行时间。我们可以增加一个以上。

提前致谢。

4

2 回答 2

5
scrollView.getLayoutParams().height = yourNewHeight; // in pixels
scrollView.getLayoutParams().width = yourNewWidth; // in pixels

编辑你的新问题:如果你想随着时间的推移增加这个,你可以通过使用 TimerTask 并在 UI 线程上运行它来轻松实现这一点,因为我们正在触摸它的视图。我会给你代码,因为它真的很简单。

int secondCounter = 0;
int delay = 0;   // delay for 0 sec.
int period = 1000;  // repeat every sec.
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
        // do work inside ui thread
        runOnUiThread(new Runnable() {
            public void run() {
                // do your work right here
                secondCounter++;
                yourNewHeight += 10;
                yourNewWidth += 10;
                scrollView.getLayoutParams().height = yourNewHeight; // in pixels
                scrollView.getLayoutParams().width = yourNewWidth; // in pixels
                //stop the timer when 10 seconds has passed
                if(secondCounter == 10){
                    timer.cancel();
                }
            }
        });
    }
}, delay, period);

编辑:强制视图刷新..

ViewGroup vg = findViewById (R.id.rootLayout);
vg.invalidate();

在设置新的高度和宽度后,您需要在 run 方法中调用 invalidate 。

于 2012-07-10T13:59:46.433 回答
1
scrollView.getLayoutParams().height = assignHeight;
scrollView.getLayoutParams().width  = assignWidth; 
于 2012-07-12T11:52:51.723 回答