我想要一个简单的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" />
除了文本颜色在聚焦状态下没有改变外,其他一切都正常。我如何使它改变为textappearance elargeinverse ?
下面是我的实现,它的行为与list中的item完全相同(至少在2.3上)
xml res - - list_video_footer布局。
<?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 /颜色/ 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>
你试过setOnFocusChangeListener吗?在处理程序中,您可以更改文本外观。
例如:
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();
}
}
});
然后,您可以应用您想要的任何更改,无论它是否集中。你也可以使用ViewTreeObserver来监听全局焦点的变化。
例如:
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
}
});
我希望这能给你们一些启发。