我遇到了一个问题,我尝试了几种方法来面对它,但仍然没有成功。
我的应用使用了多个主题,例如:万圣节、圣诞节等,并且我在小部件上使用了一些颜色属性,例如 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/
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
在Ruby类中,我重写了三个方法,并且在每个方法中,我基本上做同样的事情:classExampleClassdefconfirmation_required?is_allowed&&superenddefpostpone_email_change?is_allowed&&superenddefreconfirmation_required?is_allowed&&superendend有更简洁的语法吗?如何缩短代码? 最佳答案 如何使用别名?classExampleClassdefconfirmation_required?is_a
我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI
如何使用Ruby的默认Curses库获取颜色?所以像这样:puts"\e[0m\e[30;47mtest\e[0m"效果很好。在浅灰色背景上呈现漂亮的黑色。但是这个:#!/usr/bin/envrubyrequire'curses'Curses.noecho#donotshowtypedkeysCurses.init_screenCurses.stdscr.keypad(true)#enablearrowkeys(forpageup/down)Curses.stdscr.nodelay=1Curses.clearCurses.setpos(0,0)Curses.addstr"Hello
状态:我正在构建一个应用程序,其中需要一个可供用户选择颜色的字段,该字段将包含RGB颜色代码字符串。我已经测试了一个看起来很漂亮但效果不佳的。它是“挑剔的颜色”,并托管在此存储库中:https://github.com/Astorsoft/picky-color.在这里我打开一个关于它的一些问题的问题。问题:请建议我在Rails3应用程序中使用一些颜色选择器。 最佳答案 也许页面上的列表jQueryUIDevelopment:ColorPicker为您提供开箱即用的产品。原因是jQuery现在包含在Rails3应用程序中,因此使用基
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
如果我有以下一段Ruby代码:classBlahdefself.bleh@blih="Hello"@@bloh="World"endend@blih和@@bloh到底是什么?@blih是Blah类中的一个实例变量,@@bloh是Blah类中的一个类变量,对吗?这是否意味着@@bloh是Blah的类Class中的一个变量? 最佳答案 人们似乎忽略了该方法是类方法。@blih将是常量Bleh的类Class实例的实例变量。因此:irb(main):001:0>classBlehirb(main):002:1>defself.blehirb
假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案