Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for space evenly in AlignContent #620

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ internal class FragmentHelper(private val activity: MainActivity, private val fl
SPACE_BETWEEN -> AlignContent.SPACE_BETWEEN
SPACE_AROUND -> AlignContent.SPACE_AROUND
STRETCH -> AlignContent.STRETCH
SPACE_EVENLY -> AlignContent.SPACE_EVENLY
else -> return
}
}
Expand All @@ -317,6 +318,7 @@ internal class FragmentHelper(private val activity: MainActivity, private val fl
AlignContent.SPACE_BETWEEN -> return SPACE_BETWEEN
AlignContent.SPACE_AROUND -> return SPACE_AROUND
AlignContent.STRETCH -> return STRETCH
AlignContent.SPACE_EVENLY -> return SPACE_EVENLY
else -> return STRETCH
}
}
Expand Down
1 change: 1 addition & 0 deletions demo-playground/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ limitations under the License.
<item>@string/space_between</item>
<item>@string/space_around</item>
<item>@string/stretch</item>
<item>@string/space_evenly</item>
</string-array>

<string-array translatable="false" name="array_align_self">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

/** This attribute controls the alignment of the flex lines in the flex container. */
@IntDef({AlignContent.FLEX_START, AlignContent.FLEX_END, AlignContent.CENTER,
AlignContent.SPACE_BETWEEN, AlignContent.SPACE_AROUND, AlignContent.STRETCH})
AlignContent.SPACE_BETWEEN, AlignContent.SPACE_AROUND, AlignContent.STRETCH,
AlignContent.SPACE_EVENLY})
@Retention(RetentionPolicy.SOURCE)
public @interface AlignContent {

Expand Down Expand Up @@ -53,4 +54,6 @@
* Flex lines are stretched to fill the remaining space along the cross axis.
*/
int STRETCH = 5;

int SPACE_EVENLY = 6;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,36 @@ void determineCrossSize(int widthMeasureSpec, int heightMeasureSpec,
mFlexContainer.setFlexLines(newFlexLines);
break;
}
case AlignContent.SPACE_EVENLY: {
if (totalCrossSize >= size) {
// If the size of the content is larger than the flex container, the
// Flex lines should be aligned center like ALIGN_CONTENT_CENTER
mFlexContainer.setFlexLines(
constructFlexLinesForAlignContentCenter(flexLines, size,
totalCrossSize));
break;
}
// The value of free space along the cross axis which needs to be put on top
// and below the bottom of each flex line.
int spaceTopAndBottom = size - totalCrossSize;
// The number of spaces along the cross axis
int numberOfSpaces = flexLines.size() + 1;
spaceTopAndBottom = spaceTopAndBottom / numberOfSpaces;
List<FlexLine> newFlexLines = new ArrayList<>();
FlexLine dummySpaceFlexLine = new FlexLine();
dummySpaceFlexLine.mCrossSize = spaceTopAndBottom;
boolean isFirstLine = true;
for (FlexLine flexLine : flexLines) {
if (isFirstLine) {
newFlexLines.add(dummySpaceFlexLine);
isFirstLine = false;
}
newFlexLines.add(flexLine);
newFlexLines.add(dummySpaceFlexLine);
}
mFlexContainer.setFlexLines(newFlexLines);
break;
}
case AlignContent.SPACE_BETWEEN: {
if (totalCrossSize >= size) {
break;
Expand Down