Выравнивание FAB по правому нижнему углу

У меня есть этот файл макета. Где я пытаюсь добавить FloatingActionButton. Я не могу разместить его в правом нижнем углу.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <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="@dimen/action_bar_size"
            android:background="@color/myPrimaryColor"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways" />

        <TextView
            android:id="@+id/offline_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#ff0000"
            android:gravity="center"
            android:padding="5dp"
            android:text="No Internet connection"
            android:textColor="@android:color/white" />

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

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_drawer_header"
    app:menu="@menu/navigation_drawer_menu" />

</android.support.v4.widget.DrawerLayout>

Невероятная часть

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:src="@drawable/ic_create_white"
    app:elevation="3dp"
    app:fabSize="normal"
    app:pressedTranslationZ="6dp"
    app:rippleColor="@color/white" />

Но где бы я ни размещал это в макете выше, это происходит не просто так. Любая помощь будет оценена.

ИЗМЕНИТЬ FrameLayout будет заменено на Fragment во время выполнения.


person Kavin Prabhu    schedule 31.07.2015    source источник
comment
проверьте, помогает ли это   -  person Blackbelt    schedule 31.07.2015


Ответы (2)


Почему вы не пытаетесь использовать FrameLayout в качестве родительского макета, чтобы вы могли поместить FAB в любое место, как показано ниже:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.widget.DrawerLayout 
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <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="@dimen/action_bar_size"
            android:background="@color/myPrimaryColor"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways" />

        <TextView
            android:id="@+id/offline_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#ff0000"
            android:gravity="center"
            android:padding="5dp"
            android:text="No Internet connection"
            android:textColor="@android:color/white" />

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

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_drawer_header"
    app:menu="@menu/navigation_drawer_menu" />

</android.support.v4.widget.DrawerLayout>


<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="@drawable/ic_create_white"
app:elevation="3dp"
app:fabSize="normal"
app:pressedTranslationZ="6dp"
app:rippleColor="@color/white" />

</FrameLayout>

Как видите, вы поместили FAB над основным макетом :)

Я надеюсь, что это поможет вам :)

person Mohammad Salem    schedule 31.07.2015

поместил это после NavigationView

<FrameLayout
    android:id="@+id/fab_rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="@dimen/fab_margin_bottom"
        android:layout_marginRight="@dimen/fab_margin_right"
        android:src="@drawable/ic_add_shopping_cart_white_18dp"
        app:backgroundTint="@color/accent"
        app:borderWidth="0dp"
        app:elevation="6dp"
        app:fabSize="normal"
        app:pressedTranslationZ="12dp"
        app:rippleColor="@android:color/white"/>

</FrameLayout>
person Abhishek    schedule 31.07.2015