【发布时间】:2016-02-11 01:40:06
【问题描述】:
我有一个 TextView,它与占用剩余空间的其他 2 个项目对齐。 TextView 是可点击的,并且 textview 的背景颜色在点击时会发生变化。问题是更改了完整视图的背景,而不仅仅是 textview 中的文本。有没有办法只为文本更改背景颜色?
<TextView
android:id="@+id/client_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:layout_toStartOf="@+id/client_header_end_text"
android:layout_toEndOf="@id/client_header_back"
android:background="@drawable/action_bar_item_background"
android:ellipsize="end"
android:maxLines="1"/>
action_bar_item_background:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/action_bar_item_background_pressed" />
</selector>
action_bar_item_background_pressed:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/light_grey" />
</shape>
【问题讨论】:
-
你可以通过 text.setTextColor();