【问题标题】:How to integrate a floating action button into linear layout with toolbar如何使用工具栏将浮动操作按钮集成到线性布局中
【发布时间】:2015-12-04 01:58:32
【问题描述】:

我有以下列表视图,我想在其中添加一个浮动操作按钮。

<?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>

在当前表单中,按钮根本不显示。 我尝试将LinearLayout 更改为CoordinatorLayout,因为许多示例都具有它。但后来我得到一个错误:

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]]
            ...

我也尝试过FrameLayout,但随后列表越过了工具栏(您可以在列表项的透明部分下方看到工具栏,但它们覆盖了工具栏,而不是相反)

【问题讨论】:

    标签: android android-layout floating-action-button


    【解决方案1】:

    您需要使用 android.support.design.widget.CoordinatorLayout 而不是 LinearLayout 作为根布局,然后只有 android.support.design.widget.FloatingActionButton 可以工作

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

    【讨论】:

      【解决方案2】:

      尝试将 ListView 和 FloatingActionButton 放在 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>
      

      【讨论】:

      • 你先生是天赐之物。感谢您提供简单的解决方案!
      【解决方案3】:

      这对我有用:

      <?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>
      

      【讨论】:

      • 感谢您的指点 - 让我朝着正确的方向前进。但是,在我的情况下 - 首先放置 FloatingActionButton 会导致它被 xml 中接下来列出的视图隐藏。将其移动到 CoordinatorLayout 中的最后一个元素解决了这个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多