Search code examples
androidandroid-linearlayoutandroid-coordinatorlayoutandroid-layout-weightandroid-layoutparams

Linear layout match parent or weight doesn't work in Coordinator layout


<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
    android:fitsSystemWindows="true"
    tools:context="com.now.sexyfacechanger.Activity_PhotoOrGallery">

    <include layout="@layout/content_activity__photo_or_gallery" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_camera"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_camera"
            android:layout_weight="1"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_gallery"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_weight="1"/>

    </LinearLayout>


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

Here is the view in android studio design, the linear layout displayed does match the parent:

enter image description here

But the output:

enter image description here

The linear layout doesn't match parent or I suspect the layout_weight applied to the two floating buttons doesn't work in Coordinator layout.


Solution

  • It is because FloatingActionButton can only have a fixed size.

    You can add some space between them.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">
        <Space    
            android:layout_weight="1"     
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_camera"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_camera"/>
        <Space    
            android:layout_weight="2"     
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_gallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_gallery"/>
        <Space    
            android:layout_weight="1"     
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
    </LinearLayout>