我想要一个简单的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 ?


当前回答

为了让它在列表视图中选择工作,请使用以下代码:

<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>

显然,键是state_activated="true"状态。

其他回答

下面是我的实现,它的行为与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
    }
});

我希望这能给你们一些启发。

如果在选项卡中使用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>

我总是使用上面的解决方案,在此之后没有搜索更多。: -)

然而,今天我偶然发现了一些东西,想分享给大家。:)

这个特性确实可以从API 1中获得,称为ColorStateList,我们可以在其中为widget的各种状态提供颜色(正如我们已经知道的那样)。

这里也有很好的记录。

selector也是这里的答案。

在源代码中搜索bright_text_dark_focused.xml,添加到res/color目录下的项目,然后从TextView引用为

android:textColor="@color/bright_text_dark_focused"