首页 > 解决方案 > Android - Cardview 不显示阴影,也不剪裁儿童

问题描述

在项目中,我在 Recyclerview 视图持有者/项目内的 ConstraintLayout 中使用 cardviews。它在 android studio 预览中正常显示,但是当我在手机或模拟器中运行项目时,不显示 cardview 的阴影,并且 cardview 内的 imageview 也没有被裁剪

此布局显示为普通平面布局,没有任何类型的阴影或剪裁。我该如何解决这个..?

布局.xml


<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/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/c_FFFFFF_white"
    android:padding="@dimen/dimen_8">


    <!--<FrameLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dimen_34"
        android:layout_marginTop="@dimen/dimen_24"
        android:layout_marginEnd="@dimen/dimen_24"
        android:layout_marginBottom="@dimen/dimen_24"
        android:background="@drawable/bg_rounded_corner_with_shadow"
        android:clipChildren="true"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">-->

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dimen_34"
        android:layout_marginTop="@dimen/dimen_24"
        android:layout_marginEnd="@dimen/dimen_24"
        android:layout_marginBottom="@dimen/dimen_24"
        android:clipChildren="true"
        android:clipToPadding="true"
        app:cardCornerRadius="@dimen/dimen_8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dimen_60dp"
            android:orientation="horizontal">

            <com.example.ui.widgets.appWidgets.AppTextView
                android:id="@+id/titleTextView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginStart="@dimen/dimen_24"
                android:layout_marginEnd="@dimen/dimen_24"
                android:maxLines="1"
                android:textColor="@color/black"
                android:textSize="@dimen/text_size_12"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@id/linkImageView"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <com.example.ui.widgets.appWidgets.AppImageView
                android:id="@+id/linkImageView"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@color/black_overlay"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
    <!--    </FrameLayout>-->

    <com.example.ui.widgets.custom.FontIconTextView
        android:id="@+id/fontIconTextView"
        android:layout_width="@dimen/dimen_32"
        android:layout_height="@dimen/dimen_32"
        android:background="@drawable/bg_circle_icon_font"
        android:elevation="@dimen/dimen_16"
        app:layout_constraintBottom_toTopOf="@+id/cardView"
        app:layout_constraintEnd_toStartOf="@+id/cardView"
        app:layout_constraintStart_toStartOf="@+id/cardView"
        app:layout_constraintTop_toTopOf="@+id/cardView" />
</androidx.constraintlayout.widget.ConstraintLayout>

样式.xml

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.Bridge">
        <item name="colorPrimary">@color/c_F5FCF9_cyan</item>
        <item name="colorPrimaryDark">@color/c_F5FCF9_cyan</item>
        <item name="colorAccent">@color/c_39B0E5_blue</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

构建.gradle


apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

apply plugin: 'com.google.gms.google-services'

android {

    compileSdkVersion 29

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 4967
        versionName "3.0.0"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            debuggable false
            multiDexEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        debug {
            debuggable true
        }
    }

    compileOptions {
        sourceCompatibility = "1.8"
        targetCompatibility = "1.8"
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

    sourceSets {
        main {
            res.srcDirs =
                    [
                            'src/main/res',
                            'src/main/res/layouts/login',
                            'src/main/res/layouts/dialog',
                            'src/main/res/layouts/learning',
                            'src/main/res/layouts/common',
                            'src/main/res/layouts/sideMenu',
                            'src/main/res/layouts/exercise',
                            'src/main/res/layouts/quiz',
                            'src/main/res/layouts/feedback',
                            'src/main/res/layouts/adapter',
                            'src/main/res/layouts/discussion',
                            'src/main/res/layouts/template',
                            'src/main/res/layouts/points',
                            'src/main/res/layouts/submission',
                            'src/main/res/layouts/team',
                            'src/main/res/layouts/audio',
                            'src/main/res/layouts/external',
                            'src/main/res/layouts/downloads',
                            'src/main/res/layouts/announcement',
                            'src/main/res/layouts'
                    ]
            assets.srcDirs = ['src/main/assets', 'src/main/assets/']
        }
    }

    kapt {
        generateStubs = true
    }

    androidExtensions {
        experimental = true
    }


    packagingOptions {
        pickFirst 'META-INF/*'
        exclude 'META-INF/atomicfu.kotlin_module'
        exclude 'META-INF/MANIFEST.MF'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation "com.android.support:exifinterface:29.1.0"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion"
    implementation "com.squareup.retrofit2:adapter-rxjava2:2.6.1"
    // reactive
    implementation "io.reactivex.rxjava2:rxjava:$rootProject.rxjava2Version"
    //noinspection GradleDependency
    implementation "io.reactivex.rxjava2:rxandroid:$rootProject.rxandroidVersion"
    implementation 'com.squareup.okhttp3:logging-interceptor:4.4.0'
    implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.4.0'
    implementation 'com.squareup.okhttp3:okhttp:4.4.0'
    // Facebook Login only
    implementation 'com.facebook.android:facebook-login:5.15.3'
    // google play
    implementation 'com.google.android.gms:play-services-auth:18.1.0'
    //glide
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'

    //JWPlayer
    implementation 'com.longtailvideo.jwplayer:jwplayer-core:3.6.0'
    implementation 'com.longtailvideo.jwplayer:jwplayer-common:3.6.0'

    //Youtube Player ====> https://github.com/PierfrancescoSoffritti/android-youtube-player
    implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.4'

    //Firebase
    implementation 'com.google.firebase:firebase-analytics:17.4.4'

    //Pusher
    implementation 'com.pusher:pusher-java-client:2.0.2'



    //Amazon S3
    implementation('com.amazonaws:aws-android-sdk-mobile-client:2.15.+@aar') { transitive = true }
    implementation 'com.amazonaws:aws-android-sdk-s3:2.15.2'
    implementation 'com.amazonaws:aws-android-sdk-core:2.15.2'
    implementation 'com.amazonaws:aws-android-sdk-cognito:2.7.+'


    //In-App Updates
    // This dependency is downloaded from the Google’s Maven repository.
    // So, make sure you also include that repository in your project's build.gradle file.
    implementation 'com.google.android.play:core:1.6.5'


    //Kotlin coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3'

    //Room DB
    implementation "androidx.room:room-runtime:$rootProject.roomVersion"
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    kapt "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

    //viewmodel extension
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.archLifecycleVersion"

    implementation "org.jsoup:jsoup:1.12.1"

    debugImplementation 'im.dino:dbinspector:4.0.0'
}

标签: androidandroid-layoutcardview

解决方案


现在问题已解决。出于某种原因,我有

android:hardwareAccelerated="false"

为活动。我删除了这一行,问题解决了。


推荐阅读