编程建立在Android的风格,而无需引用资源 [英] programmatically create styles in Android without referring to resources

查看:165
本文介绍了编程建立在Android的风格,而无需引用资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,读取从XML文档中的文本,然后显示在屏幕上的文本。我希望能够创造编程基于不XML文档中给出​​的参数(字体,大小,颜色,粗体/斜体等) TextAppearanceSpan 对象依托资源文件( SpannableString 在我的TextView)。

I'm working on an app that reads in text from an XML document and then displays that text on the screen. I want to be able to create a TextAppearanceSpan object programmatically based on parameters given in the XML document (font, size, color, bold/italic, etc.) that don't rely on Resource files (for SpannableStrings in my TextView).

我在看下面的构造:

TextAppearanceSpan(字符串的家庭,诠释风格,诠释大小,颜色ColorStateList,ColorStateList LINKCOLOR)

但我似乎无法找到如何 Col​​orStateList 的工作的任何信息。正是我试图做甚至可能吗?

but I can't seem to find any information on how ColorStateLists work. Is what I'm trying to do even possible?

推荐答案

您可以查看源$ C ​​$ C为ColorStateList这里:

You can look at the source code for ColorStateList here:

<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.1_r1/android/content/res/ColorStateList.java\"相对=nofollow> grep的code:ColorStateList

例如,下面的XML选择器:

For example, the following XML selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true" android:color="@color/testcolor1"/>
   <item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" />
   <item android:state_enabled="false" android:color="@color/testcolor3" />
   <item android:color="@color/testcolor5"/>
 </selector>

是等效于以下code:

is equivalent to the following code:

int[][] states = new int[4][];
int[] colors = new int[4];

states[0] = new int[] { android.R.attr.state_focused };
states[1] = new int[] { android.R.attr.state_pressed, -android.R.attr.state_enabled };
states[2] = new int[] { -android.R.attr.state_enabled };
states[3] = new int[0];

colors[0] = getResources().getColor(R.color.testcolor1);
colors[1] = getResources().getColor(R.color.testcolor2);
colors[2] = getResources().getColor(R.color.testcolor3);
colors[3] = getResources().getColor(R.color.testcolor5);

ColorStateList csl = new ColorStateList(states, colors);

什么颜色的状态,以及如何选择工作文档这里

这篇关于编程建立在Android的风格,而无需引用资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆