Skip to content

Commit c462f8f

Browse files
authored
Fix the baseline calculation above API Level 24+ (#342)
1 parent 5d7f82b commit c462f8f

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,27 @@ public void testAlignItems_baseline() throws Throwable {
22622262
assertThat(topPluBaseline2, is(topPluBaseline3));
22632263
}
22642264

2265+
@Test
2266+
@FlakyTest
2267+
public void testAlignItems_baseline_wrapContent() throws Throwable {
2268+
// This test verifies the issue that baseline calculation is broken on API level +24
2269+
// https://github.com/google/flexbox-layout/issues/341
2270+
final FlexboxTestActivity activity = mActivityRule.getActivity();
2271+
FlexboxLayout layout =
2272+
createFlexboxLayout(R.layout.activity_align_items_baseline_wrap_content);
2273+
TextView textView1 = activity.findViewById(R.id.text1);
2274+
TextView textView2 = activity.findViewById(R.id.text2);
2275+
TextView textView3 = activity.findViewById(R.id.text3);
2276+
int topPluBaseline1 = textView1.getTop() + textView1.getBaseline();
2277+
int topPluBaseline2 = textView2.getTop() + textView2.getBaseline();
2278+
int topPluBaseline3 = textView3.getTop() + textView3.getBaseline();
2279+
2280+
assertThat(topPluBaseline1, is(topPluBaseline2));
2281+
assertThat(topPluBaseline2, is(topPluBaseline3));
2282+
assertThat(layout.getFlexLines().size(), is(1));
2283+
assertTrue(layout.getFlexLines().get(0).getCrossSize() > textView1.getHeight());
2284+
}
2285+
22652286
@Test
22662287
@FlakyTest
22672288
public void testAlignItems_baseline_wrapReverse() throws Throwable {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
Copyright 2017 Google Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
17+
xmlns:app="http://schemas.android.com/apk/res-auto"
18+
android:id="@+id/flexbox_layout"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
app:flexDirection="row"
22+
app:flexWrap="wrap"
23+
app:alignItems="baseline">
24+
25+
<TextView
26+
android:id="@+id/text1"
27+
android:layout_width="30dp"
28+
android:layout_height="80dp"
29+
android:text="1"
30+
android:gravity="bottom" />
31+
32+
<TextView
33+
android:id="@+id/text2"
34+
android:layout_width="30dp"
35+
android:layout_height="80dp"
36+
android:text="2"
37+
android:gravity="top" />
38+
39+
<TextView
40+
android:id="@+id/text3"
41+
android:layout_width="30dp"
42+
android:layout_height="80dp"
43+
android:text="3"
44+
android:paddingTop="20dp"
45+
android:gravity="center" />
46+
</com.google.android.flexbox.FlexboxLayout>

flexbox/src/main/java/com/google/android/flexbox/FlexboxLayout.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,28 +349,30 @@ private void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) {
349349

350350
// TODO: Consider the case any individual child's mAlignSelf is set to ALIGN_SELF_BASELINE
351351
if (mAlignItems == AlignItems.BASELINE) {
352-
int viewIndex = 0;
353352
for (FlexLine flexLine : mFlexLines) {
354353
// The largest height value that also take the baseline shift into account
355354
int largestHeightInLine = Integer.MIN_VALUE;
356-
for (int i = viewIndex; i < viewIndex + flexLine.mItemCount; i++) {
357-
View child = getReorderedChildAt(i);
355+
for (int i = 0; i < flexLine.mItemCount; i++) {
356+
int viewIndex = flexLine.mFirstIndex + i;
357+
View child = getReorderedChildAt(viewIndex);
358+
if (child == null || child.getVisibility() == View.GONE) {
359+
continue;
360+
}
358361
LayoutParams lp = (LayoutParams) child.getLayoutParams();
359362
if (mFlexWrap != FlexWrap.WRAP_REVERSE) {
360363
int marginTop = flexLine.mMaxBaseline - child.getBaseline();
361364
marginTop = Math.max(marginTop, lp.topMargin);
362365
largestHeightInLine = Math.max(largestHeightInLine,
363-
child.getHeight() + marginTop + lp.bottomMargin);
366+
child.getMeasuredHeight() + marginTop + lp.bottomMargin);
364367
} else {
365368
int marginBottom = flexLine.mMaxBaseline - child.getMeasuredHeight() +
366369
child.getBaseline();
367370
marginBottom = Math.max(marginBottom, lp.bottomMargin);
368371
largestHeightInLine = Math.max(largestHeightInLine,
369-
child.getHeight() + lp.topMargin + marginBottom);
372+
child.getMeasuredHeight() + lp.topMargin + marginBottom);
370373
}
371374
}
372375
flexLine.mCrossSize = largestHeightInLine;
373-
viewIndex += flexLine.mItemCount;
374376
}
375377
}
376378

0 commit comments

Comments
 (0)