2016-11-01 1 views
0

Image LinkLe bouton flottant n'est pas ajusté au centre de la barre d'applications de certains périphériques

J'ai fourni la capture d'écran de l'écran de mon application. Vous pouvez voir que le bouton flottant (dans ce cas, j'ai utilisé Image Button et fourni mon image) ne montre pas complet, c'est-à-dire que sa moitié est derrière la barre d'application, la barre d'application est de couleur verte. Bien que ce même bouton FAB montre plein dans certains appareils et versions comme 4.2, 4.3, mais ne montre pas plein sur certains appareils et ma version 5.0 et plus.

Voici mon code pour cette mise en page.

<?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" 
android:id="@+id/coordinator_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#FFFFFF" 
android:fitsSystemWindows="true"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="190dp" 
    android:fitsSystemWindows="true" 
    android:background="@drawable/cvr1" 
    app:expanded="true"> 
    <!--android:theme="?attr/customAppBarOverlay"--> 


<android.support.design.widget.CollapsingToolbarLayout 
    android:id="@+id/toolbar_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    app:contentScrim="?attr/colorPrimary" 
    app:layout_scrollFlags="snap"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     app:layout_collapseMode="pin" 
     android:background="@null"/> 


</android.support.design.widget.CollapsingToolbarLayout> 
</android.support.design.widget.AppBarLayout> 

<include layout="@layout/content_main" /> 

<ImageButton 
    android:id="@+id/add_fab" 
    android:background="@drawable/starts" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:layout_margin="@dimen/fab_margin" 
    app:layout_anchor="@id/app_bar" 
    app:elevation="0dp" 
    app:layout_anchorGravity="bottom|center" 
    /> 

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

Je ne sais pas exactement quel est le problème. J'ai vérifié sur internet mais j'ai trouvé les mêmes solutions comme ces lignes mais je n'ai toujours pas montré plein

Aidez-nous!

+0

au lieu d'essayer de marge fixe 'layout_centerVertical = « true »' et 'layout_centerHorizontal = « true »' – rookieDeveloper

+0

Non, il ne fonctionne pas :( –

+0

essayer avec disposition relative – rookieDeveloper

Répondre

0

Le les meilleures pratiques est d'utiliser la F A B (barre d'action flottant) prévu dans la bibliothèque de soutien.

Ajouter ce fichier à build.gradle:

compile 'com.android.support:design:24.2.1' 

Et à l'intérieur du CoordinatorLayout, essayez ceci:

<android.support.design.widget.CoordinatorLayout 
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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

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

    <LinearLayout 
     android:id="@+id/viewB" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="0.6" 
     android:background="@android:color/holo_orange_light" 
     android:orientation="horizontal"/> 

</LinearLayout> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="16dp" 
    android:clickable="true" 
    android:src="@drawable/ic_done" 
    app:layout_anchor="@id/viewA" 
    app:layout_anchorGravity="bottom|right|end"/> 

Cela devrait produire le FAB comme il est prévu dans l'image ci-dessous:

enter image description here

Pour changer la position du FAB, vous pouvez changer:

app:layout_anchorGravity="bottom|right|end"/> 

à la manière préférée pour la position idéale dans votre mise en page.

+0

J'ai essayé cela mais encore ça ne fonctionne pas, la moitié du bouton FAB toujours caché derrière la mise en page de la barre d'application –

0

La raison en est que vous utilisez app:elevation="0dp" sur votre bouton. Comme le AppBarLayout a une élévation par défaut de 4dp, sur les périphériques lollipop et plus, le bouton sera dessiné en dessous.

+0

Il ne fonctionne pas non plus :( –