material-components / material-components/material-components-android

[AppBarLayout] `fitsSystemWindows="true"` is ignored if wrapped inside `CoordinatorLayout` without `fitsSystemWindows`

Offen
#4,541 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug Widget: TopAppBar
Vorherrschende Sprache
Java
Sterne
17.4k
Forks
3.2k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Description: With SDK 35 I had another look at edge-to-edge interactions. In my case I have a androidx.coordinatorlayout.widget.CoordinatorLayout as the root layout with a nested com.google.android.material.appbar.AppBarLayout that has android:fitsSystemWindows="true". The activity sets WindowCompat.setDecorFitsSystemWindows(getWindow(), false); on creation. However, android:fitsSystemWindows doesn't seem to have any influence on the visuals, so any text from the toolbar (and potential menu controls) are behind transparent controls (if present).

As a workaround I wrote this snippet to apply the required padding manually:

protected void addSystemPaddingToComponentNoBottom(View view) {
    int originalLeftPadding = view.getPaddingLeft();
    int originalTopPadding = view.getPaddingTop();
    int originalRightPadding = view.getPaddingRight();
    int originalBottomPadding = view.getPaddingBottom();

    ViewCompat.setOnApplyWindowInsetsListener(view, (v, windowInsets) -> {
        Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout());
        int leftPadding = originalLeftPadding + insets.left;
        int topPadding = originalTopPadding + insets.top;
        int rightPadding = originalRightPadding + insets.right;

        view.setPadding(leftPadding, topPadding, rightPadding, originalBottomPadding);

        return windowInsets;
    });
}

If I apply this to all com.google.android.material.appbar.AppBarLayouts in my app, everything works as expected.

Expected behavior: I would android:fitsSystemWindows="true" to apply this inset by itself. it works without issues when using it on the root CoordinatorLayout (which I want to be visible behind the controls, so that's why this is not an option).

Source code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:liftOnScroll="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/top_app_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:importantForAccessibility="no"
        android:paddingStart="@dimen/recycler_view_outer_padding"
        android:paddingEnd="@dimen/recycler_view_outer_padding"
        android:paddingBottom="80dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/add_pack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="@dimen/floating_action_button_margin"
            android:contentDescription="@string/add_new"
            android:onClick="add"
            android:src="@drawable/plus_symbol" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Please not how android:fitsSystemWindows="true" works for the nested CoordinatorLayout without any issues.

Android API version: 35

Material Library version: 1.12.0

Device: Probably all of them, tested in Emulator with Pixel 4a API 31

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere die SDK 35-Konfiguration mit dem bereitgestellten CoordinatorLayout, dem verschachtelten AppBarLayout und dem Setup für WindowCompat.setDecorFitsSystemWindows. Beginne damit, die Verarbeitung der Window-Insets durch AppBarLayout und CoordinatorLayout nachzuverfolgen, und vergleiche sie mit dem verschachtelten CoordinatorLayout, das funktioniert. Als abgeschlossen gilt die Aufgabe, wenn AppBarLayout die erwarteten oberen und seitlichen Systemleisten-Insets ohne manuelles Padding anwendet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
android
Bereich
mobile-dev
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.