android - 如何使用 AppCompat v21 在 android 中创建 float 操作按钮 (FAB)?

标签 android xml

我想创建一个 float 操作按钮(将项目添加到 ListView ),如谷歌日历,保持与 Lollipop 之前的 Android 版本(5.0 之前)的兼容性。

我创建了这个布局:

Activity main_activity.xml:

<LinearLayout ... >

     <include
         layout="@layout/toolbar"/>

     <RelativeLayout ... >

     <!-- My rest of the layout -->

          <!-- Floating action button -->
          <ImageButton style="@style/AppTheme"
                     android:layout_width="60dp"
                     android:layout_height="60dp"
                     android:text="New Button"
                     android:id="@+id/button"
                     android:src="@drawable/ic_fab"
                     android:background="@drawable/fab"
                     android:layout_alignParentBottom="true"
                     android:layout_alignParentRight="true"
                     android:layout_marginBottom="24dp"
                     android:layout_marginRight="24dp"/>

     </RelativeLayout>

 </LinearLayout>

可绘制的 fab.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
    <solid android:color="#ffa48bc0"/>
</shape>

样式样式.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#ff1d79b1</item>
        <item name="colorPrimaryDark">#ff084d95</item>
    </style>
</resources>

结果相似,但没有阴影,这是 Material 设计的一个特点:

日历的 float 操作按钮:

Calendar's floating action button

我的应用的 float 操作按钮:

My app's floating action button

如何为我的按钮添加阴影?

我已经使用了属性提升,但是不起作用

最佳答案

不再需要创建自己的 FAB 或使用第三方库,它已包含在 AppCompat 22 中。

https://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html

只需在您的 gradle 文件中添加名为 design 的新支持库:

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

...你很高兴:

<android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_happy_image" />

关于android - 如何使用 AppCompat v21 在 android 中创建 float 操作按钮 (FAB)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26963219/

相关文章:

android - Android Studio多个项目样式问题

android - 连接到 ADB 服务器的最大设备数

php - 如何在 PHP 5 中启用 XSLT 函数?

xml - 在冷融合中使用 cfif

android - 如何为 iOS 和 Android 创建多轨播放器(如 DJ 混音器)?

android - Robotium 中的 EnterText()

java - Android Kotlin : How to update recyclerView after async call? 获取 CalledFromWrongThreadException

xml - 是否可以在 Odoo 中的字段上实现条件属性?

xml - 如何制作一个包含关系的XML文档?

c# - Xml 序列化为 XmlAttribute : namespace prefix lost when Element in same namespace