Android ImageButton setMaxheight没有效果

时间:2013-01-02 06:21:06

标签: android height imagebutton image-scaling

我使用表格布局使用权重布局按钮,按钮获得0dip大小或wrap_content,因此它们按重量扩展。

我正在尝试将按钮转换为ImageButton并将图像设置为按钮大小而不是展开它。

我让按钮被创建并获得它的大小然后我使用ImageButton.setMaxHeight()来分配图像并限制它的高度;

final ImageButton ib = (ImageButton) findViewById(R.id.buttonOptions);
    ib.post(new Runnable() {
        public void run() {
            final int height = ib.getHeight();
            ib.setScaleType(ScaleType.FIT_CENTER);
            ib.setMaxHeight(5); // or height    
            ib.setImageResource(R.drawable.options);

        }});

正在以原始尺寸绘制图像,从而扩展按钮... setMaxHeight()不限制图像高度/宽度。知道为什么吗?

3 个答案:

答案 0 :(得分:3)

使用

解决了它
ib.setAdjustViewBounds(true);

答案 1 :(得分:0)

ViewTreeObserver observer = ib.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//here you can get height and set height
}

不确定但是尝试一下。它可以帮到你。

答案 2 :(得分:0)

setMaxHeight()用于图像按钮,而不用于图像中的图像。

尝试,

ib.setScaleType(ScaleType.CENTER_CROP);