Home » Android » android – match_parent doesn't work with CoordinatorLayout

android – match_parent doesn't work with CoordinatorLayout

Posted by: admin May 14, 2020 Leave a comment

Questions:

I use the Android Design Support Library (com.android.support:appcompat-v7:22.2.0) and I have a LinearLayout in CoordinatorLayout like this:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <!-- Content -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#44ff44"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yo Yo"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yo Yo"
            />

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout> 

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:itemIconTint="@color/nav_item_icon_tint_color"
    app:itemTextColor="@color/nav_item_text_color"
    app:menu="@menu/navigation_drawer_items" />

In the code I have changed the background colour and I expect the BG should fill all on the display (because I use match_parent in layout_width and layout_height) but I got the BG colour like this:

enter image description here

Look like it shows with wrap_content more than use match_parent.
I try to use layout_weight in LinearLayout and layout_centerInParent and it doesn’t work also.

What I did wrong?. Or is it an API bug?

Update:**

To fulfil the background I can use android:layout_gravity="fill_vertical" (Suggest by @Abdullah). But another my point for this question is I want the match_parent, layout_weight in LinearLayout or layout_centerInParent and other relative values in RelativeLayout to work properly under the CoordinatorLayout. Right now when I add fragment to the LinearLayout, the layout alignment (in fragment layout) is not same as my expectation. Everything that using ratio or relative value does not work.

Thank you

How to&Answers:

The behavior appbar_scrolling_view_behavior should be applied to a View that’s implement ScrollingView.

Remove the behavior if you don’t need auto-hide of the ToolbarLayout when scrolling the main content or use a RecyclerView instead of the LinearLayout.