Saturday 15 September 2012

Why is my android custom view not square? -


I have created a custom view to display a gameboard to develop a game. The gameboard should be a class all the time. So the height and height should be the same. I followed it to get the

I added the following code for my custom view: Zero onMeasure

  @Override protected (integer widthMeasureSpec, integer heightMeasureSpec) {super. OnMeasure (widthMeasureSpec, heightMeasureSpec); Int width = getMeasuredWidth (); Int height = getMeasuredHeight (); Int width width padding = width - getPaddingLeft () - getPaddingRight (); Int heigthWithoutPadding = height - getPaddingTop () - getPaddingBottom (); Int maxWidth = (int) (heathwithout padding * RATIO); Int maxHeight = (int) (width expansion padding / ratio); If (Width Extended Padding> MaxWidth) {width = maxWidth + getPaddingLeft () + getPaddingRight (); } And {height = maximum heights + get paddingtop () + getPaddingBottom (); } SetMeasuredDimension (width, height); } @ Override Scared Protected Zero (Canvas Canvas) {Super. Don (canvas); Paint P = new paint (); P.setStyle (Paint.Style.STROKE); P.setStrokeWidth (3); Canvas.drawRect (0, 0, getMeasuredWidth () - 1, getMeasuredHeight () - 1, p); }   

The layout looks like this with my custom view:

  & lt; Relative layout xmlns: android = "http://schemas.android.com/ apk / res / android" xmlns: tool = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height Android: paddingLeft = "@ Dimen / activity_horizontal_margin" Android: paddingRight = "@ Dimen / activity_horizontal_margin" Android: paddingTop = "@ Dimen / activity_vertical_margin" Android: paddingBottom = "@ Dimen / activity_vertical_margin" Devices: Reference = "MainActivity . " & Gt; & Lt; Visual Android: layout_width = "wrap_content" android: layout_height = "wrap_content" class = "com.peerkesoftware.nanograms.controls.GameBoard" Android: id = "@ + id / view" Android: background = "@Android: color / holo_purple "/> & Lt; / RelativeLayout & gt;   

The class made in the Ondra method is always a square. It works fine

In the layout I add custom view and give the background the background color. When I display it on a device in portrait mode then everything is fine and the background color fills the square that pulls in that open method.

When I switch the device to landscape mode Square is still a square, but if the area of ​​the background color is large, then he will. It has the same height, but there is more width but I do not know why GetMeasuredWidth () and getMeasuredHeight () methods come back to the right values ​​How can it be that the scene is still bigger?

For some reason, you do not have to consider working in a relational layout for the landscape orientation measurement (the following obstacles Ends on the phone):

width = MeasureSpec: exactly 404; Height = Measurement Separak: AT_MOST 388

This is different from the measured width due to the actual width (measured on my phone = 388, actual = 404). This is the reason why you see the detail of the background beyond your works.

In addition, measure does not help in respecting mode, measurement still ends with the same result above.

The solution is to use a different layout to make LinearLayout and FrameLayout work for me apart, it is a good idea to use real view size in OnDraw with getWidth () and getHeight (), < / P>

If you really need RelativeLayout, then you can add a child FrameLayout to your ideas like this.

  & lt; Relative layout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: Android : PaddingBottom = "@ dimen / activity_vertical_margin" Android: paddingLeft = "@ dimen / activity_horizontal_margin" Android: paddingRight = "@ dimen / activity_horizontal_margin" Android: paddingTop = "@ Dimen / activity_vertical_margin" tool: reference = "main activity" & gt; ; & Lt; FrameLayout Android: layout_width = "match_parent" Android: layout_height = "match_parent" & gt; & Lt; Visual Android: id = "@ + id / view" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" class = "com.peerkesoftware.nogramogram.controls.GameBoard" Android: Background = "@Android: Color / Holo_Pipple "/> & Lt; / FrameLayout & gt; & Lt; / RelativeLayout & gt;    

No comments:

Post a Comment