chrisbanes / insetter

Insetter is a library to help apps handle WindowInsets more easily
https://chrisbanes.github.io/insetter
Apache License 2.0
1.13k stars 42 forks source link

Insets-Margins are not applied for views with layout_marginEnd or layout_marginStart #128

Closed ChristophKaser closed 1 day ago

ChristophKaser commented 2 months ago

When I try to use the Insetter with margins on a view with layout_marginEnd, no horizontal margins are applied.

This is caused by the method "setMargins" used in ViewGroup.MarginLayoutParams.updateMargins, which internally sets the NEED_RESOLUTION_MASK layout flag, which causes the specified rightMargin to be ignored.

To reproduce, use this code:

Insetter.builder()
    .margin(
        WindowInsetsCompat.Type.systemBars()
        | WindowInsetsCompat.Type.displayCutout()
        | WindowInsetsCompat.Type.ime(),
        Side.BOTTOM | Side.RIGHT | Side.LEFT
)
.applyToView(binding.addButton);

with a FAB that is defined like this:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
chrisbanes commented 2 months ago

I'm afraid that Insetter is basically in maintenance mode. Feel free to submit PRs though if you want to fix it yourself.

sebastianharder commented 1 week ago

@ChristophKaser, sure that marginEnd is the issue? I just spent a lot of time trying to figure out a margin issue and it turned out that the layout_gravity attribute was the problem.

ChristophKaser commented 1 week ago

Yes, I am pretty sure that my issue was solved by using "marginHorizontal" instead of "marginEnd". I did not see issues with layout_gravity, though that might depend on the parent layout.