web-dev-qa-db-fra.com

Comment intégrer un bouton d'action flottant dans une disposition linéaire avec la barre d'outils

J'ai la vue de liste suivante à laquelle je veux ajouter un bouton d'action flottant.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:orientation="vertical"
    Android:background="@drawable/background_serious" >
    <include layout="@layout/toolbar"/>

    <ListView Android:id="@id/Android:list"
        Android:layout_width="fill_parent"
        Android:layout_height="wrap_content"
        Android:cacheColorHint="#00000000">
    </ListView>

    <Android.support.design.widget.FloatingActionButton
        Android:id="@+id/fab"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_gravity="end|bottom"
        Android:layout_margin="@dimen/fab_margin"
        Android:src="@drawable/ic_done" />

</LinearLayout>

Dans le formulaire actuel, le bouton ne s'affiche pas du tout. J'ai essayé de changer LinearLayout en CoordinatorLayout car de nombreux exemples le proposent. Mais je reçois une erreur:

Java.lang.RuntimeException: Unable to start activity ComponentInfo{de.sudoq/de.sudoq.controller.menus.SudokuLoadingActivity}: Android.view.InflateException: Binary XML file line #6: Error inflating class CoordinatorLayout
            at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2325)
            ...
     Caused by: Android.view.InflateException: Binary XML file line #6: Error inflating class CoordinatorLayout
            at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:757)
            ...
     Caused by: Java.lang.ClassNotFoundException: Didn't find class "Android.view.CoordinatorLayout" on path: DexPathList[[Zip file "/data/app/de.sudoq-2/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
            ...

J'ai également essayé FrameLayout mais la liste passe ensuite sur la barre d'outils (vous pouvez voir la barre d'outils sous des parties transparentes des éléments de la liste, mais elles couvrent la barre d'outils, et non l'inverse)

17
peer

Essayez de placer ListView et FloatingActionButton dans FrameLayout

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
   Android:layout_width="fill_parent"
   Android:layout_height="fill_parent"
   Android:orientation="vertical"
   Android:background="@drawable/background_serious" >

      <include layout="@layout/toolbar"/>
  <FrameLayout
         Android:layout_width="match_parent"
         Android:layout_height="match_parent">

          <ListView Android:id="@id/Android:list"
              Android:layout_width="fill_parent"
              Android:layout_height="wrap_content"
              Android:cacheColorHint="#00000000">
          </ListView>

     <Android.support.design.widget.FloatingActionButton
         Android:id="@+id/fab"
         Android:layout_width="wrap_content"
         Android:layout_height="wrap_content"
         Android:layout_gravity="end|bottom"
         Android:layout_margin="@dimen/fab_margin"
         Android:src="@drawable/ic_done" />

  </FrameLayout>
</LinearLayout>
33
Ankii Rawat

vous devez utiliser Android.support.design.widget.CoordinatorLayout comme disposition racine au lieu de LinearLayout puis uniquement Android.support.design.widget.FloatingActionButton marchera

<Android.support.design.widget.CoordinatorLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:orientation="vertical"
    Android:background="@drawable/background_serious" >

// your code

</Android.support.design.widget.CoordinatorLayout>
5
Ravi Rupareliya

Cela a fonctionné pour moi:

<?xml version="1.0" encoding="utf-8"?>
<Android.support.design.widget.CoordinatorLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:fitsSystemWindows="true"
tools:context=".HomeFeedActivity">
<Android.support.design.widget.FloatingActionButton
    Android:id="@+id/fab"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_gravity="bottom|end"
    Android:layout_margin="@dimen/fab_margin"
    Android:src="@drawable/ic_action_add" />

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:orientation="vertical"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:fitsSystemWindows="true"
    tools:context=".HomeFeedActivity">

    <ListView
        Android:id="@+id/list"
        Android:layout_width="fill_parent"
        Android:layout_height="wrap_content"
        Android:divider="@null" />

</LinearLayout>
4
Edward