android - 在 Android 5.0 上的 AppCompat CardView 上以 XML 设置海拔

标签 android android-appcompat android-cardview android-elevation

据我了解,在预览阶段的早期,似乎无法在没有 Java 破解的情况下仅在 CardView 上设置 XML 中的高度。现在正式发布了,有没有什么方法可以在 XML 中做到这一点,而无需编写 Java 代码来设置海拔?

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

Pre Lollipop,效果很好。

这是我的完整 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">

最佳答案

看起来像边距/填充问题,请尝试设置 cardUseCompatPadding属性为真。例如:

<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 文档的解释:

CardView adds additional padding to draw shadows on platforms before 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.

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

关于android - 在 Android 5.0 上的 AppCompat CardView 上以 XML 设置海拔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26997109/

相关文章:

android - 无法从 Assets 文件夹打开数据库 [Android]

android - 在 CardView 展开时滚动

android - DrawerLayout 内的 CardView

android - 以安全的方式在Android上存储API key 吗?

android - 从语音识别 Intent 记录/保存音频

android.support.v7.widget.Toolbar VectorDrawableCompat 使用支持库时出现 IllegalStateException 24+

java - 如何使用此 Activity 解决 : java. lang.IllegalStateException : You need to use a Theme. AppCompat 主题(或后代)?

android - AppCompat、Vectors 和 4.1.2 发生奇怪的崩溃

Android SwipeRefreshLayout 与 RecyclerView

android - 用于搜索要在 build.gradle 中使用的 android 库的网站