欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > Android 开发问题:CardView 的阴影效果会受到父容器的裁切

Android 开发问题:CardView 的阴影效果会受到父容器的裁切

2025/6/19 2:47:00 来源:https://blog.csdn.net/weixin_52173250/article/details/148721134  浏览:    关键词:Android 开发问题:CardView 的阴影效果会受到父容器的裁切
  • 在 Android 开发中,CardView 的阴影效果会受到父容器的裁切(见上图),此问题下的父容器可以分为两种
  1. 无 padding 的父容器
<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><androidx.cardview.widget.CardViewandroid:layout_width="492px"android:layout_height="420px"app:cardCornerRadius="10px"app:cardElevation="8px"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" /></androidx.cardview.widget.CardView>
</LinearLayout>
  1. 有 padding 的父容器
<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="20px"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><androidx.cardview.widget.CardViewandroid:layout_width="492px"android:layout_height="420px"app:cardCornerRadius="10px"app:cardElevation="8px"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" /></androidx.cardview.widget.CardView>
</LinearLayout>
处理策略
(1)针对无 padding 的父容器
  1. 禁用裁切,给祖父容器添加 android:clipChildren="false"
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"android:clipChildren="false"tools:context=".ViewShadowActivity"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><androidx.cardview.widget.CardViewandroid:layout_width="492px"android:layout_height="420px"app:cardCornerRadius="10px"app:cardElevation="8px"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" /></androidx.cardview.widget.CardView></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>
  1. 添加 CardView 的外边距,给 CardView 添加 android:layout_margin
<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><androidx.cardview.widget.CardViewandroid:layout_width="492px"android:layout_height="420px"android:layout_margin="20px"app:cardCornerRadius="10px"app:cardElevation="8px"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" /></androidx.cardview.widget.CardView>
</LinearLayout>
  1. 启用 CardView 的兼容模式,给 CardView 添加 app:cardUseCompatPadding="true"
<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><androidx.cardview.widget.CardViewandroid:layout_width="492px"android:layout_height="420px"app:cardUseCompatPadding="true"app:cardCornerRadius="10px"app:cardElevation="8px"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" /></androidx.cardview.widget.CardView>
</LinearLayout>
(2)针对有 padding 的父容器
  • 给父容器添加 android:clipToPadding="false"
<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:clipToPadding="false"android:padding="20px"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><androidx.cardview.widget.CardViewandroid:layout_width="492px"android:layout_height="420px"app:cardCornerRadius="10px"app:cardElevation="8px"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" /></androidx.cardview.widget.CardView>
</LinearLayout>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词