如何将浮动按钮固定在屏幕右下角而不固定在布局上

问题描述 投票:0回答:2

我有一个可滚动的布局,并添加了一个浮动菜单按钮。所有这些都可以,但是按钮固定在布局上的某个位置,我希望将按钮固定在屏幕的底部。

如果用户上下滚动按钮固定在屏幕位置而不是布局,换句话说,无论滚动视图在哪里,它始终保持在右下角

我已经读过,人们说要使用相对布局,但是我确定我选择了约束布局,因为我希望布局可以滚动。我不断遇到的例子是:

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

任何指导将不胜感激

Floating_Menu.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textview_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="80dp"
        android:layout_marginBottom="156dp"
        android:background="#5C212121"
        android:padding="8dp"
        android:text="StockDetails"
        android:textColor="#000000"
        android:visibility="invisible" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="147dp"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:elevation="16dp"
        android:focusable="true"
        android:visibility="invisible"
        app:backgroundTint="#ffffff"
        app:maxImageSize="50dp"
        app:pressedTranslationZ="12dp"
        app:srcCompat="@drawable/ic_pound" />


    <TextView
        android:id="@+id/textview_mail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="82dp"
        android:layout_marginBottom="99dp"
        android:background="#60212121"
        android:padding="8dp"
        android:text="StockFigures"
        android:textColor="#000000"
        android:visibility="invisible" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="90dp"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:elevation="16dp"
        android:focusable="true"
        android:visibility="invisible"
        app:backgroundTint="#ffffff"
        app:maxImageSize="50dp"
        app:pressedTranslationZ="12dp"
        app:srcCompat="@drawable/ic_arrow" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="32dp"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:elevation="16dp"
        android:focusable="true"
        android:visibility="visible"
        app:backgroundTint="#ffffff"
        app:maxImageSize="50dp"
        app:pressedTranslationZ="12dp"
        app:srcCompat="@drawable/ic_add" />
</RelativeLayout>

活动布局:(浮动菜单位于底部的包括)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/ScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".StockEnquiry">


    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/StockCodeLayout"
            android:layout_width="200dp"
            android:layout_height="60dp"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/BarcodeLayout">

            <EditText
                android:id="@+id/StockCode"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/StockCodetext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Stock Code" />
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/DescriptionLayout"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/StockCodeLayout">

            <EditText
                android:id="@+id/inputdescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:duplicateParentState="false"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/Product_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Product Description" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/SupplierLayout"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/SoldLayout">

            <EditText
                android:id="@+id/supplier"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/supplierText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Supplier" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/Outerlayout"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginTop="1dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/DescriptionLayout">

            <EditText
                android:id="@+id/outer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/Outertext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Outer" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/onOrderLayout"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/StockLevelLayout">

            <EditText
                android:id="@+id/OnOrder"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/onOrdertext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Stock on Order" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/StockLevelLayout"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="2dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/DescriptionLayout">

            <EditText
                android:id="@+id/stockLevel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/Stockleveltext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Stock Level" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/SoldLayout"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/sellPriceLayout">

            <EditText
                android:id="@+id/sold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/soldtext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Sold" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/RRPLayout"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/onOrderLayout">

            <EditText
                android:id="@+id/RRprice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/RRpricetext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="RRP" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/sellPriceLayout"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/Outerlayout">

            <EditText
                android:id="@+id/sellPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/sellPricetext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Sell Price" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/BarcodeLayout"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <EditText
                android:id="@+id/lblScanData"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/Barcodetext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Barcode" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/SalesLayout"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/SupplierLayout">

            <EditText
                android:id="@+id/salesCost"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/sales_Cost"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Total cost this year" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/salesQtyLayout"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/SupplierLayout">

            <EditText
                android:id="@+id/salesQty"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/sales_qty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Total qty this year" />
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/salesValueLayout"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/SalesLayout">

            <EditText
                android:id="@+id/salesValue"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/sales_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Total Sales Value" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/TestLayout"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="200dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/SupplierLayout">

            <EditText
                android:id="@+id/test"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/text_boarder"
                android:focusable="false"
                android:inputType="none"
                android:maxLines="1"
                android:padding="10dp"
                android:paddingLeft="5dp"
                android:singleLine="true" />

            <TextView
                android:id="@+id/testext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="7dp"
                android:background="#ffffff"
                android:text="Some Test Box" />
        </RelativeLayout>

        <include
            layout="@layout/floating_menu"
            android:layout_width="411dp"
            android:layout_height="0dp"
            android:layout_marginBottom="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
    </android.support.constraint.ConstraintLayout>
</ScrollView>
android android-layout android-constraintlayout
2个回答
0
投票
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".StockEnquiry"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> </ScrollView> <View android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content"/> </RelativeLayout>

FrameLayout如下:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".StockEnquiry"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> </ScrollView> <View android:layout_gravity="bottom" android:layout_width="match_parent" android:layout_height="wrap_content"/> </FrameLayout>


0
投票
<RelativeLayout> . ... <ScrollView> ...Your Scroll View Content </ScrollView> <include layout="@layout/floating_menu" android:layout_width="411dp" android:layout_height="0dp" android:layout_marginBottom="10dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </RelativeLayout>

最好使用约束布局代替相对布局

© www.soinside.com 2019 - 2024. All rights reserved.