gpt4 book ai didi

android - 上下文/覆盖主题颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:12:07 30 4
gpt4 key购买 nike

我遇到了一个问题,我尝试了几种方法来面对它,但仍然没有成功。

我的应用使用了多个主题,例如:万圣节、圣诞节等,并且我在小部件上使用了一些颜色属性,例如 TabLayout 背景、文本颜色等,以将应用背景化。

问题是:如何根据主题上下文使用具有不同值的相同颜色属性?

所以,基本上这是声明颜色的正常方法:

<color name="mapMarkerSelectedTextColor">@android:color/white</color>
<color name="mapLoadingIndicatorColor">@color/white</color>

但是,主题和颜色是不可变的,所以我想,也许我可以覆盖每个主题中的那些颜色,例如:

    <item name="mapMarkerUnselectedTextColor">@color/christmas_red</item>
<item name="mapMarkerSelectedTextColor">@color/white</item>

=> 不成功

其他线索,将这些颜色声明为属性:

<attr name="mapLoadingIndicatorColor" format="reference|color" />
<attr name="map_autocomplete_accent_color" format="reference|color" />

并在我的 XML 中使用主题,如下所示:“?attr/mapLoadingIndicatorColor”。但是此功能仅在 Lollipop 版本后才允许使用,并且之前会导致崩溃。

我已经阅读了很多关于主题定制、颜色覆盖的内容,但从未找到针对这种情况的明确解决方案。

还是谢谢。

最佳答案

你提到过:

And use theme in my XML like this : "?attr/mapLoadingIndicatorColor". But this features is only allowed since Lollipop version and cause crashs before.

我不确定 ?attr/something 不能在 Lollipop 之前使用(Lollipop 的 API 级别为 21),因为我在模拟器中的 API 级别为 16 的设备上使用它并且它工作正常。 I used it like below to change the background color of a button when different theme is chosen:

在 activity_main.xml 中(在布局文件夹中):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A button"
style="?attr/myButton"/>

</LinearLayout>

在 attrs.xml 中(在 values 文件夹中):

<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myButton" format="reference"></attr>
</resources>

在 styles.xml 中(在 values 文件夹中):

<resources>

<!-- default theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="myButton">@style/defaultButtonStyle</item>
</style>

<style name="defaultButtonStyle" parent="android:Widget.Button">
<item name="android:background">@color/green</item>
</style>

<!-- custom theme -->
<style name="AppTheme.CustomTheme">
<item name="myButton">@style/customButtonStyle</item>
</style>

<style name="customButtonStyle" parent="android:Widget.Button">
<item name="android:background">@color/blue</item>
</style>

</resources>

实际上,我对 Android 编程还是很陌生,如果你能具体说明你在哪里找到 ?attr/mapLoadingIndicatorColor 会导致 pre-Lollipop 崩溃的语句,那就太好了! (我到处都找不到,我只知道你不能使用 elevate attribute pre-Lollipop)

关于android - 上下文/覆盖主题颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34859814/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com