在 Android 5.0 上的 AppCompat CardView 上以 XML 格式设置高程 [英] Setting Elevation in XML on AppCompat CardView on Android 5.0

查看:36
本文介绍了在 Android 5.0 上的 AppCompat CardView 上以 XML 格式设置高程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在预览阶段的早期,似乎无法在没有 Java hack 的情况下仅在 CardView 上设置 XML 中的高度.既然正式版出来了,有没有什么办法不用写Java代码来设置高程,用XML做这个?

From what I understand, early in the preview stage there seemed to be no way to set elevation in XML only on CardViews without a hack in Java. Now that the official release is out, is there any way of doing this in XML without writing Java code to set elevation?

我试过 card_view:cardElevation 没有效果.当我使用 5.0 的模拟器时,我以为一切都很好.但是现在我在我的实际设备上使用官方版本,我所有的 CardView 都消失了

I have tried card_view:cardElevation to no effect. I had thought when I was using the emulators for 5.0 everything was fine. But now that I'm using the official version on my actual device all of my CardViews disappeared

Pre Lollipop,效果很好.

Pre Lollipop, it works great.

这是我的完整 xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:id="@+id/cv1"
    card_view:cardElevation="4dp"
    android:layout_margin="6dp"
    card_view:cardCornerRadius="3dp"
    android:layout_height="match_parent">

推荐答案

看起来像margin/padding的问题,尝试设置cardUseCompatPadding 属性为 true.例如:

It looks like a margin/padding problem, try to set the cardUseCompatPadding attribute to true. E.g.:

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="6dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="4dp"
    card_view:cardCornerRadius="3dp">

Android 文档中的说明:

Explanation from Android doc :

CardView 添加了额外的填充来在平台上绘制阴影之前L.

CardView adds additional padding to draw shadows on platforms before L.

这可能会导致卡片在 L 之间和 L 之前具有不同的大小.如果需要将 CardView 与其他 Views 对齐,可能需要 api版本特定的维度资源以说明更改.作为或者,您可以将 cardUseCompatPadding 标志设置为 true,CardView 将在平台 L 及之后添加相同的填充值.

This may cause Cards to have different sizes between L and before L. If you need to align CardView with other Views, you may need api version specific dimension resources to account for the changes. As an alternative, you can set cardUseCompatPadding flag to true and CardView will add the same padding values on platforms L and after.

由于将 cardUseCompatPadding 标志设置为 true 会在 UI 中增加不必要的间隙,因此默认值为 false.

Since setting cardUseCompatPadding flag to true adds unnecessary gaps in the UI, default value is false.

这篇关于在 Android 5.0 上的 AppCompat CardView 上以 XML 格式设置高程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆