How know the size a component can assume in it's parent?
I'm creating a custom component, where can I know the dimensions this can assume on it's parent? In Layout 开发者_StackOverflowit's defined with fill_parent x fill_parent.
I tried getWidth() x getHeight() on the constructor, on the onMeasure method and onFinishInflate, in all cases this return 0, I wish to know the size it's have to draw some components in independent screen size.
You can override onMeasure() and get values from there.
http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)
In onMeasure you can actually set your own dimensions for the component and build component accordingly to sizes after that.
Use View.MeasureSpec.getSize(one of the parameters methods takes in. Either width, or height) to get dimensions.
getWidth()
and getHeight()
will only be populated after the Layout Phase, that is after onLayout()
has been called.
You should not use the values within onMeasure()
or getMeasuredWidth()
/getMeasuredHeight()
, since those are only the dimensions the View is initially offered or would like to have, not necessarily the ones it actually ends up getting.
See http://developer.android.com/guide/topics/ui/how-android-draws.html for more information.
精彩评论