簡體   English   中英

如何將浮動操作按鈕與工具欄集成到線性布局中

[英]How to integrate a floating action button into linear layout with toolbar

我有以下列表視圖,我想添加一個浮動操作按鈕。

<?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但是然后列表遍布工具欄(你可以在列表項的透明部分下看到工具欄,但是它們覆蓋工具欄,而不是相反)

嘗試將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>

你需要使用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>

這對我有用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM