Skip to content

Commit

Permalink
修复高度计算bug
Browse files Browse the repository at this point in the history
  • Loading branch information
brook committed Mar 17, 2019
1 parent 2c0b7dd commit 86f0c8f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#### 方式一:使用jcenter仓库,在build.gradle中添加(推荐)

```groovy
implementation 'com.brook.android:DependencyLayout:1.1.0'
implementation 'com.brook.android:DependencyLayout:1.1.1'
```

#### 方式二:使用jetpack仓库,在根项目的build.gradle中添加下面的代码
Expand All @@ -29,7 +29,7 @@ allprojects {
然后在需要使用的模块的build.gradle中添加以下代码
```groovy
dependencies {
implementation 'com.github.Brook007:DependencyLayout:1.1.0'
implementation 'com.github.Brook007:DependencyLayout:1.1.1'
}
```

Expand Down
9 changes: 5 additions & 4 deletions app/src/main/debug/res/layout/activity_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
android:textColor="#323232"
android:textSize="15dp"
app:alignParentRight="true"
app:centerInParentVertical="true" />
app:centerInParentVertical="true"
app:selfWidth="50%pw" />

</com.brook.app.android.supportlibrary.view.DependencyLayout>

Expand All @@ -104,12 +105,12 @@

<FrameLayout
android:layout_width="wrap_content"
android:background="#009688"
android:layout_height="wrap_content"
android:layout_alignTop="@id/user_head"
android:layout_alignBottom="@id/user_head"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/user_head">
android:layout_toRightOf="@id/user_head"
android:background="#009688">


<TextView
Expand Down Expand Up @@ -145,10 +146,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#ffffff"
android:gravity="center"
android:layout_centerVertical="true"
android:includeFontPadding="false"
android:paddingLeft="30dp"
android:paddingTop="7dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static float calculation(DependencyLayout.LayoutParams.Attribute source,
} else {
if (pdw.unit == DependencyLayout.LayoutParams.Attribute.PX) {
size = parentWidth / pdw.value * Util.dp2px(source.value);
} else if (pdw.unit == DependencyLayout.LayoutParams.Attribute.DIP){
} else if (pdw.unit == DependencyLayout.LayoutParams.Attribute.DIP) {
size = parentWidth / pdw.value * source.value;
}
}
Expand Down Expand Up @@ -178,17 +178,17 @@ public static DependencyLayout.LayoutParams.Attribute unbox(String text) {
} else if ("px".equals(unit)) {
attribute.unit = DependencyLayout.LayoutParams.Attribute.PX;
} else if ("%sw".equals(unit)) {
attribute.unit = DependencyLayout.LayoutParams.Attribute.PX;
attribute.unit = DependencyLayout.LayoutParams.Attribute.SW;
} else if ("%sh".equals(unit)) {
attribute.unit = DependencyLayout.LayoutParams.Attribute.PX;
attribute.unit = DependencyLayout.LayoutParams.Attribute.SH;
} else if ("%pw".equals(unit)) {
attribute.unit = DependencyLayout.LayoutParams.Attribute.PX;
attribute.unit = DependencyLayout.LayoutParams.Attribute.PW;
} else if ("%ph".equals(unit)) {
attribute.unit = DependencyLayout.LayoutParams.Attribute.PX;
attribute.unit = DependencyLayout.LayoutParams.Attribute.PH;
} else if ("%mw".equals(unit)) {
attribute.unit = DependencyLayout.LayoutParams.Attribute.PX;
attribute.unit = DependencyLayout.LayoutParams.Attribute.MW;
} else if ("%mh".equals(unit)) {
attribute.unit = DependencyLayout.LayoutParams.Attribute.PX;
attribute.unit = DependencyLayout.LayoutParams.Attribute.MH;
} else {
attribute.unit = DependencyLayout.LayoutParams.Attribute.ERROR;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureChildren(widthMeasureSpec, heightMeasureSpec);

if (widthMode != MeasureSpec.EXACTLY) {
// 宽度warp_content, 高度必须指定
// 宽度warp_content,或者UNSPECIFIED, 高度必须指定
int widthMakeMeasureSpec = MeasureSpec.makeMeasureSpec((int) (maxWidth + getPaddingRight()), MeasureSpec.EXACTLY);
this.setMeasuredDimension(widthMakeMeasureSpec, heightMeasureSpec);

Expand All @@ -254,12 +254,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
View child = getChildAt(i);
LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
if (layoutParams.centerInParentHorizontal || layoutParams.centerInParent) {
layoutParams.left = (getMeasuredWidth() - child.getMeasuredWidth()) / 2 + layoutParams.marginLeft;
layoutParams.left = (getMeasuredWidth() - child.getMeasuredWidth()) / 2F + layoutParams.marginLeft;
layoutParams.right = layoutParams.left + child.getMeasuredWidth();
}
}
} else if (heightMode != MeasureSpec.EXACTLY) {
// 高度warp_content, 宽度必须指定
// 高度warp_content,或者UNSPECIFIED, 宽度必须指定
int heightMakeMeasureSpec = MeasureSpec.makeMeasureSpec((int) (maxHeight + getPaddingBottom()), MeasureSpec.EXACTLY);
this.setMeasuredDimension(widthMeasureSpec, heightMakeMeasureSpec);

Expand All @@ -268,7 +268,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
View child = getChildAt(i);
LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
if (layoutParams.centerInParentVertical || layoutParams.centerInParent) {
layoutParams.top = (getMeasuredHeight() - child.getMeasuredHeight()) / 2 + layoutParams.marginTop;
layoutParams.top = (getMeasuredHeight() - child.getMeasuredHeight()) / 2F + layoutParams.marginTop;
layoutParams.bottom = layoutParams.top + child.getMeasuredHeight();
}
}
Expand Down Expand Up @@ -431,6 +431,7 @@ protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
}

}

child.measure(layoutParams.resultWidth, layoutParams.resultHeight);


Expand Down Expand Up @@ -499,22 +500,22 @@ protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
}

if (layoutParams.alignParentCenterHorizontalTop) {
layoutParams.bottom = parentHeight / 2 - layoutParams.marginBottom;
layoutParams.bottom = parentHeight / 2F - layoutParams.marginBottom;
if (hasVerticalAttr) {
layoutParams.top = layoutParams.bottom - child.getMeasuredHeight();
}
}

if (layoutParams.alignParentCenterHorizontalBottom) {
layoutParams.top = parentHeight / 2 + layoutParams.marginTop;
layoutParams.top = parentHeight / 2F + layoutParams.marginTop;
if (hasVerticalAttr) {
layoutParams.bottom = layoutParams.top + child.getMeasuredHeight();
}
}

// 与父View的水平中间对齐
if (layoutParams.centerInParentVertical) {
layoutParams.top = parentHeight / 2 - child.getMeasuredHeight() / 2 + layoutParams.marginTop;
layoutParams.top = parentHeight / 2F - child.getMeasuredHeight() / 2F + layoutParams.marginTop;
layoutParams.bottom = layoutParams.top + child.getMeasuredHeight();
}

Expand Down Expand Up @@ -559,7 +560,7 @@ protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
if (layoutParams.alignCenterHorizontalTop > 0) {
View dependencies = findViewById(layoutParams.alignCenterHorizontalTop);
LayoutParams params = (LayoutParams) dependencies.getLayoutParams();
layoutParams.bottom = params.top + dependencies.getMeasuredHeight() / 2 - layoutParams.marginBottom;
layoutParams.bottom = params.top + dependencies.getMeasuredHeight() / 2F - layoutParams.marginBottom;
if (hasVerticalAttr) {
layoutParams.top = layoutParams.bottom - child.getMeasuredHeight();
}
Expand All @@ -568,7 +569,7 @@ protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
if (layoutParams.alignCenterHorizontalBottom > 0) {
View dependencies = findViewById(layoutParams.alignCenterHorizontalBottom);
LayoutParams params = (LayoutParams) dependencies.getLayoutParams();
layoutParams.top = params.top + dependencies.getMeasuredHeight() / 2 + layoutParams.marginTop;
layoutParams.top = params.top + dependencies.getMeasuredHeight() / 2F + layoutParams.marginTop;
if (hasVerticalAttr) {
layoutParams.bottom = layoutParams.top + child.getMeasuredHeight();
}
Expand All @@ -577,7 +578,7 @@ protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
if (layoutParams.centerVerticalOf > 0) {
View dependencies = findViewById(layoutParams.centerVerticalOf);
LayoutParams params = (LayoutParams) dependencies.getLayoutParams();
layoutParams.top = params.top + dependencies.getMeasuredHeight() / 2 - child.getMeasuredHeight() / 2;
layoutParams.top = params.top + dependencies.getMeasuredHeight() / 2F - child.getMeasuredHeight() / 2F;
layoutParams.bottom = layoutParams.top + child.getMeasuredHeight();
}

Expand All @@ -599,23 +600,23 @@ protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
}

if (layoutParams.toParentCenterVerticalLeft) {
layoutParams.right = parentWidth / 2 - layoutParams.marginRight;
layoutParams.right = parentWidth / 2F - layoutParams.marginRight;
if (hasHorizontalAttr) {
layoutParams.left = layoutParams.right - child.getMeasuredWidth();
}
}

// 对齐父View水平中线的右边
if (layoutParams.toParentCenterVerticalRight) {
layoutParams.left = parentWidth / 2 + layoutParams.marginLeft;
layoutParams.left = parentWidth / 2F + layoutParams.marginLeft;
if (hasHorizontalAttr) {
layoutParams.right = layoutParams.left + child.getMeasuredWidth();
}
}

if (layoutParams.centerInParentHorizontal) {
if (parentWidth > 0) {
layoutParams.left = parentWidth / 2 - child.getMeasuredWidth() / 2 + layoutParams.marginLeft;
layoutParams.left = parentWidth / 2F - child.getMeasuredWidth() / 2F + layoutParams.marginLeft;
} else {
layoutParams.left = layoutParams.marginLeft;
}
Expand Down Expand Up @@ -666,7 +667,7 @@ protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
if (layoutParams.toCenterVerticalLeft > 0) {
View dependencies = findViewById(layoutParams.toCenterVerticalLeft);
LayoutParams params = (LayoutParams) dependencies.getLayoutParams();
layoutParams.right = params.left + dependencies.getMeasuredWidth() / 2 - layoutParams.marginLeft;
layoutParams.right = params.left + dependencies.getMeasuredWidth() / 2F - layoutParams.marginLeft;
if (hasHorizontalAttr) {
layoutParams.left = layoutParams.right - child.getMeasuredWidth();
}
Expand All @@ -676,7 +677,7 @@ protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
if (layoutParams.toCenterVerticalRight > 0) {
View dependencies = findViewById(layoutParams.toCenterVerticalRight);
LayoutParams params = (LayoutParams) dependencies.getLayoutParams();
layoutParams.left = params.left + dependencies.getMeasuredWidth() / 2;
layoutParams.left = params.left + dependencies.getMeasuredWidth() / 2F;
if (hasHorizontalAttr) {
layoutParams.right = layoutParams.left + child.getMeasuredWidth();
}
Expand All @@ -687,17 +688,17 @@ protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
if (layoutParams.centerHorizontalOf > 0) {
View dependencies = findViewById(layoutParams.centerHorizontalOf);
LayoutParams params = (LayoutParams) dependencies.getLayoutParams();
layoutParams.left = (params.left + dependencies.getMeasuredWidth() / 2) - child.getMeasuredWidth() / 2;
layoutParams.left = (params.left + dependencies.getMeasuredWidth() / 2F) - child.getMeasuredWidth() / 2F;
layoutParams.right = layoutParams.left + child.getMeasuredWidth();
}


// 影响水平与垂直的属性
if (layoutParams.centerInParent) {
layoutParams.left = parentWidth / 2 - child.getMeasuredWidth() / 2;
layoutParams.left = parentWidth / 2F - child.getMeasuredWidth() / 2F;
layoutParams.right = layoutParams.left + child.getMeasuredWidth();

layoutParams.top = parentHeight / 2 - child.getMeasuredHeight() / 2;
layoutParams.top = parentHeight / 2F - child.getMeasuredHeight() / 2F;
layoutParams.bottom = layoutParams.top + child.getMeasuredHeight();
}

Expand All @@ -706,10 +707,10 @@ protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
View dependencies = findViewById(layoutParams.centerOf);
LayoutParams params = (LayoutParams) dependencies.getLayoutParams();

layoutParams.top = params.top + dependencies.getMeasuredHeight() / 2 - child.getMeasuredHeight() / 2;
layoutParams.top = params.top + dependencies.getMeasuredHeight() / 2F - child.getMeasuredHeight() / 2F;
layoutParams.bottom = layoutParams.top + child.getMeasuredHeight();

layoutParams.left = params.left + dependencies.getMeasuredWidth() / 2 - child.getMeasuredWidth() / 2;
layoutParams.left = params.left + dependencies.getMeasuredWidth() / 2F - child.getMeasuredWidth() / 2F;
layoutParams.right = layoutParams.left + child.getMeasuredWidth();
}

Expand Down
Binary file removed library_aar/dependencylayout-1.1.0.aar
Binary file not shown.
Binary file added library_aar/dependencylayout_1.1.1.aar
Binary file not shown.

0 comments on commit 86f0c8f

Please sign in to comment.