简体   繁体   English

StateListDrawable在TableRow上不起作用

[英]StateListDrawable not working on TableRow

I'm trying to get a TableRow, in a TableLayout, to change the background color when a user touches it, but it doesn't seem to be working. 我正在尝试在TableLayout中获得一个TableRow,以在用户触摸它时更改背景颜色,但是它似乎不起作用。 When I click on the TableRow, nothing happens. 当我单击TableRow时,没有任何反应。 This what I have so far: 这是我到目前为止所拥有的:

Layout: 布局:

 <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: 样式:

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

Drawable (onclickhighlight.xml) 可绘制(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>

Colors: 颜色:

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

I know the TableRow is using the onclickhighlight StateListDrawable because if I change the "Inactive state"s android:drawable property from @android:color/transparent, to @android:color/background_light, the background color changes to white. 我知道TableRow正在使用onclickhighlight StateListDrawable,因为如果我将“非活动状态”的android:drawable属性从@android:color / transparent更改为@android:color / background_light,则背景色将更改为白色。

I had the same problem, and this is what worked for me. 我遇到了同样的问题,这对我有用。

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

I am having exactly the same issue and I'm trying to find the answer. 我遇到完全相同的问题,并试图找到答案。 I bet if you were to remove the android:onClick="onClick" from the table row xml, you'd find the highlight would work. 我敢打赌,如果您要从表格行xml中删除android:onClick="onClick" ,您会发现突出显示将起作用。 But then, how would you get the click event to associate with the method you want?! 但是,然后,您将如何使click事件与所需的方法相关联?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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