2017-05-14 3 views
1

J'essaie d'ajouter un bouton d'action flottante dans l'une de mes mises en page. J'ai créé une disposition distincte pour le bouton d'action flottante à inclure dans la disposition principale de la mine. mais chaque fois que je l'inclue, ce n'est pas en haut de la liste.Inclure le bouton d'action flottante au-dessus de ListView

Bouton Floating Action 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"> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/floatingActionButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:layout_marginBottom="23dp" 
    android:layout_marginEnd="22dp" 
    android:clickable="true" 
    app:fabSize="mini" 
    app:srcCompat="@android:drawable/ic_menu_add" /> 
    </RelativeLayout> 

mise en page d'activité:

<LinearLayout 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:orientation="vertical" 
android:layout_height="match_parent" 
tools:context=".MainActivity" 
android:weightSum="1" 
android:background="@color/BG_beige"> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="44dp" 
    android:orientation="horizontal" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:paddingTop="10dp"> 

    <TextView 
     android:id="@+id/textView17" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.58" 
     android:text="Name" 
     android:textSize="20sp"/> 

    <TextView 
     android:id="@+id/textView15" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Price" 
     android:textSize="20sp"/> 

    <TextView 
     android:id="@+id/textView19" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.51" 
     android:text="Paid" 
     android:textSize="20sp" 
     android:gravity="center"/> 



</LinearLayout> 

<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<include 
    layout="@layout/utility_fab" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

</LinearLayout> 

Ainsi, la mise en page qui en résulte est la suivante: Main Layout

Répondre

1

Enveloppez votre mise en page à l'intérieur FrameLayout et appliquez android:layout_gravity="bottom"

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <include 
     layout="@layout/utility_fab" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="bottom" /> 
</FrameLayout> 
+0

mais ne pas la mise en page de trame détruire mon positionnement actuel? parce que im utilisant linéaire – JJCADIZ

+0

'FrameLayout' est seulement utilisé pour aligner listview et bouton d'action flottant ... il n'affectera pas linearlayout – rafsanahmad007

0

Si vous souhaitez placer un composant l'un sur l'autre, vous pouvez essayer d'utiliser FrameLayout, par ex. vous trouverez ici l'exemple (voir la réponse acceptée):

How to show one layout on top of the other programmatically in my case?

L'autre option consiste à utiliser RelativeLayout (voir la réponse acceptée):

how to put 2 layouts on top of each others

MISE À JOUR: Vous pouvez pour utiliser le code suivant. Probablement, vous devrez faire une idée vous-même configuration, mais cela est le plus général:

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

    <LinearLayout 
    android:orientation="vertical" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" 
    android:weightSum="1" 
    android:background="@color/BG_beige"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="44dp" 
     android:orientation="horizontal" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:paddingTop="10dp"> 

     <TextView 
      android:id="@+id/textView17" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1.58" 
      android:text="Name" 
      android:textSize="20sp"/> 

     <TextView 
      android:id="@+id/textView15" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Price" 
      android:textSize="20sp"/> 

     <TextView 
      android:id="@+id/textView19" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.51" 
      android:text="Paid" 
      android:textSize="20sp" 
      android:gravity="center"/> 
    </LinearLayout> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

<include 
    layout="@layout/utility_fab" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

</FrameLayout> 
+0

mais la mise en page d'image ne détruira pas mon positionnement actuel? parce que im en utilisant linéaire – JJCADIZ

0

Vous avez le RelativeLayout mal réglé

<RelativeLayout> 
    <Your main layout/> 
    <Fab button/> 
</RelativeLayout> 

android vous avez actuellement dit de mettre le bouton fabuleux et le parent RelativeLayout mise en page après la listview, je soupçonne qu'il est en dehors de l'écran