ConstraintLayout: Anchor view to center of parent, while bounding its dimensions to not overlap siblings
Asked Answered
N

0

6

I have three views lined up horizontally in a ConstraintLayout. I need the center view to be anchored to the center of the parent. The tricky part is that all three views have dynamic widths.

If I constrain the center view horizontally to the two views at the ends, the center view will not be centered relative to the parent if the other two views have different widths. Instead it will be centered in the free space between the two views.

If I constrain the center view horizontally to the parent, it will be centered relatively to the parent. However, the center view can then grow wide enough to overlap with the two views at the ends.

I need to be able to anchor the center view while at the same time bounding its width so that it cannot overlap with the two views at the end.

I have tried this with LinearLayout, RelativeLayout, and ConstraintLayout, but none of them seems to be able to satisfy both of the criteria.

Any help would be appreciated!

Nilla answered 4/11, 2019 at 21:46 Comment(2)
I'm facing the exact same issue, did you find a solution to this situation?Sexual
I managed a workaround, but it involves code: https://mcmap.net/q/1625770/-constraintlayout-center-view-on-full-screen-but-limit-width-to-not-overlap-with-side-viewsKalindi

© 2022 - 2024 — McMap. All rights reserved.