繁体   English   中英

如何在Android中的超长列表视图上方设置浮动操作按钮的固定位置?

[英]How to set the fixed position for floating action button above very long listview in Android?

我正在开发一个主要使用listview的Android应用程序。 但我在使用Floating Action ButtonLong ListView时遇到问题。 我的问题如下。

当列表视图只有几个项目时。 可以看到Floating item

这是截图:

在此输入图像描述

正如您在上面所看到的,仍然可以看到Floating Action Button但是当ListView有太多项目并且对屏幕过度时,无法看到Floating Action Button

这是Long Listview的屏幕截图

在此输入图像描述

对于第二个屏幕截图,它不能再scrolled down

我想要实现的是,我希望浮动底部始终位于Listview上方的屏幕右下方。 就像HTMLCSS固定位置一样。

无论Listview有多高,我都希望Floating Action Button始终在这里

在此输入图像描述

这是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
        <ListView
            android:dividerHeight="@dimen/list_item_divider_height"
            android:padding="@dimen/list_padding"
            android:divider="@color/whitesmoke"
            android:id="@+id/listview_podcast_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></ListView>

        <TextView
            android:textSize="17dp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:textAlignment="center"
            android:id="@+id/tf_no_records"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/stop_podcast_button"
        android:background="@color/colorPrimary"
        android:src="@android:drawable/ic_dialog_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"/>
</LinearLayout>

如何修复我的代码来实现它?

尝试使用RelativeLayout而不是LinearLayout。 而对于FloatingActionButton添加这样的属性,

    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_gravity="end|bottom"

尝试使用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="listview.anubavam.com.listview.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.v7.widget.Toolbar>


    </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_menu_camera" />

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

不要将按钮放在线性布局中。

采用

FrameLayout,因此您可以将Views放在其他人之上

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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