有没有办法在前棒棒糖设备中为View
添加高程而不将其包裹在CardView
中?
答案 0 :(得分:72)
ViewCompat.setElevation(View, int)
目前没有创建垫片。
现在模拟高程的唯一方法是在v21之前应用阴影。在values
中定义您的样式/布局/可绘制,并在values-v21
中覆盖它。对于按钮我使用样式覆盖。对于布局,我通常会使用引用覆盖(使用@null
来摆脱drawable)。
希望将来对支持库的更新会增加填充程序。
This reddit thread会跟踪所述更新。
新的support design library实际上会为浮动操作按钮创建填充程序。
答案 1 :(得分:64)
这是一个如何在棒棒糖前设备上的Toolbar
下方添加阴影的示例:
布局应为:
<RelativeLayout
android:id="@+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:animateLayoutChanges="true"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<View
android:id="@+id/toolbar_shadow"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_shadow"
android:layout_below="@id/toolbar"
android:background="@drawable/toolbar_dropshadow" />
</RelativeLayout>
阴影是:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#88444444"
android:startColor="@android:color/transparent" />
</shape>
答案 2 :(得分:47)
我使用
达到同样的效果 android:background="@android:drawable/dialog_holo_light_frame"
我测试的输出:
参考 - https://stackoverflow.com/a/25683148/3879847
更新:如果你想改变这个抽象的颜色,试试@Irfan回答
答案 3 :(得分:-3)
您现在可以使用架构xmlns:app="http://schemas.android.com/apk/res-auto"
和app:elevation
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:elevation="5dp">