How to create a floating action button (FAB) in android, using AppCompat v21?

前端 未结 7 951
眼角桃花
眼角桃花 2020-11-30 18:03

I would like to create a floating action button (to add items to a listview), like google calendar, maintaining compatibility with pre-lollipop Android versions (before 5.0)

相关标签:
7条回答
  • 2020-11-30 18:43

    Try this library, it supports shadow, there is minSdkVersion=7 and also supports android:elevation attribute for API-21 implicitly.

    Original post is here.

    0 讨论(0)
  • 2020-11-30 18:43

    Add padding and elevation:

     android:elevation="10dp"
     android:padding="10dp"
    
    0 讨论(0)
  • 2020-11-30 18:47

    Here is one aditional free Floating Action Button library for Android It has many customizations and requires SDK version 9 and higher

    enter image description here

    Full Demo Video

    0 讨论(0)
  • 2020-11-30 18:48

    I've generally used xml drawables to create shadow/elevation on a pre-lollipop widget. Here, for example, is an xml drawable that can be used on pre-lollipop devices to simulate the floating action button's elevation.

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="8px">
        <layer-list>
            <item>
                <shape android:shape="oval">
                    <solid android:color="#08000000"/>
                    <padding
                        android:bottom="3px"
                        android:left="3px"
                        android:right="3px"
                        android:top="3px"
                        />
                </shape>
            </item>
            <item>
                <shape android:shape="oval">
                    <solid android:color="#09000000"/>
                    <padding
                        android:bottom="2px"
                        android:left="2px"
                        android:right="2px"
                        android:top="2px"
                        />
                </shape>
            </item>
            <item>
                <shape android:shape="oval">
                    <solid android:color="#10000000"/>
                    <padding
                        android:bottom="2px"
                        android:left="2px"
                        android:right="2px"
                        android:top="2px"
                        />
                </shape>
            </item>
            <item>
                <shape android:shape="oval">
                    <solid android:color="#11000000"/>
                    <padding
                        android:bottom="1px"
                        android:left="1px"
                        android:right="1px"
                        android:top="1px"
                        />
                </shape>
            </item>
            <item>
                <shape android:shape="oval">
                    <solid android:color="#12000000"/>
                    <padding
                        android:bottom="1px"
                        android:left="1px"
                        android:right="1px"
                        android:top="1px"
                        />
                </shape>
            </item>
            <item>
                <shape android:shape="oval">
                    <solid android:color="#13000000"/>
                    <padding
                        android:bottom="1px"
                        android:left="1px"
                        android:right="1px"
                        android:top="1px"
                        />
                </shape>
            </item>
            <item>
                <shape android:shape="oval">
                    <solid android:color="#14000000"/>
                    <padding
                        android:bottom="1px"
                        android:left="1px"
                        android:right="1px"
                        android:top="1px"
                        />
                </shape>
            </item>
            <item>
                <shape android:shape="oval">
                    <solid android:color="#15000000"/>
                    <padding
                        android:bottom="1px"
                        android:left="1px"
                        android:right="1px"
                        android:top="1px"
                        />
                </shape>
            </item>
            <item>
                <shape android:shape="oval">
                    <solid android:color="#16000000"/>
                    <padding
                        android:bottom="1px"
                        android:left="1px"
                        android:right="1px"
                        android:top="1px"
                        />
                </shape>
            </item>
            <item>
                <shape android:shape="oval">
                    <solid android:color="#17000000"/>
                    <padding
                        android:bottom="1px"
                        android:left="1px"
                        android:right="1px"
                        android:top="1px"
                        />
                </shape>
            </item>
        </layer-list>
    </item>
    <item>
        <shape android:shape="oval">
            <solid android:color="?attr/colorPrimary"/>
        </shape>
    </item>
    </layer-list>
    

    In place of ?attr/colorPrimary you can choose any color. Here's a screenshot of the result:

    enter image description here

    0 讨论(0)
  • 2020-11-30 18:56

    There are a bunch of libraries out there add a FAB(Floating Action Button) in your app, Here are few of them i Know.

    makovkastar's FAB

    futuersimple's Composite FAB

    Material Design library which includes FAB too

    All these libraries are supported on pre-lollipop devices, minimum to api 8

    0 讨论(0)
  • 2020-11-30 18:56

    @Justin Pollard xml code works really good. As a side note you can add a ripple effect with the following xml lines.

        <item>
        <ripple
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:color="?android:colorControlHighlight" >
            <item android:id="@android:id/mask">
                <shape android:shape="oval" >
                    <solid android:color="#FFBB00" />
                </shape>
            </item>
            <item>
                <shape android:shape="oval" >
                    <solid android:color="@color/ColorPrimary" />
                </shape>
            </item>
        </ripple>
    </item>
    
    0 讨论(0)
提交回复
热议问题