StateListDrawable 不适用于 TableRow

发布于 2024-12-11 13:01:44 字数 1864 浏览 0 评论 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>

Drawable (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:color/transparent 更改“非活动状态”的 android:drawable 属性,到@android:color/background_light,背景颜色变为白色。

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. When I click on the TableRow, nothing happens. 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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路弥 2024-12-18 13:01:44

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

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. But then, how would you get the click event to associate with the method you want?!

青丝拂面 2024-12-18 13:01:44

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

<?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 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文