我想创建一个 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 操作按钮:
我的应用的 float 操作按钮:
如何为我的按钮添加阴影?
我已经使用了属性提升,但是不起作用
最佳答案
不再需要创建自己的 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/