2015-06-10 2 views
29

Я хочу выровнять по нижнему праву мой 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> 

ответ

77

Для RelativeLayout:

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    ... /> 

Для CoordinatorLayout, вы должны использовать android:layout_gravity="end|bottom"


Для ConstraintLayout:

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintEnd_toEndOf="parent" 
    ... /> 

Для получения дополнительной информации см. this answer.

+1

Да, я скорректировал формат кода, поэтому появляется RelativeLayout! Tx –

+0

Это тоже помогло мне, спасибо! Почему LinearLayout не работает для FAB? – Michael

+1

Рекомендуется: android: layout_alignParentEnd = "true" – Zon

1

Я был в состоянии получить код для работы, делая это:

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

Однако он не работает с below в TextView вместо выше, я считаю, что это может быть ошибка.

+0

Хорошо, но у меня нет текстового представления, у меня есть список, который занимает весь экран, поэтому, если я его выложу выше, у меня не будет желаемой позиции! –

5

Похоже, что 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> 
9

Макет должен быть 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> 

+0

Обтекание плавающей кнопкой в ​​линейной компоновке - вот что сделал трюк для меня. В противном случае он будет игнорировать поля. –

0

Попробуйте

андроида: layout_gravity = "дно | правильный"

2

Попробуйте использовать android.support.design.widget.CoordinatorLayout и android:layout_gravity="bottom|right" работает для меня.