簡體   English   中英

底部對齊浮動操作按鈕

[英]Bottom Align Floating Action Button

我想與我的 FAB 右下角對齊。

  1. 我試過android:gravity="bottom|right"
  2. 當我嘗試android:layout_alignParentBottom="true " FAB消失
  3. 當我嘗試android:layout_alignBottom="@id/lista_tiendas" FAB消失

看起來並不復雜,但我就是做不到

有任何想法嗎?

<?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">

<ListView
    android:id="@+id/lista_tiendas"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:divider="@android:color/transparent"
    android:dividerHeight="4.0sp"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add_white_48dp"
    app:backgroundTint="@color/spg_rosa"
    app:borderWidth="0dp"
    app:elevation="8dp"
    app:fabSize="normal"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="@id/lista_tiendas"
     />
</RelativeLayout>

對於RelativeLayout

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    ... />

對於CoordinatorLayout ,你應該使用android:layout_gravity="end|bottom"


對於ConstraintLayout

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    ... />

有關更多信息,請參閱此答案

布局應該是RelativeLayout 嘗試下一個代碼:

<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">

<LinearLayout
    android:id="@+id/ly_list_form"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <ListView
        android:id="@+id/list_forms"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dp" />

</LinearLayout>

<LinearLayout
    android:id="@+id/ly_bar_bottom"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="right"
    android:orientation="horizontal">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/button_addc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="28dp"
        android:src="@drawable/ic_add"
        app:borderWidth="0dp"
        app:elevation="6dp"
        app:pressedTranslationZ="12dp" />
</LinearLayout>

似乎 FloatingActionButton 在 RelativeLayout 中的位置不正確。

我把RelativeLayout改成了FrameLayout,問題解決了! 希望能幫助到你!

<ListView
    android:id="@+id/mylist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:divider="@android:color/transparent"
    android:dividerHeight="1.0sp" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom"
    android:src="@drawable/ic_add_white_48dp"
    app:backgroundTint="@color/spg_rosa"
    app:borderWidth="0dp"
    app:elevation="8dp"
    app:fabSize="normal" />

</FrameLayout>

嘗試使用android.support.design.widget.CoordinatorLayoutandroid:layout_gravity="bottom|right"對我android:layout_gravity="bottom|right"

通過這樣做,我能夠使代碼正常工作:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="#ff0000"
    app:borderWidth="0dp"
    app:elevation="8dp"
    app:fabSize="normal"
    android:layout_alignLeft="@+id/text"
    android:layout_above="@+id/text"/>

但是,它不適用於TextView below而不是上方,我相信這可能是一個錯誤。

嘗試

android:layout_gravity="底部|右"

在相對布局的底部和中心對齊按鈕

將這兩個添加到您的浮動操作按鈕容器中

    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"

輸出將是:像這樣

暫無
暫無

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

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