Android 主题覆盖所有颜色

Android theme overrides all colors

我有一个 XML 文件,它用颜色和可绘制对象定义了应用程序 UI,但它们似乎都被 themes.xml 的主题覆盖了。我该如何禁用它?

示例:

这个按钮:

<Button
            android:id="@+id/capture_button"
            android:layout_width="match_parent"
            android:background="@drawable/round_button"
            android:layout_height="0dp"
            android:text="@string/capture_button_text_en"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/camera_activity_frameLayout">
</Button>

应具有黑色背景,如 round_button.xml 中所定义:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/black"/>
    <corners android:radius="7dp"/>
    <stroke android:width="1dp" android:color="@color/white"/>
</shape>

不过是紫色,颜色来自主题:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.myproject" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- 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. -->
    </style>
</resources>

如何覆盖这个主题?

由于您的应用程序主题扩展了 Material 组件主题 (Theme.MaterialComponents.DayNight.NoActionBar),它将接管视图 inflation 并用相应的 Material 替换一些小部件组成部分一。这意味着,如果您的视图定义了一个按钮,您将获得一个 MaterialButton and as a consequence it changes how you should control your button's appearance. You should no longer use the background attribute, but the attributes discussed here and here. This process is explained a bit more in this 博客 post。

现在要获得带有白色轮廓的黑色 Material 按钮,您要使用 Widget.MaterialComponents.Button.OutlinedButton 样式:

<Button
    android:id="@+id/capture_button"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:text="@string/capture_button_text_en"
    android:textColor="@color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:shapeAppearance="@style/ShapeAppearance.MyApp.LargeComponent"
    app:backgroundTint="@color/black"
    app:strokeWidth="1dp"
    app:strokeColor="#ffffff"
    app:layout_constraintTop_toBottomOf="@+id/camera_activity_frameLayout"/>

您还需要在您的 styles.xml 中使用以下内容来使角的圆角半径为 7dp(从您的原始背景 drawable 中可以看出):

    <style name="ShapeAppearance.MyApp.LargeComponent" parent="ShapeAppearance.MaterialComponents.MediumComponent">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">7dp</item>
    </style>