簡體   English   中英

我們如何將浮動操作按鈕放在任何布局的頂部

[英]How we can put Floating action button on top of any layout

我正在對浮動操作按鈕進行一些操作。 我有一個列表視圖並在屏幕底部顯示谷歌廣告,浮動操作按鈕顯示在同一位置(底部|末端)。 所以操作按鈕隱藏了廣告。

我想將浮動操作按鈕移動到廣告線性布局上方一點。

請看圖片: 在此處輸入圖像描述

我的代碼是這樣的:

<android.support.design.widget.FloatingActionButton
     android:id="@+id/fab"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_margin="@dimen/fab_margin"
     android:layout_alignParentBottom="true"
     android:layout_alignParentEnd="true"
     android:layout_alignParentRight="true"
     android:layout_gravity="bottom|end"
     android:src="@drawable/fav"
     android:adjustViewBounds="false" />


adLayoutId is ad layout id

請幫忙。

提前致謝。

您應該使用RelativeLayout ,正如我們所知, RelativeLayout RelativeLayout前面的孩子之上。

因此,要讓 FAB 浮動在任何布局之上,請確保在所有視圖之后(在 XML 布局的末尾)定義 FAB。

如果您使用android:layout_margin="@dimen/fab_margin"操作 FAB 的最佳方式是設置視圖之間的依賴關系,例如:

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

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/adLayoutId"/>

    <LinearLayout
        android:id="@+id/adLayoutId"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/black"/>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/adLayoutId"
        android:layout_alignParentRight="true"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/fav"/>
</RelativeLayout>

浮動操作按鈕始終位於廣告線性布局之上,並且獨立於廣告布局尺寸。 希望能幫助到你。

<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_marginBottom="100dp"
        android:layout_marginRight="30dp"
       />

fab 按鈕內的這兩行幫助我解決了這個問題:

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"

這是我單獨用於按鈕的完整代碼:

 <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_newStudent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:contentDescription="@string/todo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_baseline_add_24" />

這兩行很重要。

android:layout_alignParentEnd="真"

機器人:layout_alignParentBottom =“真”

添加 55dp 的底部邊距,這是 Admob 橫幅的大小

此行位於浮動按鈕或封閉布局中。

android:layout_marginBottom="55dp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM