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

前端 未结 3 998
我在风中等你
我在风中等你 2021-02-07 01:01

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




        
相关标签:
3条回答
  • 2021-02-07 01:45

    you need to use android.support.design.widget.CoordinatorLayout as your root layout instead of LinearLayout then only android.support.design.widget.FloatingActionButton will work

    <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>
    
    0 讨论(0)
  • 2021-02-07 01:48

    This worked for me:

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

    0 讨论(0)
  • 2021-02-07 01:53

    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>
    
    0 讨论(0)
提交回复
热议问题