Android 选择器和文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1219312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android selector & text color
提问by yanchenko
I want a simple TextView
to behave the way simple_list_item_1
in a ListView
does. Here's the XML:
我想要一个简单TextView
的运行方式simple_list_item_1
在ListView
做。这是 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
?
除了(预期)在聚焦状态下不会改变的文本颜色之外,一切正常。我如何使它更改为textAppearanceLargeInverse
?
采纳答案by yanchenko
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
在源中搜索bright_text_dark_focused.xml,添加到res/color 目录下的项目中,然后从TextView 引用为
android:textColor="@color/bright_text_dark_focused"
回答by Klaus Balduino
I got by doing several tests until one worked, so: res/color/button_dark_text.xml
我做了几次测试直到一个成功,所以:res/color/button_dark_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
res/layout/view.xml
res/layout/view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT"
android:textColor="@color/button_dark_text" />
</LinearLayout>
回答by kreker
Here's my implementation, which behaves exactly as item in list (at least on 2.3)
这是我的实现,它的行为与列表中的项目完全相同(至少在 2.3 上)
res/layout/list_video_footer.xml
res/layout/list_video_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/list_video_footer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:drawable/list_selector_background"
android:clickable="true"
android:gravity="center"
android:minHeight="98px"
android:text="@string/more"
android:textColor="@color/bright_text_dark_focused"
android:textSize="18dp"
android:textStyle="bold" />
</FrameLayout>
res/color/bright_text_dark_focused.xml
res/color/bright_text_dark_focused.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#444"/>
<item android:state_focused="true" android:color="#444"/>
<item android:state_pressed="true" android:color="#444"/>
<item android:color="#ccc"/>
</selector>
回答by Manuel Escrig
In order to make it work on selection in a list view use the following code:
为了使其在列表视图中进行选择,请使用以下代码:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#fff"/>
<item android:state_activated="true" android:color="#fff"/>
<item android:color="#000" />
</selector>
Apparently the key is state_activated="true"
state.
显然,关键是state_activated="true"
状态。
回答by Günay Gültekin
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.
这是选择器的示例。如果您使用 eclipse ,当您同时单击 ctrl 和 space 时它不会建议任何内容:/您必须输入它。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_default_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/btn_default_selected"
android:state_focused="true"
android:state_enabled="true"
android:state_window_focused="true" />
<item android:drawable="@drawable/btn_default_normal" />
You can look at for reference;
可以参考一下;
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
回答by Atul O Holic
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).
这个特性确实在 API 1 中可用,称为ColorStateList,我们可以在其中为 Widget 的各种状态提供颜色(正如我们已经知道的)。
It is also very well documented, here.
它也有很好的记录,here。
回答by CoolMind
In res/color
place a file "text_selector.xml":
在res/color
地方文件“text_selector.xml”:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/blue" android:state_focused="true" />
<item android:color="@color/blue" android:state_selected="true" />
<item android:color="@color/green" />
</selector>
Then in TextView
use it:
然后在TextView
使用它:
<TextView
android:id="@+id/value_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text"
android:textColor="@color/text_selector"
android:textSize="15sp"
/>
And in code you'll need to set a click listener.
在代码中,您需要设置一个点击侦听器。
private var isPressed = false
private fun TextView.setListener() {
this.setOnClickListener { v ->
run {
if (isPressed) {
v.isSelected = false
v.clearFocus()
} else {
v.isSelected = true
v.requestFocus()
}
isPressed = !isPressed
}
}
}
override fun onResume() {
super.onResume()
textView.setListener()
}
override fun onPause() {
textView.setOnClickListener(null)
super.onPause()
}
Sorry if there are errors, I changed a code before publishing and didn't check.
抱歉,如果有错误,我在发布之前更改了代码并没有检查。
回答by samis
If using TextViews in tabs this selector definition worked for me (tried Klaus Balduino's but it did not):
如果在选项卡中使用 TextViews,这个选择器定义对我有用(尝试过 Klaus Balduino 的,但没有):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item
android:state_selected="true"
android:state_focused="false"
android:state_pressed="false"
android:color="#000000" />
<!-- Inactive tab -->
<item
android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:color="#FFFFFF" />
</selector>
回答by lilbyrdie
Have you tried setOnFocusChangeListener
? Within the handler, you could change the text appearance.
你试过setOnFocusChangeListener
吗?在处理程序中,您可以更改文本外观。
For instance:
例如:
TextView text = (TextView)findViewById(R.id.text);
text.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
((TextView)v).setXXXX();
} else {
((TextView)v).setXXXX();
}
}
});
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.
然后,无论是否聚焦,您都可以应用所需的任何更改。您还可以使用 ViewTreeObserver 来侦听全局焦点更改。
For instance:
例如:
View all = findViewById(R.id.id_of_top_level_view_on_layout);
ViewTreeObserver vto = all.getViewTreeObserver();
vto.addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() {
public void onGlobalFocusChanged(
View oldFocus, View newFocus) {
// xxxx
}
});
I hope this helps or gives you ideas.
我希望这对您有所帮助或给您一些想法。