可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
?
回答1:
I got by doing several tests until one worked, so:
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
<?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>
回答2:
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
android:textColor=\"@color/bright_text_dark_focused\"
回答3:
Here\'s my implementation, which behaves exactly as item in list (at least on 2.3)
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
<?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>
回答4:
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.
回答5:
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.
<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
回答6:
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.
回答7:
If using TextViews in tabs this selector definition worked for me (tried Klaus Balduino\'s but it did not):
<?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>
回答8:
Have you tried setOnFocusChangeListener
? Within the handler, you could change the text appearance.
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.
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.