Android 选择器 & 文字颜色
我希望一个简单的 TextView
的行为方式与 ListView
中的 simple_list_item_1
相同。 XML 如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:gravity="center" android:focusable="true"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@android:drawable/list_selector_background" />
除了文本颜色(预期)在聚焦状态下不会改变之外,一切正常。 如何将其更改为 textAppearanceLargeInverse
?
I want a simple TextView
to behave the way simple_list_item_1
in a ListView
does. Here's the XML:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:gravity="center" android:focusable="true"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@android:drawable/list_selector_background" />
Everything works except for the text color that (expectedly) doesn't change in focused state. How do I make it change to textAppearanceLargeInverse
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我通过进行多次测试直到其中一个有效,所以:
res/color/button_dark_text.xml
res/layout/view.xml
I got by doing several tests until one worked, so:
res/color/button_dark_text.xml
res/layout/view.xml
选择器也是这里的答案。
在源中搜索 Bright_text_dark_focused.xml,将其添加到 res/color 目录下的项目中,然后从 TextView 中引用为
And selector is the answer here as well.
Search for bright_text_dark_focused.xml in the sources, add to your project under res/color directory and then refer from the TextView as
为了使其能够在列表视图中进行选择,请使用以下代码:
显然,关键是
state_activated="true"
state。In order to make it work on selection in a list view use the following code:
Apparently the key is
state_activated="true"
state.这是我的实现,其行为与列表中的项目完全相同(至少在 2.3 上)
res/layout/list_video_footer.xml
res/color/bright_text_dark_focused.xml
Here's my implementation, which behaves exactly as item in list (at least on 2.3)
res/layout/list_video_footer.xml
res/color/bright_text_dark_focused.xml
在
res/color
中放置一个文件“text_selector.xml”:然后在
TextView
中使用它:在代码中,您需要设置一个点击侦听器。
抱歉,如果有错误,我在发布之前更改了代码并且没有检查。
In
res/color
place a file "text_selector.xml":Then in
TextView
use it:And in code you'll need to set a click listener.
Sorry if there are errors, I changed a code before publishing and didn't check.
这是选择器的示例。 如果您使用 eclipse ,当您单击 ctrl 和 space 时它不会提出任何建议:/您必须键入它。
您可以参考一下;
http://developer.android.com/guide/topics/resources /drawable-resource.html#StateList
Here is the example of selector. If you use eclipse , it does not suggest something when you click ctrl and space both :/ you must type it.
You can look at for reference;
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
我总是使用上面的解决方案,之后没有进行更多搜索。 ;-)
然而,今天我遇到了一些事情并想分享它。 :)
这个功能确实可以从 API 1 中获得,被称为 ColorStateList,我们可以在其中为 Widget 的各种状态提供颜色(正如我们已经知道的那样)。
它也有很好的文档记录,此处。
I always used the above solution without searching more after this. ;-)
However, today I came across something and thought of sharing it. :)
This feature is indeed available from API 1 and is called as ColorStateList, where we can supply a color to various states of Widgets (as we already know).
It is also very well documented, here.
如果在选项卡中使用 TextViews,则此选择器定义对我有用(尝试了 Klaus Balduino 的但没有):
If using TextViews in tabs this selector definition worked for me (tried Klaus Balduino's but it did not):
您尝试过
setOnFocusChangeListener
吗? 在处理程序中,您可以更改文本外观。例如:
然后,您可以在焦点或不焦点时应用您想要的任何更改。 您还可以使用 ViewTreeObserver 来监听全局焦点变化。
例如:
我希望这对您有所帮助或给您带来想法。
Have you tried
setOnFocusChangeListener
? Within the handler, you could change the text appearance.For instance:
You can then apply whatever changes you want when it's focused or not. You can also use the ViewTreeObserver to listen for Global focus changes.
For instance:
I hope this helps or gives you ideas.