如何在滚动视图顶部添加浮动操作按钮 [英] How to add floating action button on top of scrollview

查看:25
本文介绍了如何在滚动视图顶部添加浮动操作按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个浮动操作按钮,我想将其添加到滚动视图之上,以便该按钮保持不变,即使您滚动也是如此.我希望它位于滚动视图的顶部和屏幕的右下角.我需要使用哪些视图组合来完成此操作?

I have a floating action button that I would like to add on top of a scrollview so that the button stays, even if you scroll. I want it on the top of the scrollview and in the bottom right corner of the screen. What combination of views do I need to use to accomplish this?

这里是xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nhscoding.safe2tell.STORIES"
    android:background="@color/stor_back">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        >

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/pink_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_icon="@drawable/ic_add"
            fab:fab_colorNormal="@color/fab_back"
            fab:fab_colorPressed="@color/fab_pressed_back"
            android:layout_gravity="end"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="16dp"/>
        </LinearLayout>


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">



            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view1"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginTop="@dimen/card_margin_top"
                android:layout_marginBottom="@dimen/card_margin_bottom"
                android:layout_marginLeft="@dimen/card_margin_left"
                android:layout_marginRight="@dimen/card_margin_right"
                card_view:cardCornerRadius="@dimen/card_radius"
                >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/card_title_top"
                    android:layout_marginBottom="@dimen/card_title_bottom"
                    android:layout_marginLeft="@dimen/card_title_left"
                    android:layout_marginRight="@dimen/card_title_right"
                    android:text="@string/card1_title"
                    android:textSize="@dimen/card_title_size"
                    android:gravity="top"
                    android:id="@+id/title1"
                    />


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

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view2"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginTop="@dimen/card_margin_top"
                android:layout_marginBottom="@dimen/card_margin_bottom"
                android:layout_marginLeft="@dimen/card_margin_left"
                android:layout_marginRight="@dimen/card_margin_right"
                card_view:cardCornerRadius="@dimen/card_radius"
                android:layout_below="@id/card_view1">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/card_title_top"
                    android:layout_marginBottom="@dimen/card_title_bottom"
                    android:layout_marginLeft="@dimen/card_title_left"
                    android:layout_marginRight="@dimen/card_title_right"
                    android:text="@string/card2_title"
                    android:textSize="@dimen/card_title_size"
                    android:gravity="top"
                    />

                <TextView
                    android:id="@+id/info_text2"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="0dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view3"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginTop="@dimen/card_margin_top"
                android:layout_marginBottom="@dimen/card_margin_bottom"
                android:layout_marginLeft="@dimen/card_margin_left"
                android:layout_marginRight="@dimen/card_margin_right"
                card_view:cardCornerRadius="@dimen/card_radius"
                android:layout_below="@id/card_view2">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/card_title_top"
                    android:layout_marginBottom="@dimen/card_title_bottom"
                    android:layout_marginLeft="@dimen/card_title_left"
                    android:layout_marginRight="@dimen/card_title_right"
                    android:text="@string/card3_title"
                    android:textSize="@dimen/card_title_size"
                    android:gravity="top"
                    />

                <TextView
                    android:id="@+id/info_text3"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="0dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"/>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view1"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginTop="@dimen/card_margin_top"
                android:layout_marginBottom="@dimen/card_margin_bottom"
                android:layout_marginLeft="@dimen/card_margin_left"
                android:layout_marginRight="@dimen/card_margin_right"
                card_view:cardCornerRadius="@dimen/card_radius">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/card_title_top"
                    android:layout_marginBottom="@dimen/card_title_bottom"
                    android:layout_marginLeft="@dimen/card_title_left"
                    android:layout_marginRight="@dimen/card_title_right"
                    android:text="@string/card4_title"
                    android:textSize="@dimen/card_title_size"
                    android:gravity="top"
                    android:id="@+id/title4"
                    />


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

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view1"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginTop="@dimen/card_margin_top"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="@dimen/card_margin_left"
                android:layout_marginRight="@dimen/card_margin_right"
                card_view:cardCornerRadius="@dimen/card_radius">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/card_title_top"
                    android:layout_marginBottom="@dimen/card_title_bottom"
                    android:layout_marginLeft="@dimen/card_title_left"
                    android:layout_marginRight="@dimen/card_title_right"
                    android:text="@string/card5_title"
                    android:textSize="@dimen/card_title_size"
                    android:gravity="top"
                    android:id="@+id/title5"
                    />


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


            </LinearLayout>
        </ScrollView>




</RelativeLayout>

推荐答案

RelativeLayout 中较晚的孩子倾向于浮动在 RelativeLayout 中较早的孩子之上.

Later children in a RelativeLayout tend to float over earlier children in a RelativeLayout.

(我说倾向于"是因为Android 5.0的elevation的东西也起了作用,而且它们之间的关系是不明确的)

(I say "tend to" because Android 5.0's elevation stuff also plays a role, and the relationship between them is ill-defined)

因此,要使浮动操作按钮 (FAB) 浮动在 RelativeLayout 中的 ScrollView 上,请确保定义了 ScrollView首先在 XML 中,然后是 FAB.这不会影响 X/Y 规则,但它应该让 FAB 出现在 Z 轴上的 ScrollView 上.

So, to have a floating action button (FAB) float over a ScrollView in a RelativeLayout, make sure that the ScrollView is defined first in the XML, with the FAB after it. This will not affect the X/Y rules, but it should have the FAB appear over the ScrollView on the Z axis.

另一种可能性,如果您只支持 Android 5.0+,将使用 android:elevation 本身来提高 FAB.

Another possibility, if you are only supporting Android 5.0+, would be to use android:elevation itself to raise the FAB.

这篇关于如何在滚动视图顶部添加浮动操作按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆