简体   繁体   中英

Change parent's background color when its child is clicked

I have a TableRow which contains a LinearLayout, and then this LinearLayout contains a TextView. What I want is, once the TextView is clicked, the entire TableRow would change its background color.

I've tried to use getParent() and performClick() to pass the click event from TextView to TableRow. The onClick() method of TableRow does get called, but its background color does not change.

Of course I've set the selector by using either

row.setBackgroundResource(R.drawable.menu_item_bgcolor);

or

row.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.menu_item_bgcolor));

Doesn't work. Can anyone provide any insight into this? Thanks,

Below is the selector xml file:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" android:drawable="@drawable/menu_item_pressed" />
<item android:state_focused="true" android:drawable="@drawable/menu_item_pressed" />
<item android:drawable="@drawable/menu_item_normal" />

</selector>

try this

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
    getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
    getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
    getResources().getDrawable(R.drawable.normal));
row.setImageDrawable(states);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM