3

Voici mon xml:FAB entre les dispositions

<?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:id="@+id/coordinatorLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

<include 
    android:id="@+id/toolbar" 
    layout="@layout/toolbar" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/layout_first" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="0.8" 
     android:background="@color/ColorPrimary" 
     android:orientation="horizontal" /> 

    <LinearLayout 
     android:id="@+id/layout_second" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="0.4" 
     android:background="@color/ColorPrimaryLight" 
     android:orientation="horizontal" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="20dp" 
     android:clickable="true" 
     android:src="@drawable/ic_search_black_48dp" 
     app:backgroundTint="#df0909" 
     app:elevation="4dp" 
     app:layout_anchor="@id/layout_first" 
     app:layout_anchorGravity="bottom|right|end" /> 

    </LinearLayout> 

</android.support.design.widget.CoordinatorLayout> 

Et ce à quoi il ressemble: enter image description here

Je l'ai essayé de cette réponse: How can I add the new “Floating Action Button” between two widgets/layouts

Mais quand je l'ai changé compile 'com.android.support:design:23.4.0'-compile 'com.android.support:design:25.0.1' I Je reçois une erreur:

apply plugin: 'com.android.application' 
android { 
compileSdkVersion 23 
buildToolsVersion "23.0.3" 

defaultConfig { 
    applicationId "com.test.test" 
    minSdkVersion 21 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 
dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.4.0' 
    compile 'com.android.support:design:23.4.0' 
} 

Ainsi est-il un moyen d'atteindre un regard comme celui-ci, où le fabuleux est entre deux mises en page (pas de défilement nécessaire): enter image description here

+1

Vous avez 'FloatingActionButton' dans un' LinearLayout'. Vous le voulez en tant qu'enfant direct de 'CoordinatorLayout'. C'est-à-dire, déplacez-le après la fermeture de la balise 'LinearLayout'. –

+1

@MikeM. Je vous remercie. C'est ce qu'il a fait. Pourquoi ne postez-vous pas une réponse? –

Répondre

2

Votre FloatingActionButton est à l'intérieur d'un LinearLayout. Pour que les attributs layout_anchor* fonctionnent, il faut que ce soit un enfant direct du CoordinatorLayout.

Déplacez simplement l'élément FloatingActionButton après la balise de fermeture LinearLayout.

0

Vous pouvez utiliser suivant:

<FloatingButton 
app:layout_anchor="@id/layout_first" 
    app:layout_anchorGravity="bottom|left|end"/> 

utiliser sur la mise en page vous voulez, et essayez de google pour obtenir l'effet désiré. il y a beaucoup de tutoriels pour l'ancrage, alors regardez-le. Mais, le point principal est que vous devez l'ancrer à la mise en page que vous voulez être sur.

0
android:layout_margin="-20dp" 

utilisation marge négative (la moitié de la hauteur FAB)