Place a FAB in a Fragment

不问归期 提交于 2019-12-12 01:37:21

问题


Good afternoon,

I am trying to put a FAB at the bottom|end of a fragment, but I do not know where I can place it.

Here is the Fragment's xml code :

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/cars_list_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:id="@+id/scrollable_layout">
                <include layout="@layout/write_comment"/>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.SwipeRefreshLayout>

And the FAB :

<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:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_plus" />

Thanks for your help !

An answer helped me to have a first "solution" with this code :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/cars_list_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:id="@+id/scrollable_layout">
                    <include layout="@layout/write_comment"/>

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recyclerview"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />

                </LinearLayout>
        </android.support.design.widget.CoordinatorLayout>
    </android.support.v4.widget.SwipeRefreshLayout>

    <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:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_plus"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>
</RelativeLayout>

But it does not work correctly :


回答1:


Try this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/cars_list_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/scrollable_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
        </android.support.design.widget.CoordinatorLayout>
    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_photo_camera_white"
        app:backgroundTint="@color/colorFabButton" />
</RelativeLayout>



回答2:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">



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

            <ImageView
                android:id="@+id/image12"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:alpha="0.5"
                android:src="@drawable/lawbook"
                android:scaleType="fitXY"/>
            <de.hdodenhof.circleimageview.CircleImageView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/profile_image"

                android:layout_width="120dp"
                android:layout_height="120dp"
                android:src="@drawable/aliyar"

                android:layout_above="@+id/text121"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="71dp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/image12"
                android:textSize="14sp"
                android:layout_marginTop="40dp"
                android:id="@+id/text121"
                android:text="@string/aliyar1"
                android:gravity="center_horizontal"

                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/text121"
                android:layout_marginTop="5dp"
                android:textSize="14sp"
                android:text="@string/aliyar2"
                android:id="@+id/text122"
                android:gravity="center_horizontal"

                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/text122"
                android:layout_marginTop="5dp"
                android:text="@string/aliyar3"
                android:textSize="14sp"
                android:id="@+id/text123"
                android:gravity="center_horizontal"

                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/text123"
                android:layout_marginTop="5dp"
                android:text="@string/aliyar4"
                android:id="@+id/text124"
                android:textSize="14sp"
                android:gravity="center_horizontal"

                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/text124"
                android:layout_marginTop="5dp"
                android:text="@string/aliyar5"
                android:id="@+id/text125"
                android:textSize="14sp"
                android:gravity="center_horizontal"

                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/text125"
                android:layout_marginTop="5dp"
                android:text="@string/aliyar6"
                android:textSize="14sp"
                android:id="@+id/text126"
                android:gravity="center_horizontal"

                />




        </RelativeLayout>





    </ScrollView>

</RelativeLayout>



回答3:


Check below Layout :

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

    <include
        layout="@layout/contentcontact"/>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/facebookfab"
        android:layout_gravity="bottom|right"
        android:layout_width="299dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        app:backgroundTint="#fff"
        android:src="@mipmap/facebook" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/callfab"
        android:layout_gravity="bottom|left"
        android:layout_width="299dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        app:backgroundTint="@color/Color_Blue"

        android:src="@drawable/ic_local_phone_black_24dp" />


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


来源:https://stackoverflow.com/questions/38995358/place-a-fab-in-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!