我想使用主题中的颜色将其应用于我的应用渲染的某些HTML.我想知道我能做到吗?
我希望使用在themes.xml中指定的颜色:
<item name="colorBackground">@android:color/background_dark</item>
<item name="textColorPrimary">@android:color/primary_text_dark</item>
Run Code Online (Sandbox Code Playgroud)
因此,看着他们,他们以同样的方式宣布.所以我想我也可以用同样的方式访问它们.
这不是原因.尝试以这种方式访问这些值时:
TypedValue tv = new TypedValue();
getTheme().resolveAttribute(android.R.attr.colorBackground, tv, true);
System.out.println("tv.string=" + tv.string);
System.out.println("tv.coerced=" + tv.coerceToString());
int colorResourceId = getResources().getColor(tv.resourceId);
System.out.println("colorResourceId=" + colorResourceId);
tv = new TypedValue();
getTheme().resolveAttribute(android.R.attr.textColorPrimary, tv, true);
System.out.println("tv.string=" + tv.string);
System.out.println("tv.coerced=" + tv.coerceToString());
colorResourceId = getResources().getColor(tv.resourceId);
System.out.println("colorResourceId=" + colorResourceId);
Run Code Online (Sandbox Code Playgroud)
我得到了这个结果:
I/System.out( 1578): tv.string=null
I/System.out( 1578): tv.coerced=#ffffffff
I/System.out( 1578): colorResourceId=-1
I/System.out( 1578): tv.string=res/color/primary_text_light.xml
I/System.out( 1578): tv.coerced=res/color/primary_text_light.xml
I/System.out( 1578): colorResourceId=-16777216
Run Code Online (Sandbox Code Playgroud)
结果不同.第一个实际上给了我"#fffffff"的颜色,这对我有用,第二个只给我一个xml.
我是否需要在这里跳过几个箍来解决实际的颜色?我的初衷是否有用?也许它不起作用,因为颜色可以是任意的drawables?
我没有找到任何相关的文件,但如果你知道的话,请指点我.
顺便说一句.我也尝试过obtainStyledAttributes(),但这基本上都是一样的问题.
我想你应该重新命名colorResourceId,以myColor或类似的东西,因为这是它应该是在你的代码,据我可以告诉.
-16777216等同于0xFF000000,这是黑色,所以可能你的主题是白色背景上的黑色文字.
| 归档时间: |
|
| 查看次数: |
4661 次 |
| 最近记录: |