简体   繁体   English

使用深色主题时如何使 Android CardView 可见

[英]How to make Android CardView visible when using Dark Theme

I am investigating CardView and Dark Theme in my current Android application我正在研究当前 Android 应用程序中的 CardView 和深色主题

The majority of my application works as expected when Dark Mode is enables except for the CardViews.当启用暗模式时,除了 CardViews 之外,我的大部分应用程序都按预期工作。

my Gradle resembles this我的 Gradle 和这个很像

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "org.application.investigation"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

  implementation 'androidx.cardview:cardview:1.0.0'

My Theme is as follows:-我的主题如下:-

<resources>

    <style name="AppTheme" parent="@style/Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorError">@color/design_default_color_error</item>
        <item name="android:textColor">?attr/colorOnBackground</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

My CardView layout resembles this:-我的 CardView 布局类似于:-

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_films_cardview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="?android:attr/colorBackground"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="5dp"
    card_view:cardUseCompatPadding="true"
    tools:context=".films.Films">

    <LinearLayout
        android:id="@+id/item_films_film"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:background="?attr/colorSurface"
        android:foreground="?android:attr/selectableItemBackground"
        android:orientation="vertical"
        android:padding="@dimen/default_padding">

        <TextView
            android:id="@+id/film_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textColor="?android:attr/textColorPrimary"
            android:textSize="@dimen/text_heading"
            android:textStyle="bold"
            tools:text="Title Of Film" />

        <TextView
            android:id="@+id/film_director"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textColor="?android:attr/textColorPrimary"
            android:textStyle="italic"
            tools:text="Director Of Film" />

        <TextView
            android:id="@+id/film_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:gravity="center_horizontal"
            android:lineSpacingMultiplier="@dimen/line_spacing_item"
            android:maxLines="3"
            android:textColor="?android:attr/textColorPrimary"
            tools:text="Description Of Film" />

    </LinearLayout>
</androidx.cardview.widget.CardView>

The CardViews are "perfect" in Light Mode, however when I switch to Dark Mode the CardViews are not visible. CardViews 在 Light Mode 下是“完美的”,但是当我切换到 Dark Mode 时 CardViews 不可见。

What mistake(s) have I made ?我犯了什么错误?

Is it possible to employ CardViews in Dark Mode and see them?是否可以在暗模式下使用 CardViews 并查看它们?

Since you are using the Theme.MaterialComponents.DayNight.* theme use the com.google.android.material.card.MaterialCardView instead of androidx.cardview.widget.CardView .由于您使用的是Theme.MaterialComponents.DayNight.*主题,请使用com.google.android.material.card.MaterialCardView而不是androidx.cardview.widget.CardView

The MaterialCardView extends the androidx.cardview.widget.CardView and uses a different style: Widget.MaterialComponents.CardView . MaterialCardView扩展了androidx.cardview.widget.CardView并使用了不同的样式: Widget.MaterialComponents.CardView

Since you are using Theme.MaterialComponents.DayNight.* , the cardView widget must use com.google.android.material.card.MaterialCardView class instead of androidx.cardview.widget.CardView由于您使用的是Theme.MaterialComponents.DayNight.* ,cardView 小部件必须使用com.google.android.material.card.MaterialCardView类而不是androidx.cardview.widget.CardView

So, in your xml, inflate card_view:cardBackgroundColor="?attr/colorSurface" instead of card_view:cardBackgroundColor="?android:attr/colorBackground" , as Material Guideline state that因此,在您的 xml 中,膨胀card_view:cardBackgroundColor="?attr/colorSurface"而不是card_view:cardBackgroundColor="?android:attr/colorBackground" ,作为 Material Guideline 状态

Additionally, the Material Android library provides PrimarySurface styles for components that act as large surfaces and commonly use colorPrimary for their background in light theme.此外,Material Android 库为充当大型表面的组件提供了 PrimarySurface 样式,并且在浅色主题中通常使用 colorPrimary 作为其背景。 These styles will automatically switch between the component's primary colored style in light theme and surface colored style in dark theme.这些样式将自动在浅色主题中的组件原色样式和深色主题中的表面色样式之间切换。

reference : material.io参考: material.io

It is Quite Easy I mean it worked for me so.这很容易我的意思是它对我有用。 first change your CardView to materialCardView because you are using Theme.MaterialComponents.DayNight .首先将CardView更改为materialCardView因为您使用的是Theme.MaterialComponents.DayNight After That Just add style="@style/CardView.Light" this attribute in your XML.之后只需在您的 XML 中添加style="@style/CardView.Light"这个属性。 file You can keep any style as per your need文件您可以根据需要保留任何样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM