提交 6599adbf 编写于 作者: T Takeshi Hagikura

Implement stretching the views by taking ALIGN_ITEMS_STRETCH and

ALIGN_SELF_STRETCH into account.

Change-Id: I4915c77407decdd218db1117a7697ea8f9ddd7cf
上级 a40d702c
......@@ -28,6 +28,7 @@ import static android.support.test.espresso.assertion.PositionAssertions.isRight
import static android.support.test.espresso.assertion.PositionAssertions.isTopAlignedWith;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
......@@ -611,6 +612,66 @@ public class FlexboxAndroidTest {
assertThat(textView3.getTop(), is(textView1.getHeight()));
}
@Test
public void testAlignItems_stretch() throws Throwable {
final FlexboxTestActivity activity = mActivityRule.getActivity();
mActivityRule.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.setContentView(R.layout.activity_stretch_test);
}
});
FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout);
assertThat(flexboxLayout.getAlignItems(), is(FlexboxLayout.ALIGN_ITEMS_STRETCH));
onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout)));
onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout)));
onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout)));
onView(withId(R.id.text2)).check(isRightOf(withId(R.id.text1)));
onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout)));
onView(withId(R.id.text3)).check(isBelow(withId(R.id.text1)));
onView(withId(R.id.text3)).check(isBelow(withId(R.id.text2)));
// There should be 2 flex lines in the layout with the given layout.
int flexLineSize = flexboxLayout.getHeight() / 2;
TextView textView1 = (TextView) activity.findViewById(R.id.text1);
TextView textView2 = (TextView) activity.findViewById(R.id.text2);
TextView textView3 = (TextView) activity.findViewById(R.id.text3);
assertThat(textView1.getHeight(), is(flexLineSize));
assertThat(textView2.getHeight(), is(flexLineSize));
assertThat(textView3.getHeight(), is(flexLineSize));
}
@Test
public void testAlignSelf_stretch() throws Throwable {
final FlexboxTestActivity activity = mActivityRule.getActivity();
mActivityRule.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.setContentView(R.layout.activity_align_self_stretch_test);
}
});
FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout);
onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout)));
onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout)));
onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout)));
onView(withId(R.id.text2)).check(isRightOf(withId(R.id.text1)));
onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout)));
onView(withId(R.id.text3)).check(isBelow(withId(R.id.text1)));
onView(withId(R.id.text3)).check(isBelow(withId(R.id.text2)));
// There should be 2 flex lines in the layout with the given layout.
// Only the first TextView's alignSelf is set to ALIGN_SELF_STRETCH
int flexLineSize = flexboxLayout.getHeight() / 2;
TextView textView1 = (TextView) activity.findViewById(R.id.text1);
TextView textView2 = (TextView) activity.findViewById(R.id.text2);
TextView textView3 = (TextView) activity.findViewById(R.id.text3);
assertThat(textView1.getHeight(), is(flexLineSize));
assertThat(textView2.getHeight(), not(flexLineSize));
assertThat(textView3.getHeight(), not(flexLineSize));
}
private TextView createTextView(Context context, String text, int order) {
TextView textView = new TextView(context);
textView.setText(text);
......
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2016 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.libraries.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="360dp"
android:layout_height="360dp"
app:flexDirection="row"
app:flexWrap="wrap"
app:alignItems="flex_start"
app:alignContent="stretch"
>
<TextView
android:id="@+id/text1"
android:layout_width="140dp"
android:layout_height="80dp"
android:text="1"
app:layout_alignSelf="stretch"
/>
<TextView
android:id="@+id/text2"
android:layout_width="140dp"
android:layout_height="80dp"
android:text="2"
/>
<TextView
android:id="@+id/text3"
android:layout_width="140dp"
android:layout_height="80dp"
android:text="3"
/>
</com.google.android.libraries.flexbox.FlexboxLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2016 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.libraries.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="360dp"
android:layout_height="360dp"
app:flexDirection="row"
app:flexWrap="wrap"
app:alignItems="stretch"
>
<TextView
android:id="@+id/text1"
android:layout_width="140dp"
android:layout_height="80dp"
android:text="1"
/>
<TextView
android:id="@+id/text2"
android:layout_width="140dp"
android:layout_height="80dp"
android:text="2"
/>
<TextView
android:id="@+id/text3"
android:layout_width="140dp"
android:layout_height="80dp"
android:text="3"
/>
</com.google.android.libraries.flexbox.FlexboxLayout>
\ No newline at end of file
......@@ -347,7 +347,22 @@ public class FlexboxLayout extends ViewGroup {
}
}
// TODO: handle stretch attributes
// Now cross size for each flex line is determined.
// Expand the views if alignItems (or alignSelf in each child view) is stretch
if (mAlignItems == ALIGN_ITEMS_STRETCH) {
int viewIndex = 0;
for (FlexLine flexLine : mFlexLines) {
for (int i = 0; i < flexLine.itemCount; i++, viewIndex++) {
stretchViewVerticallyAt(viewIndex, flexLine.crossSize);
}
}
} else {
for (FlexLine flexLine : mFlexLines) {
for (Integer index : flexLine.indicesAlignSelfStretch) {
stretchViewVerticallyAt(index, flexLine.crossSize);
}
}
}
// Set this FlexboxLayout's width and height depending on the calculated length of main axis
// and cross axis.
......@@ -415,6 +430,21 @@ public class FlexboxLayout extends ViewGroup {
}
}
/**
* Expand the view vertically to the size of the crossSize (considering the view margins)
* @param index the absolute index of the view to be expanded
* @param crossSize the cross size
*/
private void stretchViewVerticallyAt(int index, int crossSize) {
View child = getReorderedChildAt(index);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
int newHeight = crossSize - lp.topMargin - lp.bottomMargin;
newHeight = Math.max(newHeight, 0);
child.measure(MeasureSpec
.makeMeasureSpec(child.getMeasuredWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
}
/**
* Determine if a wrap is required (add a new flex line).
*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册