Android Jelly Bean MeasureSpec bug

筅森魡賤 提交于 2019-12-10 16:12:46

问题


I was having this problem: https://stackoverflow.com/questions/20121696/slidingmenu-bug-in-android-4-3
But now I've fixed and I want to share my solution 'cause probably someone will need it too.
I'll answer this question myself bellow.


回答1:


So the problem I was having lies on the fact that Build.VERSION_CODES.JELLY_BEAN_MR2 has a problem when we want to create a MeasureSpec:

MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams..., MeasureSpec.EXACTLY);

With MeasureSpec.EXACTLY when I perform for example a .measure(widthMeasureSpec, heightMeasureSpec); it returns values completely strange, so this problem can be solved if we use MeasureSpec.AT_MOST instead of MeasureSpec.EXACTLY.


Hope it helps someone in the future ;)
ps: I don't know if Android Kitkat (4.4, API 19) has this problem too.

EDIT: it does.

int widthMeasureSpec;
int heightMeasureSpec;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.AT_MOST);
    heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.AT_MOST);
} else {
    widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.EXACTLY);
    heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY);
}


来源:https://stackoverflow.com/questions/20147938/android-jelly-bean-measurespec-bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!