I've come with a problem. I need to highlight the selected item within a Gallery. I've tried changing the appearance of the selected view through this method:
@Override
public void onItemSelected(AdapterView<?> parent, V开发者_运维技巧iew view, int position, long id)
The second parameter of that view is the current selected view, and I'm trying in this case, increase the size of the text. However this doesn't work, not even if I call invalidate in the selected item or in the entire Gallery.
This is the code I use to change the textview text size
TextView textview = (TextView)view;
textview.setTextSize(50, TypedValue.COMPLEX_UNIT_SP);
textview.invalidate();
Do you know how to do this? Thanks
Your implementation works, but it does not return the text size to normal once the item becomes unselected - the text stays larger size.
This should fix it:
private View lastview;
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (lastview != null) lastview.setBackgroundColor(0xFFFFFFFF);
lastview = arg1;
arg1.setBackgroundColor(0xFFFF0000);
}
You can set text size instead of color, or do whatever you want to the style of it.
Try StateListDrawable - looks apt for your situation.
UPDATE: You have reversed the parameters in setTextSize. It should be:
textview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 50);
精彩评论