简体   繁体   中英

How to add Floating Action Button in fragment?

I want to implement bottom navigation bar in my application with different fragments and each fragment contain different FloatingActionButton and if i click on floating action new activity will open. it is possible?. Can anyone provide code or refer document or video any thing that help me.

Here is my app i want add FAB in it and different FAB in Different fragment:

在此处输入图片说明

Open your fragment UI xml file. add the FloatingActionButton where you want to show:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@drawable/ic_my_icon"
        android:contentDescription="@string/submit"
        android:layout_margin="16dp" />

from google android documentation: Add a Floating Action Button

Here is a YT video that explains how to create a bottom navigation.

https://youtu.be/jpaHMcQDaDg

When you are done with the creation of the bottom navigation just add a FAB in each fragment you want. With the following code in your fragment XML.

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginBottom="32dp"
        android:src="@drawable/ic_add_alarm_black_24dp"
        android:background="@color/colorAccent"
        app:backgroundTint="@color/colorAccent"
        app:fabSize="normal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

Also add the following dependency to your gradle.

implementation 'com.google.android.material:material:1.0.0'

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