android - Android 主题覆盖所有颜色
问题描述
我有一个 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>
如何覆盖这个主题?
解决方案
由于您的应用程序的主题扩展了一个材质组件主题 (Theme.MaterialComponents.DayNight.NoActionBar),它将接管视图膨胀并用相应的材质组件替换一些小部件。这意味着,如果您的视图定义了一个按钮,您将获得一个MaterialButton,因此它会改变您应该如何控制按钮的外观。您不应再使用背景属性,而应使用此处和此处讨论的属性。这个过程在这篇博文中有更多解释。
现在要获得带有白色轮廓的黑色 MaterialButton,您需要使用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 圆角半径(从您的原始背景可绘制):
<style name="ShapeAppearance.MyApp.LargeComponent" parent="ShapeAppearance.MaterialComponents.MediumComponent">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">7dp</item>
</style>
推荐阅读
- javascript - 为什么 todos.map 不是函数?
- javascript - 缺少值时在 JavaScript 中计算平均值
- python - Tk Filedialog 返回一个元组
- c# - 左右对齐gridview列
- ssh - 从 Windows 10 SSH 到远程 Windows 10 的 WSL2
- php - 如何使用查询生成器在 eloquent laravel 中获取乘法列
- python - 带有雪花的熊猫不工作 >> 退出代码 139
- postgresql - /opt/bitnami/scripts/postgresql/entrypoint.sh:第 30 行:exec:max_connections=500:未找到
- python - venv 中缺少 usr/bin 二进制文件?
- windows - 相同 git 版本在 Windows 上 git.exe 的差异