开发者

What's a simple way to make a view's layout_width="match_parent" and make the height equal to its width?

I would like a view styled开发者_运维技巧 similar to the album art in the Rdio app (img)

I don't know how I should be going about this. I want to load a bitmap into the view, if that affects the solution at all.


In this case (where the control has the full width of the device) you can always just use getWindowManager().getDefaultDisplay().getWidth(); to get the width and then manually set the height.

If you're not using the full display width then you'll probably need to override onMeasure in the View class and set the height to match the width in there, perhaps like this:

@Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec )
{
    viewWidth = MeasureSpec.getSize( widthMeasureSpec );
    viewHeight = MeasureSpec.getSize( heightMeasureSpec );

    if(viewWidth != 0) viewHeight = viewWidth;
    setMeasuredDimension( viewWidth, viewHeight );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜