安卓AppCompat 21高程

77
在不需要将视图包装在CardView中的情况下,有没有办法在Lollipop以前的设备上向View添加高度?

参考此处:http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html 他们已经针对Action bar说过getSupportActionBar().setElevation(0)。因此,尝试在您的卡片视图中使用相同的方法。 - AruLNadhaN
3
我不打算给CardView增加高度。如果可能的话,我想要将高度添加到任何View上。 - Eliezer
4个回答

80

ViewCompat.setElevation(View, int)当前没有创建任何支架。

目前模拟高度的唯一方法是在 v21 之前应用阴影。在 values 中定义您的样式/布局/可绘制项,并在 values-v21 中进行覆盖。对于按钮,我使用样式重写。对于布局,我通常选择引用覆盖(使用@null 来清除可绘制对象)。

希望将来支持库的更新会添加支架。

这个 Reddit 帖子 跟踪了该更新。

编辑

新的支持设计库实际上会为浮动操作按钮创建支架。


3
还是不行吗?“shims”是什么意思呢? - Daniel Wilson
6
@DanielWilson,一个“shim”是一种解决问题的方法(http://en.wikipedia.org/wiki/Shim_(computing))。由于影子在5.0中是本地支持的,因此需要使用 < 5.0 的“shim”。 - Derk-Jan
@Derk-Jan 很遗憾,即使在你写下这句话的几个月后,Android 5 的市场份额仍不到2%。 - Greg Ennis
抱歉,但这段代码在我的设备上无法运行。我的设备是Android 4.1版本,并且我添加了最新的android-support-v4.jar文件。 - Aaron Lee
@AaronLee“[...]不创建任何shim”。请注意‘no’。 - Derk-Jan
显示剩余4条评论

65

这是一个示例,演示如何在较旧的 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>

1
你能否提供关于 - android:layout_height="@dimen/toolbar_shadow" 的信息? - Ranjithkumar
2
它是阴影的大小,数值越高,工具栏就越“凸起”。 - thiagolr
根据从Google设计网站选择的值,endColor应该是#45000000。然而,这取决于获取值的位置。 - Curious Sam
这是一个非常简单的解决方案。谢谢! - B.K.

48

1
我在RecyclerView适配器中使用了这个技巧,效果非常好。感谢分享! - Alberto Gaona
如何在AlertDialog中实现这个?我有一个应用于对话框的样式,所以我在那里添加了提到的属性,但它看起来很奇怪。它看起来像我添加了多个背景层。有什么线索吗? - cgr
使用系统资源,如@android:drawable/dialog_holo_light_frame是危险的,不是吗?一些移动设备的生产商可能会覆盖系统资源,因此这些资源在不同的设备上可能会显示不同,不是吗? - P. Ilyin
我已经使用这个资源超过2年了,但仍然没有遇到任何问题。它可以在三星、华硕、Nexus、摩托罗拉和联想等品牌的设备上正常工作。 - Ranjithkumar

-5

现在您可以使用模式 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">

在视图中必须使用AppCompat,例如'android.support.v7.widget.AppCompatTextView'。 - abbasalim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接