0

我试图在 TableLayout 中获得一个 TableRow,以在用户触摸它时更改背景颜色,但它似乎不起作用。当我单击 TableRow 时,没有任何反应。这是我到目前为止所拥有的:

布局:

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  style="@style/BaseStyle"
   android:stretchColumns="*">

      <TableRow style="@style/TableRow">   
           <TextView 
                 android:id="@+id/settings
                 style="@style/BaseStyle.SettingsText"
                 android:text="Settings"
                 android:onClick="onClick"
                 android:clickable="true"
            />  
      </TableRow>

 </TableLayout>

风格:

<style name="TableRow">
     <item name="android:background">@drawable/onclickhighlight</item>
</style>

可绘制(onclickhighlight.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">

     <!--  Active state --> 
     <item android:state_selected="true" android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/background_light" />

      <!--  Inactive state-->
     <item android:state_selected="false" android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/transparent" />

      <!--  Pressed state-->
      <item android:state_selected="true" android:state_focused="true" android:state_pressed="true" android:drawable="@android:color/background_light" />

</selector>

颜色:

 <resources>
     <color name="background_light">#FFFFFF</color>
 </resources>

我知道 TableRow 正在使用 onclickhighlight StateListDrawable,因为如果我将“非活动状态”的 android:drawable 属性从 @android:color/transparent 更改为 @android:color/background_light,背景颜色将变为白色。

4

2 回答 2

0

我有同样的问题,这对我有用。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
    <item android:state_pressed="true" android:drawable="@color/yourcolor" />
    <item android:drawable="@color/yournormalbackground"/> 
</selector>
于 2012-09-02T07:20:19.493 回答
0

我遇到了完全相同的问题,我正在努力寻找答案。我敢打赌,如果您android:onClick="onClick"要从表格行 xml 中删除,您会发现突出显示会起作用。但是,您如何让点击事件与您想要的方法相关联?!

于 2011-10-29T06:24:10.580 回答