J'ai un bouton d'action flottant que j'aimerais ajouter au-dessus d'une vue à défilement pour qu'il reste en place, même si vous faites défiler. Je le veux en haut de l'écran de défilement et dans le coin inférieur droit de l'écran. Quelle combinaison de vues dois-je utiliser pour accomplir cela?
Voici le fichier XML:
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
xmlns:fab="http://schemas.Android.com/apk/res-auto"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context="com.nhscoding.safe2tell.STORIES"
Android:background="@color/stor_back">
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:gravity="bottom"
>
<com.getbase.floatingactionbutton.FloatingActionButton
Android:id="@+id/pink_icon"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
fab:fab_icon="@drawable/ic_add"
fab:fab_colorNormal="@color/fab_back"
fab:fab_colorPressed="@color/fab_pressed_back"
Android:layout_gravity="end"
Android:layout_marginBottom="16dp"
Android:layout_marginRight="16dp"/>
</LinearLayout>
<ScrollView
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical">
<Android.support.v7.widget.CardView
xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view1"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius"
>
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card1_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top"
Android:id="@+id/title1"
/>
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView
xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view2"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius"
Android:layout_below="@id/card_view1">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card2_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top"
/>
<TextView
Android:id="@+id/info_text2"
Android:layout_marginTop="16dp"
Android:layout_marginBottom="0dp"
Android:layout_marginLeft="16dp"
Android:layout_marginRight="16dp"
Android:layout_width="match_parent"
Android:layout_height="match_parent"/>
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView
xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view3"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius"
Android:layout_below="@id/card_view2">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card3_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top"
/>
<TextView
Android:id="@+id/info_text3"
Android:layout_marginTop="16dp"
Android:layout_marginBottom="0dp"
Android:layout_marginLeft="16dp"
Android:layout_marginRight="16dp"
Android:layout_width="wrap_content"
Android:layout_height="match_parent"/>
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView
xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view1"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card4_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top"
Android:id="@+id/title4"
/>
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView
xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view1"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="16dp"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card5_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top"
Android:id="@+id/title5"
/>
</Android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Les enfants plus tard dans une RelativeLayout
ont tendance à flotter au-dessus des enfants plus anciens dans une RelativeLayout
.
(Je dis "tendance à" parce que les options elevation
d'Android 5.0 jouent également un rôle et que la relation entre elles est mal définie)
Ainsi, pour qu'un FAB flotte sur une ScrollView
dans une RelativeLayout
, assurez-vous que la ScrollView
est définie en premier dans le XML, suivie de la FAB. Cela n’affectera pas les règles X/Y, mais le FAB devrait apparaître sur la variable ScrollView
sur l’axe des Z.
Une autre possibilité, si vous ne supportez que Android 5.0+, serait d'utiliser Android:elevation
lui-même pour élever le FAB.
essaye ça:
Android: layout_alignParentRight = "true"
Android: layout_alignParentBottom = "true"
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
xmlns:fab="http://schemas.Android.com/apk/res-auto"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context="com.nhscoding.safe2tell.STORIES"
Android:background="@color/stor_back">
<ScrollView
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical">
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view1"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card1_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top"
Android:id="@+id/title1" />
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view2"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius"
Android:layout_below="@id/card_view1">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card2_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top" />
<TextView
Android:id="@+id/info_text2"
Android:layout_marginTop="16dp"
Android:layout_marginBottom="0dp"
Android:layout_marginLeft="16dp"
Android:layout_marginRight="16dp"
Android:layout_width="match_parent"
Android:layout_height="match_parent" />
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view3"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius"
Android:layout_below="@id/card_view2">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card3_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top" />
<TextView
Android:id="@+id/info_text3"
Android:layout_marginTop="16dp"
Android:layout_marginBottom="0dp"
Android:layout_marginLeft="16dp"
Android:layout_marginRight="16dp"
Android:layout_width="wrap_content"
Android:layout_height="match_parent" />
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view1"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card4_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top"
Android:id="@+id/title4" />
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view1"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="16dp"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card5_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top"
Android:id="@+id/title5" />
</Android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
<LinearLayout
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:gravity="bottom"
Android:layout_alignParentRight="true"
Android:layout_alignParentBottom="true"
Android:layout_margin="10dp">
<com.getbase.floatingactionbutton.FloatingActionButton
Android:id="@+id/pink_icon"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
fab:fab_icon="@drawable/ic_add"
fab:fab_colorNormal="@color/fab_back"
fab:fab_colorPressed="@color/fab_pressed_back"
Android:layout_gravity="end"
Android:layout_marginBottom="16dp"
Android:layout_marginRight="16dp" />
</LinearLayout>
</RelativeLayout>
Disposition relative comme base. Dans cette vue, une vue de défilement, puis le bouton d'action flottante (avec alignParent Right et Bottom défini sur true).
<?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">
<ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_height="match_parent"
Android:layout_width="match_parent">
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical">
<TextView
Android:text="Your content"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
<Android.support.design.widget.FloatingActionButton
Android:id="@+id/fab1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignParentRight="true"
Android:layout_alignParentBottom="true"
Android:src="@drawable/ic_add"
Android:layout_margin="16dp"/>
</RelativeLayout>
BR Matthias
Vous pouvez utiliser Android.support.design.widget.CoordinatorLayout
en tant que disposition parent. Créez deux XML avec lesquels vous pouvez définir une barre de défilement ou une liste. Autre est un conteneur parent qui contient toutes les informations avec votre FloatingActionButton
.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<Android.support.design.widget.CoordinatorLayout
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:layout_height="match_parent"
Android:fitsSystemWindows="true"
tools:context=".MainActivity">
<Android.support.design.widget.AppBarLayout
Android:layout_height="wrap_content"
Android:layout_width="match_parent"
Android:theme="@style/AppTheme.AppBarOverlay">
<Android.support.v7.widget.Toolbar
Android:id="@+id/toolbar"
Android:layout_width="match_parent"
Android:layout_height="?attr/actionBarSize"
Android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</Android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<Android.support.design.widget.FloatingActionButton
Android:id="@+id/fab"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="bottom|end"
app:elevation="6dp"
app:backgroundTint="@color/colorAccent"
app:pressedTranslationZ="12dp"
Android:layout_margin="@dimen/fab_margin"
Android:src="@drawable/ic_add" />
</Android.support.design.widget.CoordinatorLayout>
content_main.xml
Ici, vous pouvez mettre votre barre de défilement. C'est assez facile.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
xmlns:app="http://schemas.Android.com/apk/res-auto" Android:layout_width="match_parent"
Android:layout_height="match_parent" Android:paddingLeft="@dimen/activity_horizontal_margin"
Android:paddingRight="@dimen/activity_horizontal_margin"
Android:paddingTop="@dimen/activity_vertical_margin"
Android:paddingBottom="@dimen/activity_vertical_margin"
Android:gravity="center"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">
<ScrollView
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical">
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view1"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card1_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top"
Android:id="@+id/title1" />
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view2"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius"
Android:layout_below="@id/card_view1">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card2_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top" />
<TextView
Android:id="@+id/info_text2"
Android:layout_marginTop="16dp"
Android:layout_marginBottom="0dp"
Android:layout_marginLeft="16dp"
Android:layout_marginRight="16dp"
Android:layout_width="match_parent"
Android:layout_height="match_parent" />
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view3"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius"
Android:layout_below="@id/card_view2">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card3_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top" />
<TextView
Android:id="@+id/info_text3"
Android:layout_marginTop="16dp"
Android:layout_marginBottom="0dp"
Android:layout_marginLeft="16dp"
Android:layout_marginRight="16dp"
Android:layout_width="wrap_content"
Android:layout_height="match_parent" />
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view1"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="@dimen/card_margin_bottom"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card4_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top"
Android:id="@+id/title4" />
</Android.support.v7.widget.CardView>
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/card_view1"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:layout_marginTop="@dimen/card_margin_top"
Android:layout_marginBottom="16dp"
Android:layout_marginLeft="@dimen/card_margin_left"
Android:layout_marginRight="@dimen/card_margin_right"
card_view:cardCornerRadius="@dimen/card_radius">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="@dimen/card_title_top"
Android:layout_marginBottom="@dimen/card_title_bottom"
Android:layout_marginLeft="@dimen/card_title_left"
Android:layout_marginRight="@dimen/card_title_right"
Android:text="@string/card5_title"
Android:textSize="@dimen/card_title_size"
Android:gravity="top"
Android:id="@+id/title5" />
</Android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
</RelativeLayout>