默认情况下,ListView在按下时将背景颜色更改为橙色,TextColorTextView也将更改为白色/黑色。通过对ListView项应用选择器,我们可以为按下状态设置背景色,但我们是否也可以在同一选择器中为该项/布局内的TextColor定义TextView?如果是,那怎么办?

最佳答案

amy88的答案解决了这个问题,但并没有具体解决更改viewgroup子属性的问题。
假设您有一个带LinearLayout的可点击的TextView,当按下时它会改变颜色。关键是在子视图上使用android:duplicateParentState="true"

<LinearLayout
    android:duplicateParentState="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:duplicateParentState="true"
        android:text="@string/hello_world"
        android:textColor="@drawable/text_selector" />

</LinearLayout>

您可以在amy88的答案中指定选择器。

08-05 20:57
查看更多