给大家介绍下本人开发过程中遇到的Android多版本兼容的一些坑,只是针对Android的api的一些算是缺陷,引起开发中一些界面的问题,此贴会持续更新。
开发中假如你使用FrameLayout包含子视图,假如没有定义子View的gravity,同时定义了子View的margin,那么你就中招了,你会发现子View的margin在4.0以上的系统显示正常,而在Android2.3的系统中没起到作用。我们一起走下Android的framework这块的api,来看下这块的版本差异:
细心的你可能很快就会定位到FrameLayout的onLayout方法,一起来看下Android版本的差异:
这是Android2.3.2的源码:
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int count = getChildCount();
final int parentLeft = mPaddingLeft + mForegroundPaddingLeft;
final int parentRight = right - left - mPaddingRight - mForegroundPaddingRight;
final int parentTop = mPaddingTop + mForegroundPaddingTop;
final int parentBottom = bottom - top - mPaddingBottom - mForegroundPaddingBottom;
mForegroundBoundsChanged = true;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();