提交 c462f8f4 编写于 作者: T Takeshi Hagikura 提交者: GitHub

Fix the baseline calculation above API Level 24+ (#342)

上级 5d7f82b9
......@@ -2262,6 +2262,27 @@ public class FlexboxAndroidTest {
assertThat(topPluBaseline2, is(topPluBaseline3));
}
@Test
@FlakyTest
public void testAlignItems_baseline_wrapContent() throws Throwable {
// This test verifies the issue that baseline calculation is broken on API level +24
// https://github.com/google/flexbox-layout/issues/341
final FlexboxTestActivity activity = mActivityRule.getActivity();
FlexboxLayout layout =
createFlexboxLayout(R.layout.activity_align_items_baseline_wrap_content);
TextView textView1 = activity.findViewById(R.id.text1);
TextView textView2 = activity.findViewById(R.id.text2);
TextView textView3 = activity.findViewById(R.id.text3);
int topPluBaseline1 = textView1.getTop() + textView1.getBaseline();
int topPluBaseline2 = textView2.getTop() + textView2.getBaseline();
int topPluBaseline3 = textView3.getTop() + textView3.getBaseline();
assertThat(topPluBaseline1, is(topPluBaseline2));
assertThat(topPluBaseline2, is(topPluBaseline3));
assertThat(layout.getFlexLines().size(), is(1));
assertTrue(layout.getFlexLines().get(0).getCrossSize() > textView1.getHeight());
}
@Test
@FlakyTest
public void testAlignItems_baseline_wrapReverse() throws Throwable {
......
<?xml version="1.0" encoding="utf-8"?><!--
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/flexbox_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:flexDirection="row"
app:flexWrap="wrap"
app:alignItems="baseline">
<TextView
android:id="@+id/text1"
android:layout_width="30dp"
android:layout_height="80dp"
android:text="1"
android:gravity="bottom" />
<TextView
android:id="@+id/text2"
android:layout_width="30dp"
android:layout_height="80dp"
android:text="2"
android:gravity="top" />
<TextView
android:id="@+id/text3"
android:layout_width="30dp"
android:layout_height="80dp"
android:text="3"
android:paddingTop="20dp"
android:gravity="center" />
</com.google.android.flexbox.FlexboxLayout>
......@@ -349,28 +349,30 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
// TODO: Consider the case any individual child's mAlignSelf is set to ALIGN_SELF_BASELINE
if (mAlignItems == AlignItems.BASELINE) {
int viewIndex = 0;
for (FlexLine flexLine : mFlexLines) {
// The largest height value that also take the baseline shift into account
int largestHeightInLine = Integer.MIN_VALUE;
for (int i = viewIndex; i < viewIndex + flexLine.mItemCount; i++) {
View child = getReorderedChildAt(i);
for (int i = 0; i < flexLine.mItemCount; i++) {
int viewIndex = flexLine.mFirstIndex + i;
View child = getReorderedChildAt(viewIndex);
if (child == null || child.getVisibility() == View.GONE) {
continue;
}
LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (mFlexWrap != FlexWrap.WRAP_REVERSE) {
int marginTop = flexLine.mMaxBaseline - child.getBaseline();
marginTop = Math.max(marginTop, lp.topMargin);
largestHeightInLine = Math.max(largestHeightInLine,
child.getHeight() + marginTop + lp.bottomMargin);
child.getMeasuredHeight() + marginTop + lp.bottomMargin);
} else {
int marginBottom = flexLine.mMaxBaseline - child.getMeasuredHeight() +
child.getBaseline();
marginBottom = Math.max(marginBottom, lp.bottomMargin);
largestHeightInLine = Math.max(largestHeightInLine,
child.getHeight() + lp.topMargin + marginBottom);
child.getMeasuredHeight() + lp.topMargin + marginBottom);
}
}
flexLine.mCrossSize = largestHeightInLine;
viewIndex += flexLine.mItemCount;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册