【问题标题】:Setting Elevation in XML on AppCompat CardView on Android 5.0在 Android 5.0 上的 AppCompat CardView 上以 XML 格式设置海拔
【发布时间】:2015-01-15 19:21:04
【问题描述】:

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

我试过card_view:cardElevation 没有效果。当我将模拟器用于 5.0 时,我曾认为一切都很好。但是现在我在我的实际设备上使用正式版,我所有的CardViews 都消失了

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">

【问题讨论】:

  • 我已经解决了这个问题,但还没有答案。当我升级到棒棒糖时,不知何故,边距或填充消失了,所以他们看不到卡片的角落或边缘,所以它是完全平坦的。

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


【解决方案1】:

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

<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 之前添加了额外的填充以在平台上绘制阴影 L.

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

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

【讨论】:

  • card_view:cardUseCompatPadding="true" 使海拔工作,但也添加了一堆填充......
  • cardElevation 的默认值是多少?
  • 神奇的应用程序来了:cardCornerRadius="3dp" 谢谢
【解决方案2】:

您必须使用cardElevation 属性。
Androidx 库:

您可以使用官方Material Components library中包含的MaterialCard

implementation 'com.google.android.material:material:1.x.x'

在你的布局中:

    <com.google.android.material.card.MaterialCardView
        app:cardElevation="xxdp"
        app:cardUseCompatPadding="true"
        ..>

或者 androidx 包中的 CardView

implementation 'androidx.cardview:cardview:1.x.x'

在你的布局中:

     <androidx.cardview.widget.CardView
        app:cardElevation="xxdp"
        app:cardUseCompatPadding="true"
        ..>

旧支持库

<android.support.v7.widget.CardView
    app:cardElevation="xxdp" 
    app:cardUseCompatPadding="true"
    ..>

【讨论】:

  • 不,我又试了一次,还是不行。我在顶部添加了完整的 xml。
  • 为我工作。默认海拔是多少?
  • 标高卡静止标高:2dp 卡升高标高:8dp 卡的默认标高为 2dp。在桌面上,卡片的静止高度为 0dp,悬停时的高度为 8dp。来自google.com/design/spec/components/…
  • 已弃用
  • @Makarand True。设计库不再维护,但我在 5 年前回答了这个问题。无论如何,我已经用 andriodx 详细信息更新了答案。
【解决方案3】:

如果你有这条线

android:hardwareAccelerated="false"

在清单应用程序标记中,您的阴影未显示。尝试删除此行

或使用

android:hardwareAccelerated="true"

这对我有用!我希望它也对你有用:)

【讨论】:

  • 是的!那是问题!太感谢了!但不是由于我用于背景的位图图像,应用程序变慢了:(
【解决方案4】:

它通过添加解决了我

xmlns:card_view="http://schemas.android.com/apk/res-auto"

例如:

<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="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    card_view:cardCornerRadius="5dp">

【讨论】:

    【解决方案5】:

    我在卡片视图上添加了一个 app:cardElevation="8dp" 属性,所以它会像:

    <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                android:src="@drawable/kmp_warna1" />
    
        </android.support.v7.widget.CardView>
    

    希望对你有帮助

    【讨论】:

      【解决方案6】:

      在cardView中设置

          <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">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-25
        • 2014-12-30
        • 2021-07-19
        • 2019-03-23
        • 2015-01-15
        • 2018-01-13
        • 2023-03-06
        • 1970-01-01
        相关资源
        最近更新 更多