0

我正在尝试在我的应用程序中设置灯光模式的背景颜色。我显示的卡片在亮或暗模式下应该是白色的。

我正在阅读的所有内容都告诉我,我可以简单地将行: app:cardBackgroundColor="#FFEF00" 放入我的<androidx.cardview.widget.CardView>,但这似乎并没有覆盖我themes.xml背景中的颜色。

我希望背景颜色为浅灰色,卡片背景为白色(在浅色模式下)。

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/movie_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/progress_bar"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        card_view:cardCornerRadius="16dp"
        app:cardBackgroundColor="#FFEF00"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/movie_photo"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_centerHorizontal="true"
                android:contentDescription="Movie Image" />

            <TextView
                android:id="@+id/movie_title"
                android:layout_below="@+id/movie_photo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:text="Title: "
                android:textSize="15sp" />

            <TextView
                android:id="@+id/movie_overview"
                android:layout_below="@+id/movie_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:text="OverView : " />

            <TextView
                android:id="@+id/movie_rating"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/movie_overview"
                android:layout_margin="5dp"
                android:text="Rating : "
                android:textSize="15sp" />


        </RelativeLayout>

    </androidx.cardview.widget.CardView>

</RelativeLayout>

我的主题.xml 文件:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MovieSpotter" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
        <item name="android:background">@color/gray</item>
    </style>
</resources>
4

3 回答 3

0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/movie_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progress_bar"
android:background="#D3D3D3"
android:orientation="vertical">

<androidx.cardview.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    app:cardBackgroundColor="#FFFFFFFF"
    card_view:cardCornerRadius="16dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/movie_photo"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_centerHorizontal="true"
            android:contentDescription="Movie Image" />

        <TextView
            android:id="@+id/movie_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/movie_photo"
            android:layout_margin="5dp"
            android:text="Title: "
            android:textSize="15sp" />

        <TextView
            android:id="@+id/movie_overview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/movie_title"
            android:layout_margin="5dp"
            android:text="OverView : " />

        <TextView
            android:id="@+id/movie_rating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/movie_overview"
            android:layout_margin="5dp"
            android:text="Rating : "
            android:textSize="15sp" />


    </RelativeLayout>

</androidx.cardview.widget.CardView>

</RelativeLayout>

你想设置背景颜色浅灰色和卡片颜色白色而不是试试这个......

谢谢...

于 2021-11-01T09:23:57.900 回答
0

我最终CardView在我的主题中为小部件创建了卡片样式,并决定使其具有适应性(浅色/深色模式)。

我在 XML 文件中添加style="Widget.App.CardStyle"到 CardView:

<androidx.cardview.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        style="Widget.App.CardStyle"
        card_view:cardCornerRadius="16dp">

然后在我看来,theme.xml我在现有<style> </style>标签下面添加了这个:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name=...>
...
</style>

    <style name="Widget.App.CardStyle"
        parent="Widget.MaterialComponents.CardView">
        <item name="materialThemeOverlay">@style/Widget.App.CardStyle</item>
    </style>
    <style name="materialThemeOverlay">
        <item name="colorPrimary">@color/white</item>
    </style>

</resources>

以及随附的暗模式视图night/themes.xml

<resources...
<style...
</style>
    
<style name="Widget.App.CardStyle"
            parent="Widget.MaterialComponents.CardView">
            <item name="materialThemeOverlay">@style/Widget.App.CardStyle</item>
        </style>
        <style name="materialThemeOverlay">
            <item name="colorPrimary">@color/black</item>
        </style>

</resources>
于 2021-11-02T16:07:15.887 回答
0

选项1:


如果您不希望这种行为是将其添加到您的主题中。

<item name="elevationOverlayEnabled">false</item>

您还可以通过更改 alpha 将其调整为另一种颜色甚至更微妙的叠加版本:

<item name="elevationOverlayColor">#FFEF00</item>

您可以在此处阅读: https ://material.io/develop/android/theming/dark/在该部分Elevation Overlays


选项 2:


样式.xml:

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
于 2021-11-01T10:43:09.350 回答