【问题标题】:How to set the fixed position for floating action button above very long listview in Android?如何在Android中非常长的列表视图上方设置浮动操作按钮的固定位置?
【发布时间】:2016-05-22 04:03:39
【问题描述】:

我正在开发一个主要使用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>

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

【问题讨论】:

    标签: android listview floating-action-button


    【解决方案1】:

    尝试使用 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>
    

    【讨论】:

      【解决方案2】:

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

      使用

      FrameLayout,这样您就可以将视图放在其他视图之上

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • 你是爱兄弟:-*
        • android:layout_alignParentEnd="true"android:layout_alignParentRight="true" 有必要吗?当LinearLayoutParentEnd 不匹配ParentRight 会出现什么情况
        猜你喜欢
        • 1970-01-01
        • 2016-07-26
        • 2016-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 2016-03-15
        • 1970-01-01
        相关资源
        最近更新 更多