浮动操作按钮滚动内容

时间:2017-02-15 17:54:43

标签: android android-layout floating-action-button

我在布局中实现了一个浮动操作按钮。问题是当我的布局中有太多数据时,按钮不会停留在相同的位置,它会滚动到布局的底部。我需要像gmail收件箱一样:一个浮动按钮,无论数据如何都可以访问。

这是我显示按钮的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <include
        android:id="@+id/frgOffline"
        android:visibility="gone"
        layout="@layout/frg_offline"/>


    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" card_view:cardBackgroundColor="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="7dp"
        android:layout_marginTop="7dp"
        android:gravity="center_vertical|left">

            <LinearLayout
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_marginTop="25dp"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="text"
                    android:layout_marginBottom="15dp"
                    android:textStyle="bold"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="15dp"
                    android:layout_margin="5dp"
                    android:textAppearance="?android:attr/textAppearanceSmall"/>

                <com.exampleapp.expandiblelist
                    android:id="@+id/expListaResultado"
                    android:groupIndicator="@null"
                    android:layout_height="fill_parent"
                    android:transcriptMode="disabled"
                    android:layout_width="match_parent"/>
            </LinearLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnNueva"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="4dp"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:src="@drawable/ic_nuevo"
            android:layout_gravity="bottom|right"
            app:backgroundTint="@color/blue"/>

        </android.support.v7.widget.CardView>
</LinearLayout>

发生了什么?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您可以看到此代码可以正常使用

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" card_view:cardBackgroundColor="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginBottom="7dp"
    android:layout_marginTop="7dp"
    android:gravity="center_vertical|left">

    <LinearLayout
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginTop="25dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="text"
            android:layout_marginBottom="15dp"
            android:textStyle="bold"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:layout_margin="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

        <com.exampleapp.expandiblelist
            android:id="@+id/expListaResultado"
            android:groupIndicator="@null"
            android:layout_height="fill_parent"
            android:transcriptMode="disabled"
            android:layout_width="match_parent"/>
    </LinearLayout>



</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/btnNueva"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:elevation="4dp"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="5dp"
    android:src="@mipmap/ic_launcher"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginRight="16dp"
   />
</RelativeLayout>

答案 1 :(得分:0)

你需要coordinatorlayout 尝试这个,它可能会有所帮助

<?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="info.androidhive.fab.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

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

</android.support.design.widget.CoordinatorLayout>
<_>在content_main布局中填写所有必要的代码..