2016-03-16 36 views
0

我在ImageButton中有一個32x32圖像。雖然layout_width和layout_height設置爲wrap_content,但ImageButton顯示爲高度大於寬度的矩形。容器佈局是一個LinearLayout,在此佈局中加權其他小部件的大小沒有什麼特別的。Android ImageButton具有不等的寬度和高度

這是爲什麼?這似乎是我從4.1.1升級了我的手機後。到4.4這個奇怪的行爲進來了。

+0

發佈您的代碼。 –

+0

按鈕不是默認的方形。所以它可能會顯示一些矩形形狀。請嘗試imageView –

+0

請在此處放置您的代碼。 –

回答

0

是圖像的大小。如果是,則表示您可以提到固定大小而不是使用換行內容。

或者,如果它是動態意味着你可以在你的應用程序中使用下面的SquareImageview

public class SquareImageView extends ImageView { 

    public SquareImageView(Context context) { 
     super(context); 
    } 

    public SquareImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int width = getMeasuredWidth(); 
     setMeasuredDimension(width, width); 
    } 
} 

使用這個類。

然後調用此SquareImageView代替ImageView和layout_height should be match_parent