简体   繁体   English

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

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

I am developing an Android app that is mostly working with listview . 我正在开发一个主要使用listview的Android应用程序。 But I am having a problem using Floating Action Button together with Long ListView . 但我在使用Floating Action ButtonLong ListView时遇到问题。 My problem is as below. 我的问题如下。

When the list view is only has a few item. 当列表视图只有几个项目时。 Floating item can be seen. 可以看到Floating item

This is the screenshot: 这是截图:

在此输入图像描述

As you can see above, Floating Action Button is still can be seen.But when ListView has so many items and become excessive to the screen, Floating Action Button cannot been seen. 正如您在上面所看到的,仍然可以看到Floating Action Button但是当ListView有太多项目并且对屏幕过度时,无法看到Floating Action Button

This is the screenshot of Long Listview 这是Long Listview的屏幕截图

在此输入图像描述

For the second screen shot, it cannot be scrolled down any more. 对于第二个屏幕截图,它不能再scrolled down

What I want to achieve is, I want the floating bottom always at the bottom right of screen above Listview . 我想要实现的是,我希望浮动底部始终位于Listview上方的屏幕右下方。 Like fixed position in HTML and CSS . 就像HTMLCSS固定位置一样。

I want Floating Action Button always here no matter how tall the Listview is 无论Listview有多高,我都希望Floating Action Button始终在这里

在此输入图像描述

This is my layout file 这是我的布局文件

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

How can I fix my code to achieve it? 如何修复我的代码来实现它?

Try using RelativeLayout instead of LinearLayout. 尝试使用RelativeLayout而不是LinearLayout。 And for FloatingActionButton add attribute like this, 而对于FloatingActionButton添加这样的属性,

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

Try using with CoordinatorLayout 尝试使用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>

Don't place button in a Linear Layout . 不要将按钮放在线性布局中。

Use 采用

FrameLayout so you can place Views on top of others FrameLayout,因此您可以将Views放在其他人之上

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

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