How to implement color selector of TextView when the OnClick listener is set on its parent Layout?
The layout xml is as below. I have a RelativeLayout, which contains a TextView. The OnClick listener is set on RelativeLayout. The RelativeLayout has a selector background. What I want is, when user clicks on the RelativeLayout, the background of the RelativeLayout should change, and the color of the text of the TextView should change too. Even though I set co开发者_开发知识库lor selector for the TextView, only the selector on RelativeLayout works. The color selector on TextView doesn't work. How can I implement change of both RelativeLayout background and text color of TextView when user clicks the layout? Thanks.
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_selector"
android:id="@+id/mylayout"
>
<TextView android:id="@+id/mytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="sans"
android:textSize="18sp"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
android:textColor="@drawable/color_blue_selector"
/>
</RelativeLayout>
Take a look at android:duplicateParentState: http://developer.android.com/reference/android/view/View.html#attr_android:duplicateParentState.
Btw, this question is also answered here: TextView color change on focus/press
Good luck!
Isn't it just to
relativeLayout.setBackgroundResource(R.color.brown);
textView.setTextColor(R.color.white);
... having brown and white defined in res/values/color.xml defined as
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="brown">#2E2B27</color>
</resources>
Perhaps you could use OnTouchListener
and respond on Down and Up events changing the text color
精彩评论