簡體   English   中英

單擊其子級時更改其父級的背景顏色

[英]Change parent's background color when its child is clicked

我有一個TableLine,其中包含LinearLayout,然后此LinearLayout包含TextView。 我想要的是,一旦單擊TextView,整個TableRow就會更改其背景顏色。

我試圖使用getParent()和performClick()將click事件從TextView傳遞到TableRow。 確實會調用TableRow的onClick()方法,但其背景顏色不會改變。

當然,我可以通過使用

row.setBackgroundResource(R.drawable.menu_item_bgcolor);

要么

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

不起作用 誰能對此提供任何見解? 謝謝,

以下是選擇器xml文件:

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

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM