如何使用工具栏将浮动操作按钮集成到线性布局中 [英] How to integrate a floating action button into linear layout with toolbar

查看:82
本文介绍了如何使用工具栏将浮动操作按钮集成到线性布局中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下列表视图,要向其中添加一个浮动操作按钮.

I have the following list view to which I want to add a floating action button.

<?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,因为许多示例都带有它.但是然后我得到一个错误:

In the current form the button is not displayed at all. I tried changing LinearLayout to CoordinatorLayout as many examples feature it. But then I get an error:

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,但是列表遍历了工具栏(您可以在列表项的透明部分下面看到工具栏,但是它们覆盖了工具栏,而不是相反)

I also tried FrameLayout but then the list goes over the toolbar(you can see the toolbar under transparent parts of list items, but they cover the toolbar, not the other way around)

推荐答案

尝试将ListView和FloatingActionButton放在FrameLayout内

Try to put ListView and FloatingActionButton inside 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>

这篇关于如何使用工具栏将浮动操作按钮集成到线性布局中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆