滚动视图上方的浮动动作按钮卡在顶部 [英] Floating action button over a scrollview stuck at top

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

问题描述

我试图让一个浮动操作按钮 (FAB) 在屏幕右下角的滚动视图上浮动",它只是卡住"在滚动视图的顶部并且不会移动.这是我的XML:

I'm trying to get a Floating Action Button (FAB) to "float" over a scrollview at the bottom right of the screen and it just gets "stuck" at the top of the scrollview and doesn't move. Here's my XML:

<?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">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <ImageView
                android:src="@drawable/Banner2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/imageView1" />
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_gravity="center"
                android:layout_marginTop="0sp"
                android:background="#000000" />
            <TextView
                android:text="New Account"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView1"
                android:textAlignment="center" />
        </LinearLayout>
        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
                <android.support.design.widget.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_margin="16dp"
                    android:clickable="true"
                    android:src="@drawable/ic_info_2x" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Enter Username"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="150.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/aTextboxUsername" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Enter Password"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="150.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:inputType="textPassword"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/aTextboxPassword"
                        android:autoLink="none" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Confirm Password"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="150.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:inputType="textPassword"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/aTextboxPassword2"
                        android:autoLink="none" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Enter Email Address"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="192.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/txtEmailAddress"
                        android:autoLink="none" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Confirm Email Address"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="193.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/txtEmailAddress2"
                        android:autoLink="none" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="First Name" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/motherfirstname"
                         />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="Middle Initial" />
                    <EditText
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:maxLength = "1"
                        android:id="@+id/mothermi"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="Last Name" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/motherlastname"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="35dp"
                        android:text="Suffix" />
                    <Spinner
                        android:layout_width="110dp"
                        android:layout_height="35dp"
                        android:id="@+id/spinner_suffix" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="Address" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/motheraddress"
                         />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="City" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/mothercity"
                         />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="State" />
                    <Spinner
                        android:layout_width="110dp"
                        android:layout_height="35dp"
                        android:id="@+id/motherstate" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="Zip Code" />
                    <EditText
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:maxLength = "5"
                        android:id="@+id/motherzip"/>
                </LinearLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="0sp"
                    android:background="#000000" />
                <TextView
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:text="Select Company, Department, Section you are registering for" />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="0sp"
                    android:background="#000000" />
                <TextView
                    android:text="Select Company"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" 
                    android:textStyle="bold"
                    android:id="@+id/textView2" />
                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:id="@+id/company_spinner" />

                    <TextView
                    android:text="Select Department"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" 
                    android:textStyle="bold"
                    android:id="@+id/textView2" />
                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:id="@+id/department_spinner" />

                    <TextView
                    android:text="Select Section"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" 
                    android:textStyle="bold"
                    android:id="@+id/textView2" />
                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:id="@+id/section_spinner" />
                <Button
                    android:text="Submit"
                    android:layout_width="200px"
                    android:layout_gravity="center"
                    android:layout_height="34.5dp"
                    android:id="@+id/button_submit" />
            </LinearLayout>
        </ScrollView>
</LinearLayout>

单击FAB后,将显示一个弹出窗口,向用户提供有关如何进行的信息.我已经尝试了一些在stackoverflow和其他地方找到的示例,但没有任何效果.我怎样才能解决这个问题?

When the FAB is clicked, a popup is displayed providing the user with information about how to proceed. I've tried several examples I found here on stackoverflow and other places and nothing has worked. How can I fix this?

推荐答案

您可以使用 CoordinatorLayout 开始布局,然后可以将FAB锚定( layout_anchor )您的 ScrollView 并应用布局重力( layout_anchorGravity )将其放置在右下角( bottom | end ):

You can start the layout with a CoordinatorLayout and then you can anchor (layout_anchor) your FAB to your ScrollView and apply a layout gravity (layout_anchorGravity) to place it bottom-right (bottom|end):

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchorGravity="bottom|end"
    app:layout_anchor="@id/scrollView1"
    app:srcCompat="@drawable/ic_info_2x" />

类似的东西:

<?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">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/imageView1" />
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_gravity="center"
                android:layout_marginTop="0sp"
                android:background="#000000" />
            <TextView
                android:text="New Account"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView1"
                android:textAlignment="center" />
        </LinearLayout>
        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Enter Username"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="150.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/aTextboxUsername" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Enter Password"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="150.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:inputType="textPassword"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/aTextboxPassword"
                        android:autoLink="none" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Confirm Password"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="150.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:inputType="textPassword"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/aTextboxPassword2"
                        android:autoLink="none" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Enter Email Address"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="192.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/txtEmailAddress"
                        android:autoLink="none" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Confirm Email Address"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="193.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/txtEmailAddress2"
                        android:autoLink="none" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="First Name" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/motherfirstname"
                         />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="Middle Initial" />
                    <EditText
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:maxLength = "1"
                        android:id="@+id/mothermi"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="Last Name" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/motherlastname"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="35dp"
                        android:text="Suffix" />
                    <Spinner
                        android:layout_width="110dp"
                        android:layout_height="35dp"
                        android:id="@+id/spinner_suffix" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="Address" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/motheraddress"
                         />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="City" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/mothercity"
                         />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="State" />
                    <Spinner
                        android:layout_width="110dp"
                        android:layout_height="35dp"
                        android:id="@+id/motherstate" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:text="Zip Code" />
                    <EditText
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:maxLength = "5"
                        android:id="@+id/motherzip"/>
                </LinearLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="0sp"
                    android:background="#000000" />
                <TextView
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:text="Select League, Program, Team you are" />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="0sp"
                    android:background="#000000" />
                <TextView
                    android:text="Select League"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" 
                    android:textStyle="bold"
                    android:id="@+id/textView2" />
                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:id="@+id/league_spinner" />
                <TextView
                    android:text="Select Program"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" 
                    android:textStyle="bold"
                    android:id="@+id/textView2" />
                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:id="@+id/program_spinner" />

                    <TextView
                    android:text="Select Team"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" 
                    android:textStyle="bold"
                    android:id="@+id/textView2" />
                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:id="@+id/team_spinner" />
                <Button
                    android:text="Submit"
                    android:layout_width="200px"
                    android:layout_gravity="center"
                    android:layout_height="34.5dp"
                    android:id="@+id/button_submit" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchorGravity="bottom|end"
        app:layout_anchor="@id/scrollView1"
        app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>

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

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