如何为TextView定义ColorStateList?
当我的 ListViewItem
突出显示时,我希望文本变成白色。我该如何定义这个?
<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>
When my ListViewItem
is highlighted, I want the text to turn white. How can I define this?
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建文件 res/drawable/text_color.xml:
然后使用 xml 中的
@drawable/text_color
(或代码中的R.drawable.text_color
)作为列表视图项的文本颜色。Create file res/drawable/text_color.xml:
Then use
@drawable/text_color
from xml (orR.drawable.text_color
from code) as text color for your list view items.试试这个...
首先,创建一个颜色状态列表
text_color.xml
放置在res/color
目录中。其次,使用
方法获取颜色状态列表。
第三,根据您的要求启用(true)或禁用(false),
Try this...
First, create a color state list
text_color.xml
placed inres/color
directory.Second, use
method to get color state list.
Third, enable(true) or disable(false) based on your requirements,
除了上面其他人所说的之外,我想强调一点,取自下面的网址。
https://developer.android.com/reference/android/content/ res/ColorStateList.html
注意:状态规格列表将按照它们在 XML 文件中出现的顺序进行匹配。因此,更具体的项目应放在文件的前面。没有状态规范的项目被认为与任何状态集匹配,并且通常可用作用作默认值的最终项目。
重要的是,您必须在选择器标记的底部拥有更广泛的条件。
In addition to what others have stated above, I would like to highlight one point, taken from the below url.
https://developer.android.com/reference/android/content/res/ColorStateList.html
Note: The list of state specs will be matched against in the order that they appear in the XML file. For this reason, more-specific items should be placed earlier in the file. An item with no state spec is considered to match any set of states and is generally useful as a final item to be used as a default.
It's important that you have the broader condition towards the bottom in the selector tag.