当前位置: 代码迷 >> Android >> 浮动动作按钮滚动显示内容
  详细解决方案

浮动动作按钮滚动显示内容

热度:35   发布时间:2023-08-04 11:18:57.0

我在布局中实现了一个浮动操作按钮。 问题是当布局中的数据过多时,按钮未停留在同一位置,而是滚动到布局的底部。 我需要像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>

这是怎么回事?

感谢您的帮助!

您可以看到此代码,它将与您一起正常工作

<?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>

您需要为此尝试这个的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布局中,放置所有必要的代码。

  相关解决方案