StateListDrawable 不适用于 TableRow
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有完全相同的问题,我正在努力寻找答案。我敢打赌,如果您要从表行 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?!我遇到了同样的问题,这对我有用。
I had the same problem, and this is what worked for me.