获得相同按钮的不同按钮高度

时间:2012-09-25 05:55:59

标签: android android-layout android-button

我在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,依此类推其他进度值,如上所示。

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

1 个答案:

答案 0 :(得分:1)

你有没有尝试过这种方式:

ViewGroup.LayoutParams params = sample_button.getLayoutParams();
params.height = progress * 20;
sample_button.setLayoutParams(params);