【发布时间】:2015-01-13 19:20:25
【问题描述】:
我想创建一个浮动操作按钮(将项目添加到列表视图),如谷歌日历,保持与棒棒糖之前的 Android 版本(5.0 之前)的兼容性。
我创建了这个布局:
活动 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>
结果相似,但没有阴影,这是材料设计的一个特点:
日历的浮动操作按钮:
我的应用的浮动操作按钮:
如何为我的按钮添加阴影?
我已经使用了属性提升,但是不起作用
【问题讨论】:
-
海拔 API 仅适用于 Lollipop,您是否在 Lollipop 设备上尝试过?
-
现在可能是您接受给定答案之一的时候了。
-
事实上,在我的 4.4.2 三星 Galaxy S III 上,阴影就在那里,这要归功于 AppCompat。