1

我有一个Button内在LinearLayout,我想找回它的高度。

我用过sample_button.getHeight(),但它给出了不同的高度值。

即我正在根据seekbar进度修改按钮的高度,如下所示:

public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromTouch) {

LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(
                new ViewGroup.MarginLayoutParams(50, progress * 20));

        lp2.setMargins(200, 0, 0, 0);
        sample_button.setLayoutParams(lp2);

Log.i("Activity", "progress = " + progress);
Log.i("Activity",
                "sample_button.getHeight() = " + sample_button.getHeight());
}

日志值如下:

progress = 1
sample_button.getHeight() = 60

progress = 2
sample_button.getHeight() = 20

progress = 3
sample_button.getHeight() = 40

progress = 2
sample_button.getHeight() = 60

progress = 3
sample_button.getHeight() = 40

progress = 4
sample_button.getHeight() = 60

progress = 3
sample_button.getHeight() = 80

请注意,按钮正在正确显示,但是当 时progress=2,它height=20首先显示,然后显示height=60,以此类推,以显示其他进度值,如上所示。

我做错什么了吗。任何帮助表示赞赏。

4

1 回答 1

1

你有没有试过这样:

ViewGroup.LayoutParams params = sample_button.getLayoutParams();
params.height = progress * 20;
sample_button.setLayoutParams(params);
于 2012-09-25T06:01:26.400 回答