简体   繁体   中英

Floating action button anchor

I need to anchor floating action button in android, but have the trouble: fab will be in navigationview Here is my activity_main layout. It includes navigation drawer.

<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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground">
    <!-- The main content view -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            layout="@layout/toolbar_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
    <!-- The navigation drawer -->
    <include
        layout="@layout/nav_drawer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>

Navigation drawer layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/clDrawer"
    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:background="@color/colorPrimary">

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

        <android.support.design.widget.NavigationView
            android:id="@+id/navView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/menu_drawer_view"
            app:headerLayout="@layout/anav_header"
            android:background="@color/colorPrimary2"
            app:itemIconTint="@color/colorIcon"
            app:itemTextColor="@color/colorIcon"/>
    </LinearLayout>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/navFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:clickable="true"
        android:src="@drawable/plus_icon"
        app:backgroundTint="@color/colorIcon"
        app:layout_anchor="@id/headerLayout"
        app:layout_anchorGravity="bottom|right|end" />

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

And my anav_header for navigation view contains only the few text view. I need fab to be anchored to the botom of the header, right between header and main content of ND. The problem is layout_anchor works only if header and nav view are in the same layout. if i use navigationview property headerLayout , layout_anchor doesnt work. Also if i use NavigationView.addHeaderView() , it doesnt work too. If i include header in navigation view layout by that way

<include
        layout="@layout/anav_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

it works, BUT header and navigation view move like different parts. And i need them to act like one navigation drawer. I need navigation drawer to lookes smth like that.

I have found my way. First i've removed NavigationView and replaced it with drawer_header.xml with is simple LinearLayout with header image, and with drawer_list just after it with is a LinearLayout too with static menu items.

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary2">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:background="@color/colorPrimary2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- <android.support.design.widget.NavigationView
                 android:id="@+id/navView"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:background="@color/colorPrimary2"
                 app:headerLayout="@layout/drawer_header"
                 app:itemIconTint="@color/colorIcon"
                 app:itemTextColor="@color/colorIcon"
                 app:menu="@menu/menu_drawer_view" />-->
            <include
                layout="@layout/drawer_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/activity_horizontal_margin"/>
            <include
                layout="@layout/layout_drawer_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </ScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/navFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:clickable="true"
        android:src="@drawable/plus_icon"
        app:backgroundTint="@color/colorIcon"
        app:layout_anchor="@id/headerLayout"
        app:layout_anchorGravity="bottom|right|end" />

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

So, i made header and menu as LinearLayout , otherwise they both have scrolled, but i need em to scroll together. To add elements in header or menu you can use addView(View v) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM