如何在Android 5+上创建一个位于右下角的圆形按钮?

8
有没有标准的工具或现有的库可以创建类似于Gmail应用程序上的圆形按钮? enter image description here

2
它被称为浮动操作按钮:https://dev59.com/L2Af5IYBdhLWcg3wnDwI - Morrison Chang
https://dev59.com/p4vda4cB1Zd3GeqPX1AN#30527761 - Gabriele Mariotti
3个回答

3
在Android支持设计库中,提供了一个名为“浮动操作按钮”的小部件,通常放置在界面的右下角(但不一定只能在那里使用)。
浮动操作按钮是一个圆形的按钮,表示界面上的主要操作。设计库的FloatingActionButton提供了单个一致的实现,默认情况下使用主题中的colorAccent进行了颜色设定。
要在build.gradle文件中获取Android设计支持库,请添加以下内容:
compile 'com.android.support:design:22.2.0'

在你的布局文件中:
<RelativeLayout
 ...
 xmlns:app="http://schemas.android.com/apk/res-auto">

       <android.support.design.widget.FloatingActionButton
            android:id="@+id/myFAB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"  
            app:layout_anchor="@id/appbar"
            app:layout_anchorGravity="bottom|right|end"
            android:src="@drawable/ic_discuss"
            android:layout_margin="10dp"
            android:clickable="true"/>
</RelativeLayout>

Android设计支持库的详细信息请 参见此处

此外,还有许多其他提供FAB的库,例如:


2

1.首要使用Android Studio

2.将以下内容添加为依赖项到你的build.gradle文件中:

dependencies {
compile 'com.melnykov:floatingactionbutton:1.3.0'
}

3. 创建布局

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

<com.melnykov.fab.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@drawable/ic_action_content_new"
        fab:fab_colorNormal="@color/primary"
        fab:fab_colorPressed="@color/primary_pressed"
        fab:fab_colorRipple="@color/ripple" />

4. 添加命名空间

xmlns:fab="http://schemas.android.com/apk/res-auto" 

将以下代码添加到您的布局文件中:

  1. 您还可以通过fab_type xml属性设置按钮类型(正常或迷你),默认为normal:

fab:fab_type="mini"

或者

fab.setType(FloatingActionButton.TYPE_MINI);


1

是的,在新的设计支持库中,提供了浮动操作按钮的默认实现 这里.

为了访问新的 FAB 类,您需要将此依赖项添加到您的 gradle 构建文件中。

compile 'com.android.support:design:22.2.0'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接