【问题标题】:Cardview not showing shadow along bottomCardview没有在底部显示阴影
【发布时间】:2015-04-02 15:43:24
【问题描述】:

我有以下用于卡片视图的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="2dp">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="0dp">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">
        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

这会导致这些阴影在底部根本没有阴影

但我正在尝试获得类似这种阴影的东西,它可以更好地包围整张卡片。

我尝试更改海拔高度但无济于事,搜索此问题似乎除了没有阴影的人根本没有问题,这不是我的情况。

我怎样才能得到正确的阴影?

谢谢。

【问题讨论】:

标签: android android-layout android-5.0-lollipop shadows


【解决方案1】:

您需要在卡片视图中添加此 xml 属性

app:cardUseCompatPadding="true"

【讨论】:

  • 在顶部,类似于:&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" ...
【解决方案2】:

clipChildren="false" 添加到父LinearLayout 也应该可以工作。

【讨论】:

    【解决方案3】:

    使用 card_view:cardElevation 属性设置海拔

    【讨论】:

    • 正如我的问题中所解释的,我试过这个,但这不是问题。看了MLProgrammer-CiM的评论,原来是我需要用card_view:cardUseCompatPadding="true"
    【解决方案4】:

    在您的 cardView 中添加这些行

        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
    

    【讨论】:

    • 请考虑为您的答案添加一些解释。
    【解决方案5】:

    我遇到了同样的问题,直到我想起如果父级包裹其内容,那么它不会包裹阴影,所以我给了父级比子级更大的尺寸,这样它将包括子级和阴影孩子的

     <androidx.constraintlayout.widget.ConstraintLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="110dp"
    android:layout_height="110dp">
    
    <androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:card_view="http://schemas.android.com/tools"
        android:layout_width="98dp"
        android:layout_height="98dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="5dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <androidx.appcompat.widget.AppCompatImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            app:srcCompat="@drawable/manhattan" />
    
      
    </androidx.cardview.widget.CardView>
    
      </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 2017-09-10
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 2015-08-04
      相关资源
      最近更新 更多