我试图在 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,背景颜色将变为白色。