10
我想使用主題中的顏色將其應用於某些HTML應用正在呈現。我想知道我能否做到這一點?如何從Android主題中提取顏色值(#rgb)?
我期待像他們在指定的themes.xml使用顏色:
<item name="colorBackground">@android:color/background_dark</item>
<item name="textColorPrimary">@android:color/primary_text_dark</item>
所以看着他們,他們都以同樣的方式聲明。所以我想我也可以用同樣的方式訪問它們。
這不是原因。當試圖訪問這些值是這樣的:
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);
我得到這個結果:
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
的結果是不同的。第一個實際上給我的顏色「#fffffff」,這將爲我工作,第二個只給了我一個XML。
我是否需要在這裏跳過更多的箍來解決實際的顏色?我的初衷是否有用?也許它不會工作,因爲顏色可以是任意的drawables?
我沒有找到任何相關文件,但如果你知道任何,請指出我那裏請。
Btw。我也嘗試了getsStyledAttributes(),但是這有基本相同的問題。
[This answer](http://stackoverflow.com/a/6540378/15882)顯示如何將顏色int轉換回其十六進制字符串。 – 2016-03-04 17:48:00