在不需要将视图包装在CardView中的情况下,有没有办法在Lollipop以前的设备上向View添加高度?
ViewCompat.setElevation(View, int)
当前没有创建任何支架。
目前模拟高度的唯一方法是在 v21 之前应用阴影。在 values
中定义您的样式/布局/可绘制项,并在 values-v21
中进行覆盖。对于按钮,我使用样式重写。对于布局,我通常选择引用覆盖(使用@null
来清除可绘制对象)。
希望将来支持库的更新会添加支架。
这个 Reddit 帖子 跟踪了该更新。
新的支持设计库实际上会为浮动操作按钮创建支架。
这是一个示例,演示如何在较旧的 Android 设备上,在 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>
android:layout_height="@dimen/toolbar_shadow"
的信息? - Ranjithkumar我使用同样的方法来达到相同的效果,
android:background="@android:drawable/dialog_holo_light_frame"
经过测试的输出:
参考 - https://dev59.com/4GEi5IYBdhLWcg3whccg#25683148
更新: 如果要更改此可绘制对象的颜色,请尝试@Irfan的答案
现在您可以使用模式 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">
CardView
增加高度。如果可能的话,我想要将高度添加到任何View
上。 - Eliezer